Search code examples
cmemory-leaksc-strings

Unwanted text inside char array


I am trying to "hard code" a very small JSON parser in C for a little project and I am encountering two main problems : The string output contain some unwanted text that came from nowhere ( actually it's description from the man page of getaddrinfo(), and I have no idea how it got there). This is the output I get :

    3484.  The default is:
#
#precedence  ::1/128       50
#precedence  ::/0          40
#precedence  2002::/16     30
#precedence ::/96          20
#precedence ::ffff:0:0/96  10
#
#    For sites which prefer IPv4 connections change the last line to
#
#precedence ::ffff:0:0/96  100

#
# scopev4  <mask>  <value>
#    Add another rule to the RFC 6724 scope table for IPv4 addresses.
#    By default the scope IDs described in section 3.2 in RFC 6724 are
#    used.  Changing these defaults should hardly ever be necessary.
#    The defaults are equivalent to:
#
#scopev4 ::ffff:169.254.0.0/112  2
#scopev4 ::ffff:127.0.0.0/104    2
#scopev4 ::ffff:0.0.0.0/96       14
{id_src:103,accel_x:101,accel_y:114,accel_z:116,temp:101} <-- This is the only
wanted section !

The second problem I am getting is that when I try to parse two structures into a json, well, the json is kind of appending the two structures :

 ... The usual unwanted data before
{id_src:103,accel_x:101,accel_y:114,accel_z:116,temp:101}{$­ûrc:1id_src:103,accel_x:101,accel_y:114,accel_z:116,temp:101}

I tried reducing the allocated space and turned out to have an error on the free() since the char array allocated had it's size changed. This is also a weird behavior I don't really understand.

Here is the code :

... some code here
char* jsonData = malloc(1048*sizeof(char));
    strcat(jsonData,"{");
    char* champsTab[] = {"id_src","accel_x","accel_y","accel_z","temp"};
    char* buff=malloc(1024*sizeof(char));
    for(i=0;i<5;i++) {
        strcat(buff,champsTab[i]);
        char* data=malloc(4*sizeof(char));
        switch(i) {
            case 0:
                sprintf(data,":%d,",capteurs->id_src);
                strcat(buff,data);
                break;
            case 1:
                sprintf(data,":%d,",capteurs->accel_x);
                strcat(buff,data);
                break;
            case 2:
                sprintf(data,":%d,",capteurs->accel_y);
                strcat(buff,data);
                break;
            case 3:
                sprintf(data,":%d,",capteurs->accel_z);
                strcat(buff,data);
                break;
            case 4:
                sprintf(data,":%d",capteurs->temp);
                strcat(buff,data);
                break;

        }
        free(data);

    }
    strcat(jsonData,buff);
    strcat(jsonData,"}");

    printf("%s\n",jsonData);
    free(jsonData);
    free(buff);

    return 0;
}

Solution

  • The problem is most likely these two lines:

    char* jsonData = malloc(1048*sizeof(char));
    strcat(jsonData,"{");
    

    When you allocate memory with malloc, it will be uninitialized. The contents will be indeterminate and seemingly random. Therefore it's a large chance that the string terminator that strcat looks for to find the end will not be the first element in the memory. It might not even be in the allocated memory at all.

    There's a simple solution to this: Use strcpy instead of strcat:

    strcpy(jsonData,"{");
    

    You have the very same problem later with buff.