Skip to content

Commit

Permalink
Merge pull request #181 from cgwalters/clang-analyzer-fixes-1
Browse files Browse the repository at this point in the history
Use cleanup_fdp to quiet clang-analyzer
  • Loading branch information
alexlarsson committed Aug 31, 2023
2 parents 597a766 + 6da0b95 commit a6e2770
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 4 additions & 0 deletions libcomposefs/lcfs-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ static inline void cleanup_freep(void *p)
free(*pp);
}

// A wrapper around close() that takes a pointer to a file descriptor (integer):
// - Never returns an error (and preserves errno)
// - Sets the value to -1 after closing to make cleanup idempotent
static inline void cleanup_fdp(int *fdp)
{
PROTECT_ERRNO;
Expand All @@ -55,6 +58,7 @@ static inline void cleanup_fdp(int *fdp)
fd = *fdp;
if (fd != -1)
(void)close(fd);
*fdp = -1;
}

#define cleanup_free __attribute__((cleanup(cleanup_freep)))
Expand Down
6 changes: 2 additions & 4 deletions tools/mkcomposefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ static int copy_file_with_dirs_if_needed(const char *src, const char *dst_base,
return res;
}
}
close(sfd);
sfd = -1;
cleanup_fdp(&sfd);

/* Make sure file is readable by all */
res = fchmod(dfd, 0644);
Expand All @@ -258,8 +257,7 @@ static int copy_file_with_dirs_if_needed(const char *src, const char *dst_base,
if (res < 0) {
return res;
}
close(dfd);
dfd = -1;
cleanup_fdp(&dfd);

if (try_enable_fsverity) {
/* Try to enable fsverity */
Expand Down

0 comments on commit a6e2770

Please sign in to comment.