clinuxvalgrindasprintf

Valgrind about asprintf: address is 0 bytes inside a block of size <size> alloc'd


I have some code like this:

void logConnectionStatus(char * domain, char * status, mqd_t logQueue) {
    char * message;
    asprintf(&message, "Connection to domain %s: %s", domain, status);
    mq_send(logQueue, message, 1024, 0);
    free(msg);
}

but valgrind check says that

Address 0x566c0f5 is 0 bytes inside a block of size 53 alloc'd

What can be the reason? Thank you.


Solution

  • (that is assuming you mean mq_send(logQueue, msg, 1024, 0); since message is nowhere to be found here)

    asprintf call is okay (unless domain or status are corrupt/null pointers).

    But right afterwards, you're sending a message of size 1024, probably way beyond the msg string size (since domain and status are probably human-readable short strings).

    You should note down the number of printed characters that asprintf returns and use that in the next call:

    char * msg;
    int nb_printed = asprintf(&msg, "Connection to domain %s: %s", domain, status);
    mq_send(logQueue, msg, nb_printed, 0);
    free(msg);