]> granicus.if.org Git - git/commitdiff
server-info: avoid calling fclose(3) twice in update_info_file()
authorRené Scharfe <l.s.r@web.de>
Sun, 16 Apr 2017 16:55:58 +0000 (18:55 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 18 Apr 2017 00:37:28 +0000 (17:37 -0700)
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 <l.s.r@web.de>
Reviewed-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
server-info.c

index 7bc4e75d22ce099ca68aa1fac50c9123546ec5b7..f6c1a3dfb04bff5310a1b930b03478d88c9c2904 100644 (file)
@@ -14,19 +14,21 @@ static int update_info_file(char *path, int (*generate)(FILE *))
        char *tmp = mkpathdup("%s_XXXXXX", path);
        int ret = -1;
        int fd = -1;
-       FILE *fp = NULL;
+       FILE *fp = NULL, *to_close;
 
        safe_create_leading_directories(path);
        fd = git_mkstemp_mode(tmp, 0666);
        if (fd < 0)
                goto out;
-       fp = fdopen(fd, "w");
+       to_close = fp = fdopen(fd, "w");
        if (!fp)
                goto out;
+       fd = -1;
        ret = generate(fp);
        if (ret)
                goto out;
-       if (fclose(fp))
+       fp = NULL;
+       if (fclose(to_close))
                goto out;
        if (adjust_shared_perm(tmp) < 0)
                goto out;