From 43651533d36b22ecb81ca9fb2fa65b6927289740 Mon Sep 17 00:00:00 2001 From: Cheyenne Wills Date: Fri, 26 Jul 2019 07:59:33 -0600 Subject: [PATCH] uss: uss_procs.c format-overflow warning GCC 9 introduced new warnings/errors and is flagging a sprintf with a format-overflow warning. With --checking-enabled, this error is causing uss_procs.c to fail during compile. A file name with the full path is being composed and the size of the buffer was triggering a possible format-overflow warning/error. Use asprintf to allocate the buffer dynamically instead of using a buffer sitting on the stack (reducing the stack requirements by 2K). Produces new error message if asprintf returns an error. Reviewed-on: https://gerrit.openafs.org/13664 Reviewed-by: Benjamin Kaduk Tested-by: BuildBot (cherry picked from commit 41ee558329560bce037ad2860282d8b49aa11b2d) Change-Id: I5c5866142ae17c92017201fb567f847b5c2907a0 Reviewed-on: https://gerrit.openafs.org/13729 Reviewed-by: Andrew Deason Reviewed-by: Marcio Brito Barbosa Reviewed-by: Michael Meffie Tested-by: BuildBot Reviewed-by: Stephan Wiesand --- src/uss/uss_procs.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/uss/uss_procs.c b/src/uss/uss_procs.c index 4e4807171..28fa78484 100644 --- a/src/uss/uss_procs.c +++ b/src/uss/uss_procs.c @@ -771,7 +771,9 @@ uss_procs_FindAndOpen(char *a_fileToOpen) FILE *rv; /*Template file descriptor */ int i; /*Loop counter */ - char tmp_str[uss_MAX_SIZE]; /*Tmp string */ + char *tmp_str; /*Points to the name of the file */ + /* -> a_fileToOpen or -> buffer @tmp_str_free */ + char *tmp_str_free = NULL; /*Dynamically built filename */ static char TemplatePath[NUM_TPL_PATHS][1024]; /*Template directories */ int cant_read; /*Can't read the file? */ @@ -780,13 +782,14 @@ uss_procs_FindAndOpen(char *a_fileToOpen) * If a full pathname was given, just take it as is. */ if (strchr(a_fileToOpen, '/')) { - strcpy(tmp_str, a_fileToOpen); + tmp_str = a_fileToOpen; rv = fopen(a_fileToOpen, "r"); } else { /* * A relative pathname was given. Try to find the file in each of * the default template directories. */ + int mem_error = 0; cant_read = 0; sprintf(TemplatePath[0], "%s", "."); @@ -794,7 +797,19 @@ uss_procs_FindAndOpen(char *a_fileToOpen) sprintf(TemplatePath[2], "%s", "/etc"); for (i = 0; i < NUM_TPL_PATHS; i++) { - sprintf(tmp_str, "%s/%s", TemplatePath[i], a_fileToOpen); + int code; + free(tmp_str_free); + tmp_str_free = NULL; + code = asprintf(&tmp_str_free, "%s/%s", + TemplatePath[i], a_fileToOpen); + if (code == -1) { + tmp_str_free = NULL; + mem_error = 1; + rv = NULL; + break; + } + tmp_str = tmp_str_free; + if ((rv = fopen(tmp_str, "r")) != NULL) break; @@ -821,7 +836,11 @@ uss_procs_FindAndOpen(char *a_fileToOpen) * Check to see if we specifically found the file but * couldn't read it. */ - if (cant_read) + if (mem_error) { + fprintf(stderr, "%s: Error allocating memory\n", + uss_whoami); + } + else if (cant_read) fprintf(stderr, "%s: Can't open template '%s': %s\n", uss_whoami, tmp_str, strerror(errno)); else { @@ -837,6 +856,7 @@ uss_procs_FindAndOpen(char *a_fileToOpen) /* * Whatever happened, return what we got. */ + free(tmp_str_free); return (rv); } /*uss_procs_FindAndOpen */ -- 2.39.5