Skip to content

Commit c9faf53

Browse files
remote-curl: fix memory leak in discover_refs() error paths
When discover_refs() encounters an HTTP error, it calls show_http_message() which may re-encode the buffer via strbuf_reencode(), allocating new memory. The function then immediately calls die() to exit, bypassing the cleanup code at the end of the function that releases all allocated strbufs and string_list resources. This results in a memory leak detected by LeakSanitizer in the re-encoded buffer allocated by reencode_string_iconv(). Fix this by releasing all allocated resources (refs_url, type, charset, effective_url, buffer, protocol_header strbufs, and extra_headers string_list) before calling die() in each error case (HTTP_MISSING_TARGET, HTTP_NOAUTH, HTTP_NOMATCHPUBLICKEY, HTTP_RATE_LIMITED, and the default case). Signed-off-by: Vaidas Pilkauskas <[email protected]>
1 parent 2352f80 commit c9faf53

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

remote-curl.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,22 +519,57 @@ static struct discovery *discover_refs(const char *service, int for_push)
519519
break;
520520
case HTTP_MISSING_TARGET:
521521
show_http_message(&type, &charset, &buffer);
522+
strbuf_release(&refs_url);
523+
strbuf_release(&type);
524+
strbuf_release(&charset);
525+
strbuf_release(&effective_url);
526+
strbuf_release(&buffer);
527+
strbuf_release(&protocol_header);
528+
string_list_clear(&extra_headers, 0);
522529
die(_("repository '%s' not found"),
523530
transport_anonymize_url(url.buf));
524531
case HTTP_NOAUTH:
525532
show_http_message(&type, &charset, &buffer);
533+
strbuf_release(&refs_url);
534+
strbuf_release(&type);
535+
strbuf_release(&charset);
536+
strbuf_release(&effective_url);
537+
strbuf_release(&buffer);
538+
strbuf_release(&protocol_header);
539+
string_list_clear(&extra_headers, 0);
526540
die(_("Authentication failed for '%s'"),
527541
transport_anonymize_url(url.buf));
528542
case HTTP_NOMATCHPUBLICKEY:
529543
show_http_message(&type, &charset, &buffer);
544+
strbuf_release(&refs_url);
545+
strbuf_release(&type);
546+
strbuf_release(&charset);
547+
strbuf_release(&effective_url);
548+
strbuf_release(&buffer);
549+
strbuf_release(&protocol_header);
550+
string_list_clear(&extra_headers, 0);
530551
die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
531552
transport_anonymize_url(url.buf), curl_errorstr);
532553
case HTTP_RATE_LIMITED:
533554
show_http_message(&type, &charset, &buffer);
555+
strbuf_release(&refs_url);
556+
strbuf_release(&type);
557+
strbuf_release(&charset);
558+
strbuf_release(&effective_url);
559+
strbuf_release(&buffer);
560+
strbuf_release(&protocol_header);
561+
string_list_clear(&extra_headers, 0);
534562
die(_("rate limited by '%s', please try again later"),
535563
transport_anonymize_url(url.buf));
536564
default:
537565
show_http_message(&type, &charset, &buffer);
566+
strbuf_release(&refs_url);
567+
strbuf_release(&type);
568+
strbuf_release(&charset);
569+
strbuf_release(&effective_url);
570+
strbuf_release(&buffer);
571+
strbuf_release(&protocol_header);
572+
string_list_clear(&extra_headers, 0);
538573
die(_("unable to access '%s': %s"),
539574
transport_anonymize_url(url.buf), curl_errorstr);
540575
}

0 commit comments

Comments
 (0)