commit 65d7451409bf603e3c302b7d0ce999f7ce542508 from: Mark Jamsek date: Thu Dec 21 08:46:59 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 - d4b583a2c1d6bd87e203e605ec2b8e9353807b4e commit + 65d7451409bf603e3c302b7d0ce999f7ce542508 blob - be366266e526e26c8734719d20d29ed05d463b06 blob + 1d2d2883f6f459ee2e81334d5953c0559d5de275 --- tog/tog.c +++ tog/tog.c @@ -5304,7 +5304,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; @@ -5318,21 +5318,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);