Blame src/check_pack.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "../lib/libcompat.h"
Packit 0b5880
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
#include <string.h>
Packit 0b5880
#include <stdio.h>
Packit 0b5880
Packit 0b5880
#include "check.h"
Packit 0b5880
#include "check_error.h"
Packit 0b5880
#include "check_list.h"
Packit 0b5880
#include "check_impl.h"
Packit 0b5880
#include "check_pack.h"
Packit 0b5880
Packit 0b5880
#ifndef HAVE_PTHREAD
Packit 0b5880
#define pthread_mutex_lock(arg)
Packit 0b5880
#define pthread_mutex_unlock(arg)
Packit 0b5880
#define pthread_cleanup_push(f, a) {
Packit 0b5880
#define pthread_cleanup_pop(e) }
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
/* Maximum size for one message in the message stream. */
Packit 0b5880
static size_t ck_max_msg_size = 0;
Packit 0b5880
#ifndef DEFAULT_MAX_MSG_SIZE
Packit 0b5880
#define DEFAULT_MAX_MSG_SIZE 4096
Packit 0b5880
#endif
Packit 0b5880
/* This is used to implement a sliding window on the receiving
Packit 0b5880
 * side. When sending messages, we assure that no single message
Packit 0b5880
 * is bigger than this.
Packit 0b5880
 * The usual size for a message is less than 80 bytes.
Packit 0b5880
 * All this is done instead of the previous approach to allocate (actually
Packit 0b5880
 * continuously reallocate) one big chunk for the whole message stream.
Packit 0b5880
 * Problems were seen in the wild with up to 4 GB reallocations.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
void check_set_max_msg_size(size_t max_msg_size)
Packit 0b5880
{
Packit 0b5880
    ck_max_msg_size = max_msg_size;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static size_t get_max_msg_size(void)
Packit 0b5880
{
Packit 0b5880
    size_t value = 0;
Packit 0b5880
    char *env = getenv("CK_MAX_MSG_SIZE");
Packit 0b5880
    if (env)
Packit 0b5880
        value = (size_t)strtoul(env, NULL, 10); // Cast in case size_t != unsigned long.
Packit 0b5880
    if (value == 0)
Packit 0b5880
        value = ck_max_msg_size;
Packit 0b5880
    if (value == 0)
Packit 0b5880
        value = DEFAULT_MAX_MSG_SIZE;
Packit 0b5880
    return value;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
/* typedef an unsigned int that has at least 4 bytes */
Packit 0b5880
typedef uint32_t ck_uint32;
Packit 0b5880
Packit 0b5880
Packit 0b5880
static void pack_int(char **buf, int val);
Packit 0b5880
static int upack_int(char **buf);
Packit 0b5880
static void pack_str(char **buf, const char *str);
Packit 0b5880
static char *upack_str(char **buf);
Packit 0b5880
Packit 0b5880
static int pack_ctx(char **buf, CtxMsg * cmsg);
Packit 0b5880
static int pack_loc(char **buf, LocMsg * lmsg);
Packit 0b5880
static int pack_fail(char **buf, FailMsg * fmsg);
Packit 0b5880
static int pack_duration(char **buf, DurationMsg * fmsg);
Packit 0b5880
static void upack_ctx(char **buf, CtxMsg * cmsg);
Packit 0b5880
static void upack_loc(char **buf, LocMsg * lmsg);
Packit 0b5880
static void upack_fail(char **buf, FailMsg * fmsg);
Packit 0b5880
static void upack_duration(char **buf, DurationMsg * fmsg);
Packit 0b5880
Packit 0b5880
static void check_type(int type, const char *file, int line);
Packit 0b5880
static enum ck_msg_type upack_type(char **buf);
Packit 0b5880
static void pack_type(char **buf, enum ck_msg_type type);
Packit 0b5880
Packit 0b5880
static int read_buf(FILE * fdes, int size, char *buf);
Packit 0b5880
static int get_result(char *buf, RcvMsg * rmsg);
Packit 0b5880
static void rcvmsg_update_ctx(RcvMsg * rmsg, enum ck_result_ctx ctx);
Packit 0b5880
static void rcvmsg_update_loc(RcvMsg * rmsg, const char *file, int line);
Packit 0b5880
static RcvMsg *rcvmsg_create(void);
Packit 0b5880
void rcvmsg_free(RcvMsg * rmsg);
Packit 0b5880
Packit 0b5880
typedef int (*pfun) (char **, CheckMsg *);
Packit 0b5880
typedef void (*upfun) (char **, CheckMsg *);
Packit 0b5880
Packit 0b5880
static pfun pftab[] = {
Packit 0b5880
    (pfun) pack_ctx,
Packit 0b5880
    (pfun) pack_fail,
Packit 0b5880
    (pfun) pack_loc,
Packit 0b5880
    (pfun) pack_duration
Packit 0b5880
};
Packit 0b5880
Packit 0b5880
static upfun upftab[] = {
Packit 0b5880
    (upfun) upack_ctx,
Packit 0b5880
    (upfun) upack_fail,
Packit 0b5880
    (upfun) upack_loc,
Packit 0b5880
    (upfun) upack_duration
Packit 0b5880
};
Packit 0b5880
Packit 0b5880
int pack(enum ck_msg_type type, char **buf, CheckMsg * msg)
Packit 0b5880
{
Packit 0b5880
    if(buf == NULL)
Packit 0b5880
        return -1;
Packit 0b5880
    if(msg == NULL)
Packit 0b5880
        return 0;
Packit 0b5880
Packit 0b5880
    check_type(type, __FILE__, __LINE__);
Packit 0b5880
Packit 0b5880
    return pftab[type] (buf, msg);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
int upack(char *buf, CheckMsg * msg, enum ck_msg_type *type)
Packit 0b5880
{
Packit 0b5880
    char *obuf;
Packit 0b5880
Packit 0b5880
    if(buf == NULL)
Packit 0b5880
        return -1;
Packit 0b5880
Packit 0b5880
    obuf = buf;
Packit 0b5880
Packit 0b5880
    *type = upack_type(&buf;;
Packit 0b5880
Packit 0b5880
    check_type(*type, __FILE__, __LINE__);
Packit 0b5880
Packit 0b5880
    upftab[*type] (&buf, msg);
Packit 0b5880
Packit 0b5880
    return buf - obuf;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void pack_int(char **buf, int val)
Packit 0b5880
{
Packit 0b5880
    unsigned char *ubuf = (unsigned char *)*buf;
Packit 0b5880
    ck_uint32 uval = val;
Packit 0b5880
Packit 0b5880
    ubuf[0] = (unsigned char)((uval >> 24) & 0xFF);
Packit 0b5880
    ubuf[1] = (unsigned char)((uval >> 16) & 0xFF);
Packit 0b5880
    ubuf[2] = (unsigned char)((uval >> 8) & 0xFF);
Packit 0b5880
    ubuf[3] = (unsigned char)(uval & 0xFF);
Packit 0b5880
Packit 0b5880
    *buf += 4;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int upack_int(char **buf)
Packit 0b5880
{
Packit 0b5880
    unsigned char *ubuf = (unsigned char *)*buf;
Packit 0b5880
    ck_uint32 uval;
Packit 0b5880
Packit 0b5880
    uval =
Packit 0b5880
        (ck_uint32) ((ubuf[0] << 24) | (ubuf[1] << 16) | (ubuf[2] << 8) |
Packit 0b5880
                     ubuf[3]);
Packit 0b5880
Packit 0b5880
    *buf += 4;
Packit 0b5880
Packit 0b5880
    return (int)uval;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void pack_str(char **buf, const char *val)
Packit 0b5880
{
Packit 0b5880
    int strsz;
Packit 0b5880
Packit 0b5880
    if(val == NULL)
Packit 0b5880
        strsz = 0;
Packit 0b5880
    else
Packit 0b5880
        strsz = strlen(val);
Packit 0b5880
Packit 0b5880
    pack_int(buf, strsz);
Packit 0b5880
Packit 0b5880
    if(strsz > 0)
Packit 0b5880
    {
Packit 0b5880
        memcpy(*buf, val, strsz);
Packit 0b5880
        *buf += strsz;
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static char *upack_str(char **buf)
Packit 0b5880
{
Packit 0b5880
    char *val;
Packit 0b5880
    int strsz;
Packit 0b5880
Packit 0b5880
    strsz = upack_int(buf);
Packit 0b5880
Packit 0b5880
    if(strsz > 0)
Packit 0b5880
    {
Packit 0b5880
        val = (char *)emalloc(strsz + 1);
Packit 0b5880
        memcpy(val, *buf, strsz);
Packit 0b5880
        val[strsz] = 0;
Packit 0b5880
        *buf += strsz;
Packit 0b5880
    }
Packit 0b5880
    else
Packit 0b5880
    {
Packit 0b5880
        val = (char *)emalloc(1);
Packit 0b5880
        *val = 0;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    return val;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void pack_type(char **buf, enum ck_msg_type type)
Packit 0b5880
{
Packit 0b5880
    pack_int(buf, (int)type);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static enum ck_msg_type upack_type(char **buf)
Packit 0b5880
{
Packit 0b5880
    return (enum ck_msg_type)upack_int(buf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
Packit 0b5880
static int pack_ctx(char **buf, CtxMsg * cmsg)
Packit 0b5880
{
Packit 0b5880
    char *ptr;
Packit 0b5880
    int len;
Packit 0b5880
Packit 0b5880
    len = 4 + 4;
Packit 0b5880
    *buf = ptr = (char *)emalloc(len);
Packit 0b5880
Packit 0b5880
    pack_type(&ptr, CK_MSG_CTX);
Packit 0b5880
    pack_int(&ptr, (int)cmsg->ctx);
Packit 0b5880
Packit 0b5880
    return len;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void upack_ctx(char **buf, CtxMsg * cmsg)
Packit 0b5880
{
Packit 0b5880
    cmsg->ctx = (enum ck_result_ctx)upack_int(buf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int pack_duration(char **buf, DurationMsg * cmsg)
Packit 0b5880
{
Packit 0b5880
    char *ptr;
Packit 0b5880
    int len;
Packit 0b5880
Packit 0b5880
    len = 4 + 4;
Packit 0b5880
    *buf = ptr = (char *)emalloc(len);
Packit 0b5880
Packit 0b5880
    pack_type(&ptr, CK_MSG_DURATION);
Packit 0b5880
    pack_int(&ptr, cmsg->duration);
Packit 0b5880
Packit 0b5880
    return len;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void upack_duration(char **buf, DurationMsg * cmsg)
Packit 0b5880
{
Packit 0b5880
    cmsg->duration = upack_int(buf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int pack_loc(char **buf, LocMsg * lmsg)
Packit 0b5880
{
Packit 0b5880
    char *ptr;
Packit 0b5880
    int len;
Packit 0b5880
Packit 0b5880
    len = 4 + 4 + (lmsg->file ? strlen(lmsg->file) : 0) + 4;
Packit 0b5880
    *buf = ptr = (char *)emalloc(len);
Packit 0b5880
Packit 0b5880
    pack_type(&ptr, CK_MSG_LOC);
Packit 0b5880
    pack_str(&ptr, lmsg->file);
Packit 0b5880
    pack_int(&ptr, lmsg->line);
Packit 0b5880
Packit 0b5880
    return len;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void upack_loc(char **buf, LocMsg * lmsg)
Packit 0b5880
{
Packit 0b5880
    lmsg->file = upack_str(buf);
Packit 0b5880
    lmsg->line = upack_int(buf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int pack_fail(char **buf, FailMsg * fmsg)
Packit 0b5880
{
Packit 0b5880
    char *ptr;
Packit 0b5880
    int len;
Packit 0b5880
Packit 0b5880
    len = 4 + 4 + (fmsg->msg ? strlen(fmsg->msg) : 0);
Packit 0b5880
    *buf = ptr = (char *)emalloc(len);
Packit 0b5880
Packit 0b5880
    pack_type(&ptr, CK_MSG_FAIL);
Packit 0b5880
    pack_str(&ptr, fmsg->msg);
Packit 0b5880
Packit 0b5880
    return len;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void upack_fail(char **buf, FailMsg * fmsg)
Packit 0b5880
{
Packit 0b5880
    fmsg->msg = upack_str(buf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void check_type(int type, const char *file, int line)
Packit 0b5880
{
Packit 0b5880
    if(type < 0 || type >= CK_MSG_LAST)
Packit 0b5880
        eprintf("Bad message type arg %d", file, line, type);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
#ifdef HAVE_PTHREAD
Packit 0b5880
static pthread_mutex_t ck_mutex_lock = PTHREAD_MUTEX_INITIALIZER;
Packit 0b5880
static void ppack_cleanup(void *mutex)
Packit 0b5880
{
Packit 0b5880
    pthread_mutex_unlock((pthread_mutex_t *)mutex);
Packit 0b5880
}
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
void ppack(FILE * fdes, enum ck_msg_type type, CheckMsg * msg)
Packit 0b5880
{
Packit 0b5880
    char *buf = NULL;
Packit 0b5880
    int n;
Packit 0b5880
    ssize_t r;
Packit 0b5880
Packit 0b5880
    n = pack(type, &buf, msg);
Packit 0b5880
    /* Keep it on the safe side to not send too much data. */
Packit 0b5880
    if(n > get_max_msg_size())
Packit 0b5880
        eprintf("Message string too long", __FILE__, __LINE__ - 2);
Packit 0b5880
Packit 0b5880
    pthread_cleanup_push(ppack_cleanup, &ck_mutex_lock);
Packit 0b5880
    pthread_mutex_lock(&ck_mutex_lock);
Packit 0b5880
    r = fwrite(buf, 1, n, fdes);
Packit 0b5880
    fflush(fdes);
Packit 0b5880
    pthread_mutex_unlock(&ck_mutex_lock);
Packit 0b5880
    pthread_cleanup_pop(0);
Packit 0b5880
    if(r != n)
Packit 0b5880
        eprintf("Error in call to fwrite:", __FILE__, __LINE__ - 2);
Packit 0b5880
Packit 0b5880
    free(buf);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int read_buf(FILE * fdes, int size, char *buf)
Packit 0b5880
{
Packit 0b5880
    int n;
Packit 0b5880
Packit 0b5880
    n = fread(buf, 1, size, fdes);
Packit 0b5880
Packit 0b5880
    if(ferror(fdes))
Packit 0b5880
    {
Packit 0b5880
        eprintf("Error in call to fread:", __FILE__, __LINE__ - 4);
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    return n;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static int get_result(char *buf, RcvMsg * rmsg)
Packit 0b5880
{
Packit 0b5880
    enum ck_msg_type type;
Packit 0b5880
    CheckMsg msg;
Packit 0b5880
    int n;
Packit 0b5880
Packit 0b5880
    n = upack(buf, &msg, &type);
Packit 0b5880
    if(n == -1)
Packit 0b5880
        eprintf("Error in call to upack", __FILE__, __LINE__ - 2);
Packit 0b5880
Packit 0b5880
    if(type == CK_MSG_CTX)
Packit 0b5880
    {
Packit 0b5880
        CtxMsg *cmsg = (CtxMsg *) & msg;
Packit 0b5880
Packit 0b5880
        rcvmsg_update_ctx(rmsg, cmsg->ctx);
Packit 0b5880
    }
Packit 0b5880
    else if(type == CK_MSG_LOC)
Packit 0b5880
    {
Packit 0b5880
        LocMsg *lmsg = (LocMsg *) & msg;
Packit 0b5880
Packit 0b5880
        if(rmsg->failctx == CK_CTX_INVALID)
Packit 0b5880
        {
Packit 0b5880
            rcvmsg_update_loc(rmsg, lmsg->file, lmsg->line);
Packit 0b5880
        }
Packit 0b5880
        free(lmsg->file);
Packit 0b5880
    }
Packit 0b5880
    else if(type == CK_MSG_FAIL)
Packit 0b5880
    {
Packit 0b5880
        FailMsg *fmsg = (FailMsg *) & msg;
Packit 0b5880
Packit 0b5880
        if(rmsg->msg == NULL)
Packit 0b5880
        {
Packit 0b5880
            rmsg->msg = strdup(fmsg->msg);
Packit 0b5880
            rmsg->failctx = rmsg->lastctx;
Packit 0b5880
        }
Packit 0b5880
        else
Packit 0b5880
        {
Packit 0b5880
            /* Skip subsequent failure messages, only happens for CK_NOFORK */
Packit 0b5880
        }
Packit 0b5880
        free(fmsg->msg);
Packit 0b5880
    }
Packit 0b5880
    else if(type == CK_MSG_DURATION)
Packit 0b5880
    {
Packit 0b5880
        DurationMsg *cmsg = (DurationMsg *) & msg;
Packit 0b5880
Packit 0b5880
        rmsg->duration = cmsg->duration;
Packit 0b5880
    }
Packit 0b5880
    else
Packit 0b5880
        check_type(type, __FILE__, __LINE__);
Packit 0b5880
Packit 0b5880
    return n;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void reset_rcv_test(RcvMsg * rmsg)
Packit 0b5880
{
Packit 0b5880
    rmsg->test_line = -1;
Packit 0b5880
    rmsg->test_file = NULL;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void reset_rcv_fixture(RcvMsg * rmsg)
Packit 0b5880
{
Packit 0b5880
    rmsg->fixture_line = -1;
Packit 0b5880
    rmsg->fixture_file = NULL;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static RcvMsg *rcvmsg_create(void)
Packit 0b5880
{
Packit 0b5880
    RcvMsg *rmsg;
Packit 0b5880
Packit 0b5880
    rmsg = (RcvMsg *)emalloc(sizeof(RcvMsg));
Packit 0b5880
    rmsg->lastctx = CK_CTX_INVALID;
Packit 0b5880
    rmsg->failctx = CK_CTX_INVALID;
Packit 0b5880
    rmsg->msg = NULL;
Packit 0b5880
    rmsg->duration = -1;
Packit 0b5880
    reset_rcv_test(rmsg);
Packit 0b5880
    reset_rcv_fixture(rmsg);
Packit 0b5880
    return rmsg;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void rcvmsg_free(RcvMsg * rmsg)
Packit 0b5880
{
Packit 0b5880
    free(rmsg->fixture_file);
Packit 0b5880
    free(rmsg->test_file);
Packit 0b5880
    free(rmsg->msg);
Packit 0b5880
    free(rmsg);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void rcvmsg_update_ctx(RcvMsg * rmsg, enum ck_result_ctx ctx)
Packit 0b5880
{
Packit 0b5880
    if(rmsg->lastctx != CK_CTX_INVALID)
Packit 0b5880
    {
Packit 0b5880
        free(rmsg->fixture_file);
Packit 0b5880
        reset_rcv_fixture(rmsg);
Packit 0b5880
    }
Packit 0b5880
    rmsg->lastctx = ctx;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
static void rcvmsg_update_loc(RcvMsg * rmsg, const char *file, int line)
Packit 0b5880
{
Packit 0b5880
    if(rmsg->lastctx == CK_CTX_TEST)
Packit 0b5880
    {
Packit 0b5880
        free(rmsg->test_file);
Packit 0b5880
        rmsg->test_line = line;
Packit 0b5880
        rmsg->test_file = strdup(file);
Packit 0b5880
    }
Packit 0b5880
    else
Packit 0b5880
    {
Packit 0b5880
        free(rmsg->fixture_file);
Packit 0b5880
        rmsg->fixture_line = line;
Packit 0b5880
        rmsg->fixture_file = strdup(file);
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
RcvMsg *punpack(FILE * fdes)
Packit 0b5880
{
Packit 0b5880
    int nread, nparse, n;
Packit 0b5880
    char *buf;
Packit 0b5880
    RcvMsg *rmsg;
Packit 0b5880
Packit 0b5880
    rmsg = rcvmsg_create();
Packit 0b5880
Packit 0b5880
    /* Allcate a buffer */
Packit 0b5880
    buf = (char *)emalloc(get_max_msg_size() * 2);
Packit 0b5880
    /* Fill the buffer from the file */
Packit 0b5880
    nread = read_buf(fdes, get_max_msg_size() * 2, buf);
Packit 0b5880
    nparse = nread;
Packit 0b5880
    /* While not all parsed */
Packit 0b5880
    while(nparse > 0)
Packit 0b5880
    {
Packit 0b5880
        /* Parse one message */
Packit 0b5880
        n = get_result(buf, rmsg);
Packit 0b5880
        nparse -= n;
Packit 0b5880
        if (nparse < 0)
Packit 0b5880
            eprintf("Error in call to get_result", __FILE__, __LINE__ - 3);
Packit 0b5880
        /* Move remaining data in buffer to the beginning */
Packit 0b5880
        memmove(buf, buf + n, nparse);
Packit 0b5880
        /* If EOF has not been seen */
Packit 0b5880
        if(nread > 0)
Packit 0b5880
        {
Packit 0b5880
            /* Read more data into empty space at end of the buffer */
Packit 0b5880
            nread = read_buf(fdes, n, buf + nparse);
Packit 0b5880
            nparse += nread;
Packit 0b5880
        }
Packit 0b5880
    }
Packit 0b5880
    free(buf);
Packit 0b5880
Packit 0b5880
    if(rmsg->lastctx == CK_CTX_INVALID)
Packit 0b5880
    {
Packit 0b5880
        free(rmsg);
Packit 0b5880
        rmsg = NULL;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    return rmsg;
Packit 0b5880
}