From 6d382d30d9ad4d0d594ebccef65aeb7d38ef2910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B6ppner?= Date: Wed, 7 Nov 2018 09:28:18 +0100 Subject: [PATCH] ipl_tools: Allocate string buffer dynamically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use util_asprintf() to allocate memory for the string buffers dynamically and get rid of the following GCC8 compile warnings: system.c: In function ‘print_fw_str’: system.c:86:46: warning: ‘%s’ directive output may be truncated writing up to 4095 bytes into a region of size 4082 [-Wformat-truncation=] snprintf(path, sizeof(path), "/sys/firmware/%s", file); ^~ system.c:98:19: read_fw_str(str, path, sizeof(str)); ~~~~ system.c:86:2: note: ‘snprintf’ output between 15 and 4110 bytes into a destination of size 4096 snprintf(path, sizeof(path), "/sys/firmware/%s", file); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cmd_chreipl.c: In function ‘set_reipl_type_helper’: cmd_chreipl.c:316:19: warning: ‘%d’ directive writing between 1 and 11 bytes into a region of size between 0 and 4095 [-Wformat-overflow=] sprintf(cmd, "%s %d:%d", chreipl_helper, major(dev), minor(dev)); ^~ cmd_chreipl.c:316:15: note: using the range [-2147483648, 2147483647] for directive argument sprintf(cmd, "%s %d:%d", chreipl_helper, major(dev), minor(dev)); ^~~~~~~~~~ cmd_chreipl.c:316:15: note: using the range [-2147483648, 2147483647] for directive argument cmd_chreipl.c:316:2: note: ‘sprintf’ output between 5 and 4120 bytes into a destination of size 4096 sprintf(cmd, "%s %d:%d", chreipl_helper, major(dev), minor(dev)); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Jan Höppner --- ipl_tools/Makefile | 2 ++ ipl_tools/cmd_chreipl.c | 8 +++++--- ipl_tools/system.c | 8 +++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ipl_tools/Makefile b/ipl_tools/Makefile index c5e99787..fe090417 100644 --- a/ipl_tools/Makefile +++ b/ipl_tools/Makefile @@ -4,6 +4,8 @@ libs = $(rootdir)/libutil/libutil.a all: chreipl lsreipl chshut lsshut +libs = $(rootdir)/libutil/libutil.a + objects = main.o ccw.o fcp.o system.o shutdown.o \ cmd_lsshut.o cmd_chshut.o cmd_lsreipl.o cmd_chreipl.o proc.o diff --git a/ipl_tools/cmd_chreipl.c b/ipl_tools/cmd_chreipl.c index 5e59199b..50cccf4e 100644 --- a/ipl_tools/cmd_chreipl.c +++ b/ipl_tools/cmd_chreipl.c @@ -303,19 +303,21 @@ static int set_reipl_type(const char *dev_name) static int get_chreipl_helper_cmd(dev_t dev, char cmd[PATH_MAX]) { - char chreipl_helper[PATH_MAX]; + char *chreipl_helper; struct proc_dev_entry pde; if (proc_dev_get_entry(dev, 1, &pde) != 0) return -1; - snprintf(chreipl_helper, PATH_MAX, - "%s/%s.%s", TOOLS_LIBDIR, "chreipl_helper", pde.name); + util_asprintf(&chreipl_helper, + "%s/%s.%s", TOOLS_LIBDIR, "chreipl_helper", pde.name); if (access(chreipl_helper, X_OK) != 0) { proc_dev_free_entry(&pde); + free(chreipl_helper); return -1; } sprintf(cmd, "%s %d:%d", chreipl_helper, major(dev), minor(dev)); proc_dev_free_entry(&pde); + free(chreipl_helper); return 0; } diff --git a/ipl_tools/system.c b/ipl_tools/system.c index 41121378..f3f511d3 100644 --- a/ipl_tools/system.c +++ b/ipl_tools/system.c @@ -9,6 +9,7 @@ * it under the terms of the MIT license. See LICENSE for details. */ +#include "lib/util_libc.h" #include "ipl_tools.h" /* @@ -81,10 +82,11 @@ void read_str(char *string, const char *path, size_t len) */ void read_fw_str(char *string, const char *file, size_t len) { - char path[PATH_MAX]; + char *path; - snprintf(path, sizeof(path), "/sys/firmware/%s", file); - return read_str(string, path, len); + util_asprintf(&path, "/sys/firmware/%s", file); + read_str(string, path, len); + free(path); } /*