Skip to content

Commit fa1912c

Browse files
rscharfegitster
authored andcommitted
server-info: avoid calling fclose(3) twice in update_info_file()
If an error occurs when or after closing the stream we call fclose(3) again in the error handler. The second call can exhibit undefined behavior, so make sure to call fclose(3) at most once. Also avoid calling close(2) after fd has been successfully associated with the stream, as fclose(3) has become responsible for doing that beyond this point. Found with Cppcheck. Signed-off-by: Rene Scharfe <[email protected]> Reviewed-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent be686f0 commit fa1912c

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

server-info.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ static int update_info_file(char *path, int (*generate)(FILE *))
1414
char *tmp = mkpathdup("%s_XXXXXX", path);
1515
int ret = -1;
1616
int fd = -1;
17-
FILE *fp = NULL;
17+
FILE *fp = NULL, *to_close;
1818

1919
safe_create_leading_directories(path);
2020
fd = git_mkstemp_mode(tmp, 0666);
2121
if (fd < 0)
2222
goto out;
23-
fp = fdopen(fd, "w");
23+
to_close = fp = fdopen(fd, "w");
2424
if (!fp)
2525
goto out;
26+
fd = -1;
2627
ret = generate(fp);
2728
if (ret)
2829
goto out;
29-
if (fclose(fp))
30+
fp = NULL;
31+
if (fclose(to_close))
3032
goto out;
3133
if (adjust_shared_perm(tmp) < 0)
3234
goto out;

0 commit comments

Comments
 (0)