Fatal Error of Missing Terminating Null in String Argument When using UDP Example in LabWindows™/CVI™

Updated Jun 6, 2018

Reported In

Software

  • LabWindows/CVI

Programming Language

  • C

Other

UDP

Issue Details

I'm using UDP example in LabWindows/CVI but I get an error in UDPReader.c file. The error happens in the errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msg)), the code is as below:

int CVICALLBACK UDPCallback (unsigned channel, int eventType, int errCode, void *callbackData)
{
    int error = 0,
        size;
    unsigned char  *msg = NULL;
    if (eventType == UDP_DATAREADY)
    {
        char            srcAddr[16];
        unsigned int    srcPort;
        char            msgSourceString[16 + 10];
        errChk (size = UDPRead(channel, NULL, 0, UDP_DO_NOT_WAIT, NULL, NULL));
        nullChk (msg = malloc(size));
        errChk (size = UDPRead(channel, msg, size, UDP_DO_NOT_WAIT, &srcPort, srcAddr));
        sprintf(msgSourceString, "[%s:%d]: ", srcAddr, srcPort);
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msgSourceString));       
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msg));
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, "\n"));
    }
Error:                                                               
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));;
    if (msg)
        free(msg);
    return 0;
}


The error code is:
FATAL RUN-TIME ERROR: Missing terminating null in string argument.

Solution

This error means the string argument msg missing terminating null '\0'.
If the size of you UDP message is 'size', then you should malloc 'size+1' memory to allow the msg to contain '\0' :

int CVICALLBACK UDPCallback (unsigned channel, int eventType, int errCode, void *callbackData)
{
    int error = 0,
        size;
    unsigned char  *msg = NULL;
    if (eventType == UDP_DATAREADY)
    {
        char            srcAddr[16];
        unsigned int    srcPort;
        char            msgSourceString[16 + 10];
        errChk (size = UDPRead(channel, NULL, 0, UDP_DO_NOT_WAIT, NULL, NULL));
        nullChk (msg = malloc(size+1));
        msg[size]='\0'

        errChk (size = UDPRead(channel, msg, size, UDP_DO_NOT_WAIT, &srcPort, srcAddr));
        sprintf(msgSourceString, "[%s:%d]: ", srcAddr, srcPort);
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msgSourceString));       
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, msg));
        errChk (SetCtrlVal(panelHandle, PANEL_TEXTBOX, "\n"));
    }
Error:                                                               
    if (error < 0)
        MessagePopup("Error", GetGeneralErrorString(error));;
    if (msg)
        free(msg);
    return 0;
}

WAS THIS ARTICLE HELPFUL?

Not Helpful