commit 61dc16bb64b935a7b13342b8bc47db282d10e676 from: Omar Polo via: Thomas Adam date: Wed May 17 15:49:55 2023 UTC tog: fix segfault in draw_commit build_refs_str() can succeed returning a NULL string if a commit has some refs pointing to it but that were all filtered out, resulting in a NULL-deref. ok stsp@ commit - ba77389f9f3a659ebd77c8a4b4974c645e0530fd commit + 61dc16bb64b935a7b13342b8bc47db282d10e676 blob - 572a730d092394c09f1ea83b262cf72884cc2dac blob + ff292e9cadc7e6213f828308478d7827b79cb69a --- tog/tog.c +++ tog/tog.c @@ -2405,6 +2405,7 @@ draw_commit(struct tog_view *view, struct got_commit_o struct tog_log_view_state *s = &view->state.log; const struct got_error *err = NULL; char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */ + char *refs_str = NULL; char *logmsg0 = NULL, *logmsg = NULL; char *author = NULL; wchar_t *wlogmsg = NULL, *wauthor = NULL; @@ -2499,13 +2500,13 @@ draw_commit(struct tog_view *view, struct got_commit_o /* Prepend reference labels to log message if possible .*/ refs = got_reflist_object_id_map_lookup(tog_refs_idmap, id); - if (refs) { - char *refs_str, *newlogmsg; - wchar_t *ws; - + if (refs) err = build_refs_str(&refs_str, refs, id, s->repo); - if (err) - goto done; + if (err) + goto done; + if (refs_str) { + char *newlogmsg; + wchar_t *ws; /* * The length of this wide-char sub-string will be @@ -2520,10 +2521,8 @@ draw_commit(struct tog_view *view, struct got_commit_o if (asprintf(&newlogmsg, "[%s] %s", refs_str, logmsg) == -1) { err = got_error_from_errno("asprintf"); - free(refs_str); goto done; } - free(refs_str); free(logmsg0); logmsg0 = newlogmsg; @@ -2558,6 +2557,7 @@ draw_commit(struct tog_view *view, struct got_commit_o done: free(logmsg0); free(wlogmsg); + free(refs_str); free(author); free(wauthor); free(line);