From fa751f9bef390a0e6de3687f61204d7a2df73d96 Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Wed, 27 Dec 2023 01:53:56 -0500 Subject: [PATCH] config.c: Avoid leaking file handle if file is 0 bytes (#12828) If fopen() is successful and redis_fstat determines that the file is 0 bytes, the file handle stored in fp will leak. This change closes the filehandle stored in fp if the file is 0 bytes. Second attempt at fixing Coverity 390029 This is a follow-up to #12796 --- src/config.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/config.c b/src/config.c index b152a8fa5..b4e14eaf1 100644 --- a/src/config.c +++ b/src/config.c @@ -1128,7 +1128,14 @@ struct rewriteConfigState *rewriteConfigReadOldFile(char *path) { int linenum = -1; struct rewriteConfigState *state = rewriteConfigCreateState(); - if (fp == NULL || sb.st_size == 0) return state; + if (fp == NULL) { + return state; + } + + if (sb.st_size == 0) { + fclose(fp); + return state; + } /* Load the file content */ sds config = sdsnewlen(SDS_NOINIT,sb.st_size);