cyclicdeadline: Fix potential path truncation in open_cpuset()
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.
--- src/sched_deadline/cyclicdeadline.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
src/sched_deadline/cyclicdeadline.c +3 -2
@@ -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
Quoted 2 lines · John Kacur
gcc -Wformat-truncation warns that the "%s/%s" format in
open_cpuset()may be truncated because 'path' is unbounded:
…
Quoted 1 line
Signed-off-by John Kacur <[email protected]>
Sebastian