git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Subject: [PATCH 3/6] apply: introduce macro and function to init images
Date: Mon, 16 Sep 2024 09:10:06 +0200	[thread overview]
Message-ID: <1b49e39bcdcf43318682d8520e6bc3d1f40bd618.1726470385.git.ps@pks.im> (raw)
In-Reply-To: <cover.1726470385.git.ps@pks.im>

We're about to convert the `struct image` to gain a `struct strbuf`
member, which requires more careful initialization than just memsetting
it to zeros. Introduce the `IMAGE_INIT` macro and `image_init()`
function to prepare for this change.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 apply.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/apply.c b/apply.c
index ac21c21297..76f7777d4c 100644
--- a/apply.c
+++ b/apply.c
@@ -284,11 +284,19 @@ struct image {
 	struct line *line_allocated;
 	struct line *line;
 };
+#define IMAGE_INIT { 0 }
+
+static void image_init(struct image *image)
+{
+	struct image empty = IMAGE_INIT;
+	memcpy(image, &empty, sizeof(*image));
+}
 
 static void image_clear(struct image *image)
 {
 	free(image->buf);
 	free(image->line_allocated);
+	image_init(image);
 }
 
 static uint32_t hash_line(const char *cp, size_t len)
@@ -322,7 +330,7 @@ static void image_prepare(struct image *image, char *buf, size_t len,
 {
 	const char *cp, *ep;
 
-	memset(image, 0, sizeof(*image));
+	image_clear(image);
 	image->buf = buf;
 	image->len = len;
 
@@ -2314,7 +2322,7 @@ static void update_pre_post_images(struct image *preimage,
 {
 	int i, ctx, reduced;
 	char *new_buf, *old_buf, *fixed;
-	struct image fixed_preimage;
+	struct image fixed_preimage = IMAGE_INIT;
 
 	/*
 	 * Update the preimage with whitespace fixes.  Note that we
@@ -2910,11 +2918,9 @@ static int apply_one_fragment(struct apply_state *state,
 	int hunk_linenr = frag->linenr;
 	unsigned long leading, trailing;
 	int pos, applied_pos;
-	struct image preimage;
-	struct image postimage;
+	struct image preimage = IMAGE_INIT;
+	struct image postimage = IMAGE_INIT;
 
-	memset(&preimage, 0, sizeof(preimage));
-	memset(&postimage, 0, sizeof(postimage));
 	oldlines = xmalloc(size);
 	strbuf_init(&newlines, size);
 
@@ -3650,7 +3656,7 @@ static int try_threeway(struct apply_state *state,
 	size_t len;
 	int status;
 	char *img;
-	struct image tmp_image;
+	struct image tmp_image = IMAGE_INIT;
 
 	/* No point falling back to 3-way merge in these cases */
 	if (patch->is_delete ||
@@ -3727,7 +3733,7 @@ static int try_threeway(struct apply_state *state,
 static int apply_data(struct apply_state *state, struct patch *patch,
 		      struct stat *st, const struct cache_entry *ce)
 {
-	struct image image;
+	struct image image = IMAGE_INIT;
 
 	if (load_preimage(state, &image, patch, st, ce) < 0)
 		return -1;
-- 
2.46.0.551.gc5ee8f2d1c.dirty


  parent reply	other threads:[~2024-09-16  7:10 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 ` Patrick Steinhardt [this message]
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
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=1b49e39bcdcf43318682d8520e6bc3d1f40bd618.1726470385.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=git@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).