commit 2a2e593352ba687c161d743f54bf5addc8f06d7d from: Mark Jamsek via: Thomas Adam date: Tue Dec 26 22:44:36 2023 UTC ensure tmp file is closed and fix UB in diff error path As reported by op, we fail to close a tmp file in some diff failure case. Also spotted by op and stsp, set FILE pointer to NULL after fclose(3) failure so we no longer attempt to access the stream or close it again in close_diff_view(). fix plus ok op@ and stsp@ commit - 8a1f36275eaed118cb07b7249c136e3da8d0ef62 commit + 2a2e593352ba687c161d743f54bf5addc8f06d7d blob - ead293d381171fe6a5cefeb397cc88b5de8b9910 blob + de94ddedf322329e50f2e9155daa6a586c650eeb --- tog/tog.c +++ tog/tog.c @@ -5308,7 +5308,7 @@ static const struct got_error * create_diff(struct tog_diff_view_state *s) { const struct got_error *err = NULL; - FILE *f = NULL, *tmp_diff_file = NULL; + FILE *tmp_diff_file = NULL; int obj_type; struct got_diff_line *lines = NULL; struct got_pathlist_head changed_paths; @@ -5322,21 +5322,18 @@ create_diff(struct tog_diff_view_state *s) return got_error_from_errno("malloc"); s->nlines = 0; - f = got_opentemp(); - if (f == NULL) { - err = got_error_from_errno("got_opentemp"); - goto done; + if (s->f && fclose(s->f) == EOF) { + s->f = NULL; + return got_error_from_errno("fclose"); } + + s->f = got_opentemp(); + if (s->f == NULL) + return got_error_from_errno("got_opentemp"); + tmp_diff_file = got_opentemp(); - if (tmp_diff_file == NULL) { - err = got_error_from_errno("got_opentemp"); - goto done; - } - if (s->f && fclose(s->f) == EOF) { - err = got_error_from_errno("fclose"); - goto done; - } - s->f = f; + if (tmp_diff_file == NULL) + return got_error_from_errno("got_opentemp"); if (s->id1) err = got_object_get_type(&obj_type, s->repo, s->id1);