The following string truncation error is reported by gcc-11 and gcc-12
toolchains:
In file included from /usr/powerpc-linux-gnu/include/string.h:535,
from src/errinjct/ioa_bus_error.c:34:
In function ‘strncpy’,
inlined from ‘get_config_addr_from_reg’ at src/errinjct/ioa_bus_error.c:207:2,
inlined from ‘hunt_loc_code’ at src/errinjct/ioa_bus_error.c:415:9:
Warning: /usr/powerpc-linux-gnu/include/bits/string_fortified.h:95:10: warning: ‘__builtin_strncpy’ output may be truncated copying 3995 bytes from a string of length 3999 [-Wstringop-truncation]
This is the result of the caller defining a buffer of BUFSZ, but the
callee only copying BUFSZ-5 of data into a new string so that there is
room to strcat "/reg" to the resulting pathname. We can save a strncpy
and static buffer allocation by doing the strcat of "/reg" to the
pathname before calling get_conig_addr_from_reg() which in general
appears to be inlined anyways.
Reported-by: John Paul Adrian Glaubitz <
glau...@physik.fu-berlin.de>
Signed-off-by: Tyrel Datwyler <
tyr...@linux.ibm.com>
---
src/errinjct/ioa_bus_error.c | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/src/errinjct/ioa_bus_error.c b/src/errinjct/ioa_bus_error.c
index 9d85cfa6875c..94662aa42d87 100644
--- a/src/errinjct/ioa_bus_error.c
+++ b/src/errinjct/ioa_bus_error.c
@@ -194,20 +194,16 @@ int ioa_bus_error_arg(char arg, char *optarg)
* Given the directory /proc/device-tree/pci@...,
* yank out the config address out of the reg file
*
- * @param devpath device-tree path of the reg file
+ * @param regpath device-tree path of the reg file
* @return 0 on failure, config address (!0) on success
*/
-static uint32_t get_config_addr_from_reg(char *devpath)
+static uint32_t get_config_addr_from_reg(char *regpath)
{
- char path[BUFSZ];
char *buf;
uint32_t *be_caddr;
uint32_t caddr = 0;
- strncpy(path, devpath, BUFSZ-5);
- strcat(path, "/reg");
-
- buf = read_file(path, NULL);
+ buf = read_file(regpath, NULL);
if (!buf)
return 1;
@@ -272,6 +268,7 @@ static int parse_sysfsname(void)
/* Obtain the config address from the device-tree reg file */
strcpy(path, "/proc/device-tree/");
strcat(path, devspec);
+ strcat(path, "/reg");
addr = get_config_addr_from_reg(path);
if (addr) {
config_addr = addr;
@@ -412,6 +409,7 @@ static int hunt_loc_code(void)
phb_id_lo = phb_id & 0xFFFFFFFF;
/* Try to get the config address from the dev-tree reg file. */
+ strcat(path, "/reg");
addr = get_config_addr_from_reg(path);
if (addr) {
config_addr = addr;
--
2.39.0