0
votes

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.

1
what is message ? - Jean-François Fabre♦
I think you mean: mq_send(logQueue, msg, strlen(msg), 0); - Jean-François Fabre♦
On which line does Valgrind say that there is an error? - Paul Floyd
@Jean-FrançoisFabre Sorry, it was copy-paste mistake, fixed. - Kamo Spertsian
@PaulFloyd It says first: Syscall param mq_timedsend(msg_ptr) points to unaddressable byte(s), and then the message in my question. Right at the line with asprintf. - Kamo Spertsian

1 Answers

2
votes

(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);