afsd: Fix problems found by static analysis

Several static analysis tools have identified various problems:
 - files left open from early returns/exits       (infer)
 - missing checks to ensure *alloc was successful (infer)
 - memory leaks due to not freeing storage before
   a return                                       (infer)
 - unused variables                               (cppcheck)

To resolve the above problems:
 - close files before returning/exiting
 - add checks to ensure *alloc was successful before using the memory
 - free storage before returning
 - remove unused variables

This commit is a reorganization of commits submitted by Pat Riehecky,
who ran the static analysis tools and developed the fixes.

Change-Id: If45d64c370ad68d1e844b6aa5881f9dbd75300c9
Reviewed-on: https://gerrit.openafs.org/14671
Reviewed-by: Andrew Deason <adeason@sinenomine.net>
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Michael Meffie <mmeffie@sinenomine.net>
Reviewed-by: Mark Vitale <mvitale@sinenomine.net>
Reviewed-by: Marcio Brito Barbosa <mbarbosa@sinenomine.net>
This commit is contained in:
Cheyenne Wills 2024-10-18 12:14:35 -06:00 committed by Andrew Deason
parent 278fc9d1c3
commit cacc1bb9d4
3 changed files with 13 additions and 5 deletions

View File

@ -896,6 +896,7 @@ CreateCacheFile(char *fname, struct stat *statp)
if (statp != NULL) {
closeResult = fstat(cfd, statp);
if (closeResult) {
close(cfd);
printf
("%s: Can't stat newly-created AFS cache file '%s' (code %d)\n",
rn, fname, errno);
@ -1519,6 +1520,7 @@ BkgHandler(void)
char dstName[256];
uspc = calloc(1, sizeof(struct afs_uspc_param));
opr_Assert(uspc != NULL);
memset(srcName, 0, sizeof(srcName));
memset(dstName, 0, sizeof(dstName));
@ -1538,7 +1540,7 @@ BkgHandler(void)
* like a AFS_USPC_SHUTDOWN, in case we're running with an
* older kernel module.
*/
return;
goto done;
}
sleep(1);
@ -1549,7 +1551,7 @@ BkgHandler(void)
switch (uspc->reqtype) {
case AFS_USPC_SHUTDOWN:
/* Client is shutting down */
return;
goto done;
case AFS_USPC_NOOP:
/* noop */
@ -1638,6 +1640,9 @@ BkgHandler(void)
break;
}
}
done:
free(uspc);
}
#endif
@ -2152,9 +2157,6 @@ CheckOptions(struct cmd_syndesc *as)
#ifdef AFS_SUN5_ENV
struct stat st;
#endif
#ifdef AFS_SGI_ENV
struct sched_param sp;
#endif
#ifdef AFS_SGI_VNODE_GLUE
if (afs_init_kernel_config(-1) < 0) {
@ -3260,6 +3262,7 @@ fork_syscall_impl(int rx, int wait, const char *rn, int syscall, va_list ap)
struct afsd_syscall_args *args;
args = malloc(sizeof(*args));
opr_Assert(args != NULL);
afsd_syscall_populate(args, syscall, ap);
args->rxpri = rx;
args->rn = rn;

View File

@ -401,6 +401,9 @@ fuafsd_write(const char *path, const char *abuf, size_t len, off_t offset,
{
int fd, code;
char *buf = malloc(len);
if (buf == NULL) {
return -ENOMEM;
}
fd = fi->fh;
memcpy(buf, abuf, len);

View File

@ -491,6 +491,7 @@ HandleMTab(char *cacheMountDir)
}
dir = strdup(cacheMountDir);
opr_Assert(dir != NULL);
/* trim trailing slashes; don't look at dir[0] in case we are somehow
* just "/" */
@ -606,6 +607,7 @@ afsd_mount_afs(const char *rn, const char *cacheMountDir)
}
mountDir = strdup(cacheMountDir);
opr_Assert(mountDir != NULL);
HandleMTab(mountDir);
free(mountDir);
}