Skip to contentSkip to conversation

cyclicdeadline: Fix potential path truncation in open_cpuset()

PATCH In review John Kacur 2 participants 2 messages Aug 31 – Sep 1

lore
Share & tools
lore

John Kacur

author
#0 lore raw

PATCH 1/1 cyclicdeadline: Fix potential path truncation in open_cpuset()

gcc -Wformat-truncation warns that the "%s/%s" format in open_cpuset() may be truncated because 'path' is unbounded:

cyclicdeadline.c:293:39: warning: '/' directive output may be truncated writing 1 byte into a region of size between 0 and 1023

The existing idiom passed MAXPATH - 1 as the size and then manually NUL-terminated the last byte, which silently proceeded with a truncated path (leading to a stat()/open() on the wrong path) and left the warning unaddressed.

Use the full buffer size (snprintf() always NUL-terminates within it) and check the return value for truncation, returning an error to the caller, which already handles a negative return.

Assisted-by Claude:claude-opus-4-8
---
 src/sched_deadline/cyclicdeadline.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/cyclicdeadline.c +3 -2
index f97344035ca3..ff3356a9be3e 100644
@@ -289,8 +289,9 @@ static int open_cpuset(const char *path, const char *name)
 	int ret;
 	int fd;
 
-	buf[MAXPATH - 1] = 0;
-	snprintf(buf, MAXPATH - 1, "%s/%s", path, name);
+	ret = snprintf(buf, sizeof(buf), "%s/%s", path, name);
+	if (ret < 0 || ret >= (int)sizeof(buf))
+		return -1;
 
 	ret = stat(buf, &st);
 	if (ret < 0)
Signature · 1 line
2.55.0