From d73680c5f70ee5aeb634a9ec88bf1097743d0f76 Mon Sep 17 00:00:00 2001 From: Cheyenne Wills Date: Mon, 11 May 2020 14:06:19 -0600 Subject: [PATCH] vol: Fix format-truncation warning with gcc-10.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Building with gcc-10.1 produces a warning (error if --enable-checking) in vol-salvage.c error: ‘%s’ directive output may be truncated writing up to 755 bytes into a region of size 255 [-Werror=format-truncation=] 809 | snprintf(inodeListPath, 255, "%s" OS_DIRSEP "salvage.inodes.%s.%d", tdir, name, Use strdup/asprintf to allocate the buffer dynamically instead of using a buffer with a hardcoded size. Change-Id: Ib2f01c2eb73c7abc162be2b1939e55688a81f812 Reviewed-on: https://gerrit.openafs.org/14207 Tested-by: BuildBot Reviewed-by: Benjamin Kaduk --- src/vol/vol-salvage.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/vol/vol-salvage.c b/src/vol/vol-salvage.c index b4a1062..6643968 100644 --- a/src/vol/vol-salvage.c +++ b/src/vol/vol-salvage.c @@ -694,7 +694,7 @@ void SalvageFileSys1(struct DiskPartition64 *partP, VolumeId singleVolumeNumber) { char *name, *tdir; - char inodeListPath[256]; + char *inodeListPath = NULL; FD_t inodeFile = INVALID_FD; static char tmpDevName[100]; static char wpath[100]; @@ -804,10 +804,16 @@ SalvageFileSys1(struct DiskPartition64 *partP, VolumeId singleVolumeNumber) tdir = (tmpdir ? tmpdir : salvinfo->fileSysPath); #ifdef AFS_NT40_ENV (void)_putenv("TMP="); /* If "TMP" is set, then that overrides tdir. */ - (void)strncpy(inodeListPath, _tempnam(tdir, "salvage.inodes."), 255); + inodeListPath = strdup(_tempnam(tdir, "salvage.inodes.")); + if (inodeListPath == NULL) { + Abort("Error allocating memory for inodeListPath\n"); + } #else - snprintf(inodeListPath, 255, "%s" OS_DIRSEP "salvage.inodes.%s.%d", tdir, name, + code = asprintf(&inodeListPath, "%s" OS_DIRSEP "salvage.inodes.%s.%d", tdir, name, getpid()); + if (code == -1) { + Abort("Error allocating memory for inodeListPath\n"); + } #endif inodeFile = OS_OPEN(inodeListPath, O_RDWR|O_TRUNC|O_CREAT, 0666); @@ -836,11 +842,16 @@ SalvageFileSys1(struct DiskPartition64 *partP, VolumeId singleVolumeNumber) if (GetInodeSummary(salvinfo, inodeFile, singleVolumeNumber) < 0) { OS_CLOSE(inodeFile); + free(inodeListPath); return; } salvinfo->inodeFd = inodeFile; if (salvinfo->inodeFd == INVALID_FD) Abort("Temporary file %s is missing...\n", inodeListPath); + + free(inodeListPath); + inodeListPath = NULL; + OS_SEEK(salvinfo->inodeFd, 0L, SEEK_SET); if (ListInodeOption) { PrintInodeList(salvinfo); -- 1.9.4