[PATCH] selftests/ipc: Use IPC_STAT to get queue information

From: Muhammed Sariyildiz

Date: Sun Nov 01 2026 - 14:49:01 EST


dump_queue() finds the message queue's stats by brute-forcing kernel
indexes 0..255 with MSG_STAT and comparing the returned identifier with
msgque->msq_id. This misses queues whose index is 256 or larger, and if
no match is found the loop ends and the code goes on using a stale
or uninitialized struct msqid_ds.

The identifier is already known, so query it directly with IPC_STAT.
This removes the loop and reports a failure if the queue cannot be
queried.

Signed-off-by: Muhammed Sariyildiz <asiyee994@xxxxxxxxx>
---
tools/testing/selftests/ipc/msgque.c | 18 +++++-------------
1 file changed, 5 insertions(+), 13 deletions(-)

diff --git a/tools/testing/selftests/ipc/msgque.c b/tools/testing/selftests/ipc/msgque.c
index 4b4c1ab..f4168c7 100644
--- a/tools/testing/selftests/ipc/msgque.c
+++ b/tools/testing/selftests/ipc/msgque.c
@@ -130,21 +130,13 @@ err:
int dump_queue(struct msgque_data *msgque)
{
struct msqid_ds ds;
- int kern_id;
int i, ret;

- for (kern_id = 0; kern_id < 256; kern_id++) {
- ret = msgctl(kern_id, MSG_STAT, &ds);
- if (ret < 0) {
- if (errno == EINVAL)
- continue;
- ksft_test_result_fail("Failed to get stats for IPC queue with id %d\n",
- kern_id);
- return -errno;
- }
-
- if (ret == msgque->msq_id)
- break;
+ ret = msgctl(msgque->msq_id, IPC_STAT, &ds);
+ if (ret < 0) {
+ ksft_test_result_fail("Failed to get stats for IPC queue with id %d\n",
+ msgque->msq_id);
+ return -errno;
}

msgque->messages = malloc(sizeof(struct msg1) * ds.msg_qnum);
--
2.47.3