Skip to content

Commit 3b1e60d

Browse files
tervonenjakartben
authored andcommitted
lib: posix: Fix NULL pointer to strcmp in posix mqueue.
When a queue has been unlinked but not removed the name pointer is set to NULL. The queue is still in the list when adding new queues and the name pointer is passed to strcmp. Signed-off-by: Jari Tervonen <jari.tervonen@nordicsemi.no>
1 parent 364322e commit 3b1e60d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/posix/options/mqueue.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ static mqueue_object *find_in_list(const char *name)
424424

425425
while (mq != NULL) {
426426
msg_queue = (mqueue_object *)mq;
427-
if (strcmp(msg_queue->name, name) == 0) {
427+
if ((msg_queue->name != NULL) && (strcmp(msg_queue->name, name) == 0)) {
428428
return msg_queue;
429429
}
430430

tests/posix/xsi_realtime/src/mqueue.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,29 @@ ZTEST(xsi_realtime, test_mqueue_notify_errors)
271271
zassert_ok(mq_close(mqd), "Unable to close message queue descriptor.");
272272
zassert_ok(mq_unlink(queue), "Unable to unlink queue");
273273
}
274+
275+
ZTEST(xsi_realtime, test_mqueue_open_and_unlink_multiple)
276+
{
277+
const char *q1 = "q1";
278+
const char *q2 = "q2";
279+
280+
mqd_t mqd1, mqd2;
281+
struct mq_attr attrs = {
282+
.mq_msgsize = MESSAGE_SIZE,
283+
.mq_maxmsg = MESG_COUNT_PERMQ,
284+
};
285+
286+
int32_t mode = 0600;
287+
int flags = O_RDWR | O_CREAT;
288+
289+
mqd1 = mq_open(q1, flags, mode, &attrs);
290+
291+
zassert_ok(mq_unlink(q1), "Unable to unlink q1");
292+
293+
mqd2 = mq_open(q2, flags, mode, &attrs);
294+
295+
zassert_ok(mq_unlink(q2), "Unable to unlink q2");
296+
297+
zassert_ok(mq_close(mqd1), "Unable to close message queue 1 descriptor.");
298+
zassert_ok(mq_close(mqd2), "Unable to close message queue 2 descriptor.");
299+
}

0 commit comments

Comments
 (0)