libafscp: Fix problems found by static analysis

Several static analysis tools have identified various problems:
 - missing checks to ensure *alloc was successful (infer)
 - memory leak                                    (infer)
 - possible null pointer dereference              (cppcheck)

To resolve the above problems:
 - add checks to ensure *alloc was successful before using the memory
 - free memory before returning
 - move pointer based assignment to after a check for NULL

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

afscp_dir.c:606: Memory dynamically allocated by `malloc`, indirectly
    via call to `afscp_DupFid()` on line 602 is not freed after the last
    access at line 606, column 9

afscp_dirops.c:44: Either the condition 'dir==NULL' is redundant or
    there is possible null pointer dereference: dir. Null pointer
    dereference

Change-Id: I17c8b7b45d581f7c84d8a2930de73783e2ae18be
Reviewed-on: https://gerrit.openafs.org/14712
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Michael Meffie <mmeffie@sinenomine.net>
Reviewed-by: Andrew Deason <adeason@sinenomine.net>
This commit is contained in:
Cheyenne Wills 2024-12-20 15:29:59 -07:00 committed by Andrew Deason
parent 60314cbe8e
commit 327b2dd0db
2 changed files with 8 additions and 1 deletions

View File

@ -509,6 +509,11 @@ afscp_HandleLink(struct afscp_venusfid *in,
return in;
}
linkbuf = malloc(s->Length + 1);
if (linkbuf == NULL) {
afscp_errno = ENOMEM;
free(in);
return NULL;
}
code = afscp_PRead(in, linkbuf, s->Length, 0);
if (code < 0) {
free(linkbuf);
@ -599,6 +604,7 @@ _ResolvePath(const struct afscp_venusfid *start, fidstack infids,
if (fids == NULL)
fids = fidstack_alloc();
if (fids == NULL) {
free(cwd);
return NULL;
}

View File

@ -41,7 +41,7 @@ afscp_CreateFile(const struct afscp_venusfid *dir, char *name,
struct AFSStoreStatus *sst, struct afscp_venusfid **ret)
{
int code, i, j;
struct AFSFid df = dir->fid;
struct AFSFid df;
struct afscp_volume *vol;
struct AFSFetchStatus dfst, fst;
struct AFSVolSync vs;
@ -56,6 +56,7 @@ afscp_CreateFile(const struct afscp_venusfid *dir, char *name,
"afscp_CreateFile called with NULL args, cannot continue\n");
return -1;
}
df = dir->fid;
vol = afscp_VolumeById(dir->cell, dir->fid.Volume);
if (vol == NULL) {
afscp_errno = ENOENT;