git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 6/6] apply: refactor `struct image` to use a `struct strbuf`

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 16, 2024, 19:30 UTC
Message-ID
<xmqq8qvrmm5e.fsf@gitster.g>
In-Reply-To
<6cf45daf84af68f74d87a5b2ae7102d4cd8ceb57.1726470385.git.ps@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 6 quoted lines
> Refactor the code to use a `struct strbuf` instead, addressing all of
> the above. Like this we can easily perform in-place updates in all
> situations, the logic to perform those updates becomes way simpler and
> the lifetime of the buffer becomes a ton easier to track.
>
> This refactoring also plugs some leaking buffers as a side effect.
Nice.

In short, the leaks were in the original code where it was making direct assignment to image->buf, and we now use strbuf_attach(), which releases the current buffer before replacing it with a piece of memory allocated outside the control of strbuf API?

Show 5 quoted lines
>  static void image_remove_last_line(struct image *img)
>  {
> -	img->len -= img->line[--img->line_nr].len;
> +	strbuf_setlen(&img->buf, img->buf.len - img->line[--img->line_nr].len);
>  }

I feel that this, while technically is faithful to the original, got a bit too complex to understand what is going on. Perhaps split it into two statements with an intermediate variable? I dunno.

Thanks.
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 11 of 20 in “apply: fix leaking buffer of `struct image`”
  1. 0/6 apply: fix leaking buffer of `struct image`Patrick Steinhardt, Sep 16, 2024
  2. 1/6 apply: reorder functions to move image-related things togetherPatrick Steinhardt, Sep 16, 2024
  3. 2/6 apply: rename functions operating on `struct image`Patrick Steinhardt, Sep 16, 2024
  4. 3/6 apply: introduce macro and function to init imagesPatrick Steinhardt, Sep 16, 2024
  5. 4/6 apply: refactor code to drop `line_allocated`Patrick Steinhardt, Sep 16, 2024
  6. Junio C HamanoSep 16, 2024
  7. Patrick SteinhardtSep 17, 2024
  8. Junio C HamanoSep 16, 2024
  9. 5/6 apply: rename members that track line count and allocation lengthPatrick Steinhardt, Sep 16, 2024
  10. 6/6 apply: refactor `struct image` to use a `struct strbuf`Patrick Steinhardt, Sep 16, 2024
  11. Junio C HamanoSep 16, 2024
  12. 0/6 apply: fix leaking buffer of `struct image`Patrick Steinhardt, Sep 17, 2024
  13. 1/6 apply: reorder functions to move image-related things togetherPatrick Steinhardt, Sep 17, 2024
  14. 2/6 apply: rename functions operating on `struct image`Patrick Steinhardt, Sep 17, 2024
  15. 3/6 apply: introduce macro and function to init imagesPatrick Steinhardt, Sep 17, 2024
  16. 4/6 apply: refactor code to drop `line_allocated`Patrick Steinhardt, Sep 17, 2024
  17. 5/6 apply: rename members that track line count and allocation lengthPatrick Steinhardt, Sep 17, 2024
  18. 6/6 apply: refactor `struct image` to use a `struct strbuf`Patrick Steinhardt, Sep 17, 2024
  19. Patrick SteinhardtSep 17, 2024
  20. Junio C HamanoSep 17, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.