All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 6/6] apply: refactor `struct image` to use a `struct strbuf`
Date: Mon, 16 Sep 2024 12:30:37 -0700	[thread overview]
Message-ID: <xmqq8qvrmm5e.fsf@gitster.g> (raw)
In-Reply-To: <6cf45daf84af68f74d87a5b2ae7102d4cd8ceb57.1726470385.git.ps@pks.im> (Patrick Steinhardt's message of "Mon, 16 Sep 2024 09:10:16 +0200")

Patrick Steinhardt <ps@pks.im> writes:

> 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?

>  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.

  reply	other threads:[~2024-09-16 19:30 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-16  7:09 [PATCH 0/6] apply: fix leaking buffer of `struct image` Patrick Steinhardt
2024-09-16  7:10 ` [PATCH 1/6] apply: reorder functions to move image-related things together Patrick Steinhardt
2024-09-16  7:10 ` [PATCH 2/6] apply: rename functions operating on `struct image` Patrick Steinhardt
2024-09-16  7:10 ` [PATCH 3/6] apply: introduce macro and function to init images Patrick Steinhardt
2024-09-16  7:10 ` [PATCH 4/6] apply: refactor code to drop `line_allocated` Patrick Steinhardt
2024-09-16 18:56   ` Junio C Hamano
2024-09-17  9:50     ` Patrick Steinhardt
2024-09-16 21:40   ` Junio C Hamano
2024-09-16  7:10 ` [PATCH 5/6] apply: rename members that track line count and allocation length Patrick Steinhardt
2024-09-16  7:10 ` [PATCH 6/6] apply: refactor `struct image` to use a `struct strbuf` Patrick Steinhardt
2024-09-16 19:30   ` Junio C Hamano [this message]
2024-09-17 10:07 ` [PATCH v2 0/6] apply: fix leaking buffer of `struct image` Patrick Steinhardt
2024-09-17 10:07   ` [PATCH v2 1/6] apply: reorder functions to move image-related things together Patrick Steinhardt
2024-09-17 10:07   ` [PATCH v2 2/6] apply: rename functions operating on `struct image` Patrick Steinhardt
2024-09-17 10:08   ` [PATCH v2 3/6] apply: introduce macro and function to init images Patrick Steinhardt
2024-09-17 10:08   ` [PATCH v2 4/6] apply: refactor code to drop `line_allocated` Patrick Steinhardt
2024-09-17 10:08   ` [PATCH v2 5/6] apply: rename members that track line count and allocation length Patrick Steinhardt
2024-09-17 10:08   ` [PATCH v2 6/6] apply: refactor `struct image` to use a `struct strbuf` Patrick Steinhardt
2024-09-17 10:08   ` [PATCH v2 0/6] apply: fix leaking buffer of `struct image` Patrick Steinhardt
2024-09-17 20:57   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq8qvrmm5e.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.