plug memory leaks in error cases of gw_diff()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index 6eb9c20..de158a3 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -370,18 +370,23 @@ gw_diff(struct gw_trans *gw_trans)
error = gw_apply_unveil(gw_trans->gw_dir->path, NULL);
if (error)
- return error;
+ goto done;
error = gw_get_header(gw_trans, header, 1);
if (error)
- return error;
+ goto done;
diff_html = gw_get_diff(gw_trans, header);
- if (diff_html == NULL)
+ if (diff_html == NULL) {
diff_html = strdup("");
+ if (diff_html == NULL) {
+ error = got_error_from_errno("strdup");
+ goto done;
+ }
+ }
- if ((asprintf(&diff_html_disp, diff_header,
+ if (asprintf(&diff_html_disp, diff_header,
gw_gen_diff_header(header->parent_id, header->commit_id),
gw_gen_commit_header(header->commit_id, header->refs_str),
gw_gen_tree_header(header->tree_id),
@@ -389,16 +394,22 @@ gw_diff(struct gw_trans *gw_trans)
gw_gen_committer_header(header->committer),
gw_gen_age_header(gw_get_time_str(header->committer_time, TM_LONG)),
gw_gen_commit_msg_header(gw_html_escape(header->commit_msg)),
- diff_html)) == -1)
- return got_error_from_errno("asprintf");
+ diff_html) == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
- if ((asprintf(&diff, diff_wrapper, diff_html_disp)) == -1)
- return got_error_from_errno("asprintf");
+ if (asprintf(&diff, diff_wrapper, diff_html_disp) == -1) {
+ error = got_error_from_errno("asprintf");
+ goto done;
+ }
kerr = khttp_puts(gw_trans->gw_req, diff);
if (kerr != KCGI_OK)
error = gw_kcgi_error(kerr);
- got_ref_list_free(&header->refs);
+done:
+ if (header)
+ got_ref_list_free(&header->refs);
gw_free_headers(header);
free(diff_html_disp);
free(diff_html);