Thread (1 message) 1 message, 1 author, 2024-09-16

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

From: Junio C Hamano <hidden>
Date: 2024-09-16 19:30:40

Patrick Steinhardt [off-list ref] 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.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help