vol: Fix format-truncation warning with gcc-10.1

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 <buildbot@rampaginggeek.com>
Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
This commit is contained in:
Cheyenne Wills 2020-05-11 14:06:19 -06:00 committed by Benjamin Kaduk
parent c81579dc7b
commit d73680c5f7

View File

@ -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);