Valgrind Invalid read size of 1
12,381
Here
char *url = malloc((length+1)*sizeof(char));
url = '\0';
strncat(url, space, length);
you immediately lose the malloced memory by setting url to NULL. Note that '\0' is 0, which is a null pointer constant. And then you try to strncat something to an invalid memory location.
You probably meant to set
*url = '\0';
there.
Related videos on Youtube
Author by
DMcB1888
Updated on July 11, 2022Comments
-
DMcB1888 12 monthsFor the life of me i cant work out why I am getting an
invalid read size of 1for this code snippet, I'm pretty sure its got something to do with me abusing thechar *url pointer...char *extractURL(char request[]) { char *space = malloc(sizeof(char *)); space = strchr(request, ' ')+1; char *hostend = malloc(sizeof(char *)); hostend = strchr(request, '\r'); int length = hostend - space; if (length > 0) { printf("Mallocing %d bytes for url\n.", length+1); char *url = (char *)malloc((length+1)*sizeof(char)); url = '\0'; strncat(url, space, length); return url; } //else we have hit an error so return NULL return NULL; }The valgrind error I am getting is :
==4156== Invalid read of size 1 ==4156== at 0x4007518: strncat (mc_replace_strmem.c:206) ==4156== by 0x8048D25: extractURL () ==4156== by 0x8048E59: processRequest () ==4156== by 0x8049881: main () ==4156== Address 0x0 is not stack'd, malloc'd or (recently) free'dCan someone 'point' me in the right direction?
-
DMcB1888 over 11 yearsThat then leads to strncat doing a conditional jump on an unintialized value though does it not? -
Daniel Fischer over 11 yearsWell, I would actually expect a segfault when callingstrncat(NULL,something,n), but valgrind might report that, if it prefers.