Valgrind error

I get this Valgrind error while using malloc but if I use calloc then there is no error.

I allocate 8 bytes for the string inside sprintf, 12 for the ip and 1 for the string terminator. This totals 21, so why is it talking about 20 bytes and 18 bytes ?

Partial code:

char ip[] = "80.80.80.80";
int buffer = 8 + strlen(ip); // 8 for headers fixed text
char *headers = malloc(buffer + 1);
sprintf(headers, " -r %s\r\n", ip);
send(foobar, headers, buffer, 0);

Your fixed-lenght text is only 6 characters, not 8. The substring "\r\n" is two bytes - carriage return and linefeed, not four bytes.

Yes, it seems gedit does not count the bytes correctly (and me neither). strlen states 6 bytes.

Valgrind is reporting the storage required correctly...