[PATCH stalld 38/52] stalld: fix buffer underflow in sched_debug_get

Wander Lairson Costa <[email protected]> Mon, 8 Jun 2026 15:31:48 -0300
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
The null termination after the read loop writes to buffer[position-1]
without checking that position is non-zero. If the read returns no
data, position remains zero and the write underflows the heap-allocated
buffer, corrupting allocator metadata.

Add the same position guard already applied to read_proc_stat.

Signed-off-by: Wander Lairson Costa <[email protected]>
---
 src/sched_debug.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/sched_debug.c b/src/sched_debug.c
index 3754b56..5e9a9e8 100644
--- a/src/sched_debug.c
+++ b/src/sched_debug.c
@@ -50,7 +50,8 @@ static int sched_debug_get(char *buffer, int size)
 
 	} while (retval > 0 && position < size);
 
-	buffer[position-1] = '\0';
+	if (position > 0)
+		buffer[position-1] = '\0';
 
 	if (position + 100 > config_buffer_size) {
 		config_buffer_size = config_buffer_size * 2;
-- 
2.54.0