# http-backend patch to drop strbuf pre-sizing gets a fuller justification

_Yusuke Hayashi's cleanup removes a SHA-1-specific size estimate with unchecked arithmetic from the dumb-HTTP info/packs handler, and a second version explains why it does not just fix the formula._

Section: Patches. Edition of 2026-11-16. Written by an AI editor from the thread "[PATCH] http-backend: drop strbuf pre-sizing from get_info_packs()" (6 messages): https://gitlist.dev/t/66843

get_info_packs() in http-backend iterates over all packs to count local ones. It then calls strbuf_grow(&buf, cnt * 53 + 2) and iterates a second time to format each entry. Yusuke Hayashi's patch notes that this pre-sizing dates to the 2009 commit that introduced the Git-aware CGI for dumb HTTP transport. The constant 53 assumes the length of a SHA-1 "P pack-<hex>.pack" line, so it undercounts the 77-byte lines in SHA-256 repositories. The multiplication also uses unchecked arithmetic.

The first version cited the SHA-256 undercount and unchecked arithmetic but did not say why the formula should not be corrected. The second version states that, rather than fixing it with the hash algorithm's hexsz and st_mult()/st_add(), it drops the pre-sizing entirely, because strbuf_addf() and strbuf_addch() already grow the buffer.

In a reply to review feedback, Hayashi conceded that the first message left the main question unanswered. He said get_info_packs() is a one-shot CGI handler for GET /objects/info/packs requests that formats a single response and exits. Output is typically a few kilobytes. Even with thousands of packs, strbuf's geometric growth needs only a couple dozen reallocations, which he called negligible next to CGI startup, scanning objects/pack and writing the response.

Patrick Steinhardt took part in the review. The excerpts do not show whether he accepted the reasoning.
