Skip to content

Commit 31ce03b

Browse files
committed
src/cachunk: use _cleanup_ in two places to close fds
1 parent 897ee61 commit 31ce03b

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/cachunk.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ int ca_chunk_file_load(
628628
ReallocBuffer *buffer,
629629
CaChunkCompression *ret_effective_compression) {
630630

631-
int fd, r;
631+
_cleanup_(safe_closep) int fd = -1;
632+
int r;
632633

633634
if (chunk_fd < 0 && chunk_fd != AT_FDCWD)
634635
return -EINVAL;
@@ -658,22 +659,25 @@ int ca_chunk_file_load(
658659
r = ca_load_and_decompress_fd(fd, buffer);
659660
else
660661
r = ca_load_fd(fd, buffer);
662+
if (r < 0)
663+
return r;
661664

662-
if (r >= 0 && ret_effective_compression)
665+
if (ret_effective_compression)
663666
*ret_effective_compression = desired_compression == CA_CHUNK_AS_IS ? CA_CHUNK_COMPRESSED : desired_compression;
664667

665668
} else {
666669
if (desired_compression == CA_CHUNK_COMPRESSED)
667670
r = ca_load_and_compress_fd(fd, compression_type, buffer);
668671
else
669672
r = ca_load_fd(fd, buffer);
673+
if (r < 0)
674+
return r;
670675

671-
if (r >= 0 && ret_effective_compression)
676+
if (ret_effective_compression)
672677
*ret_effective_compression = desired_compression == CA_CHUNK_AS_IS ? CA_CHUNK_UNCOMPRESSED : desired_compression;
673678
}
674679

675-
safe_close(fd);
676-
return r;
680+
return 0;
677681
}
678682

679683
int ca_chunk_file_save(
@@ -687,7 +691,7 @@ int ca_chunk_file_save(
687691
uint64_t l) {
688692

689693
_cleanup_free_ char *suffix = NULL;
690-
int fd, r;
694+
int r;
691695

692696
if (chunk_fd < 0 && chunk_fd != AT_FDCWD)
693697
return -EINVAL;
@@ -717,7 +721,8 @@ int ca_chunk_file_save(
717721
if (asprintf(&suffix, ".%" PRIx64 ".tmp", random_u64()) < 0)
718722
return -ENOMEM;
719723

720-
fd = ca_chunk_file_open(chunk_fd, prefix, chunkid, suffix, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC);
724+
_cleanup_(safe_closep) int fd =
725+
ca_chunk_file_open(chunk_fd, prefix, chunkid, suffix, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY|O_CLOEXEC);
721726
if (fd < 0)
722727
return fd;
723728

@@ -732,7 +737,6 @@ int ca_chunk_file_save(
732737
assert(desired_compression == CA_CHUNK_UNCOMPRESSED);
733738
r = ca_save_and_decompress_fd(fd, p, l);
734739
}
735-
safe_close(fd);
736740
if (r < 0)
737741
goto fail;
738742

0 commit comments

Comments
 (0)