From c91d8bd5f9102cb91ba6839cf98c40dd26521cd3 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Mon, 23 Mar 2020 11:30:10 +0100 Subject: [PATCH] zipl: use `unsigned int` for the iteration variable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the false positive CC zipl/src/misc.o misc.c: In function ‘misc_temp_dev’: misc.c:313:27: warning: ‘%04d’ directive writing between 4 and 11 bytes into a region of size 5 [-Wformat-overflow=] sprintf(filename, "zipl%04d", retry); ^~~~ misc.c:313:22: note: directive argument in the range [-2147483648, 999] sprintf(filename, "zipl%04d", retry); ^~~~~~~~~~ misc.c:313:4: note: ‘sprintf’ output between 9 and 16 bytes into a destination of size 9 sprintf(filename, "zipl%04d", retry); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ While at it, add an assertion that protects against a format-overflow, e.g. if TEMP_DEV_MAX_RETRIES changes. Reviewed-by: Jan Höppner Signed-off-by: Marc Hartmayer Signed-off-by: Jan Höppner --- zipl/src/misc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/zipl/src/misc.c b/zipl/src/misc.c index dff5c218..8d1a2ee1 100644 --- a/zipl/src/misc.c +++ b/zipl/src/misc.c @@ -9,6 +9,7 @@ * it under the terms of the MIT license. See LICENSE for details. */ +#include #include #include #include @@ -296,7 +297,7 @@ misc_temp_dev(dev_t dev, int blockdev, char** devno) char filename[] = "zipl0000"; mode_t mode; unsigned int path; - int retry; + unsigned int retry; int rc; int fd; @@ -310,7 +311,8 @@ misc_temp_dev(dev_t dev, int blockdev, char** devno) if (pathname[path] == NULL) continue; for (retry=0; retry < TEMP_DEV_MAX_RETRIES; retry++) { - sprintf(filename, "zipl%04d", retry); + assert(retry < 10000); + sprintf(filename, "zipl%04u", retry); result = misc_make_path(pathname[path], filename); if (result == NULL) return -1;