C: Valgrind: using an uninitialized value of size 4 And using an uninitialized value of size 4

These 2 errors appear with a page / low level read / strtok file.

==10982== Use of uninitialised value of size 4 ==10982== at 0x40C3899: strtok (strtok.S:197) ==10982== by 0x8048719: main (main.c:9) ==10982== Uninitialised value was created by a stack allocation ==10982== at 0x80487D2: buildList (functions.c:13) ==10982== ==10982== Use of uninitialised value of size 4 ==10982== at 0x40C38C1: strtok (strtok.S:223) ==10982== by 0x8048719: main (main.c:9) ==10982== Uninitialised value was created by a stack allocation ==10982== at 0x80487D2: buildList (functions.c:13) ==10982== ==10982== Conditional jump or move depends on uninitialised value(s) ==10982== at 0x40C38C4: strtok (strtok.S:224) ==10982== by 0x8048719: main (main.c:9) ==10982== Uninitialised value was created by a stack allocation ==10982== at 0x80487D2: buildList (functions.c:13) 

Here is the buildList function

 void buildList(int fdin, int lines) { char ara[4096]; int num; double p; read(fdin, ara, 4096); char *nl = "\n"; char *ws = " "; char *temp = strtok(ara, " "); while(temp != NULL) { Stock *s = (Stock*)calloc(1, sizeof(Stock)); s->c.symbol = (char*)calloc(strlen(temp)+1, sizeof(char)); strcpy(s->c.symbol, temp); temp = strtok(NULL, "\n"); s->c.name = (char*)calloc(strlen(temp)+1, sizeof(char)); strcpy(s->c.name, temp); temp = strtok(NULL, "\n"); sscanf(temp, "%lf", &p); temp = strtok(NULL, "\n"); s->price = p; sscanf(temp, "%d", &num); s->shares = num; Node *n = (Node*)calloc(1, sizeof(Node)); n->data = s; addOrdered(n); temp = strtok(NULL, " "); } close(fdin); } 

I can not understand why this error occurs. From what I read, this is because I am assigning char * things from strtok without assigning any memory to them. However, this is how I did it in the past, and I think that everything is in order.

+6
source share
3 answers

As others have suggested, valgrind complains that strtok uses uninitialized memory. The question then becomes why, because it seems like it is initializing:

 read(fdin, ara, 4096); 

However, read() does not complete the \0 data, and strtok expects the \0 terminated string. Make sure the login is completed correctly:

 ssize_t result = read(fdin, ara, sizeof[ara]-1); /* leave room for '\0' */ ara[result > 0 ? result : 0] = '\0'; 

memset(ara, 0, sizeof(ara)); clause memset(ara, 0, sizeof(ara)); Lee Daniel Crocker meets the same fix, but it only works if the file contains less than sizeof(ara) bytes of input. You are still facing a similar problem if the file you are reading has 4096 bytes of data or more. Then strtok will be forced to scan beyond the end of the buffer to find \0 , which will lead to undefined behavior.

+4
source

read(fdin, ara, 4096);

... which may return:

  • -1, that is, the read received an error, and ara not filled;
  • 0, that is, you get to the end of the file, and ara not filled;
  • value <4096, i.e. there are less than 4096 bytes left in the file, and not all ara bytes are full.

Always check the return value of read() .

char * temp = strtok (ara, "");

 $ man strtok 

...

 The strtok() function is used to isolate sequential tokens in a null-ter- minated string, str. ... 

There is nothing that would guarantee that ara has a null character ( '\0' ) in it, so you cannot pass strtok() correctly. Do this char ara[4097] and do ara[4096] = '\0'; after reading, if you want to make sure that there is '\0' .

Or even better, if read() return value is in the data_read variable, and you already checked to make sure it is not -1, do ara[data_read] = '\0'; so that you set the byte after the last one is read before '\0' .

And what happens if the file contains more than 4096 characters, and your read() call reads, for example, several lines and the beginning of another line? With that in mind, you might consider using fopen() to open a file and using fgets() to read lines.

+3
source

Does the data read read null terminator at the end? If not, maybe a problem.

+1
source

Source: https://habr.com/ru/post/945810/


All Articles