Skip to content

Commit 7db28d0

Browse files
pks-tgitster
authored andcommitted
apply: introduce macro and function to init images
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2231903 commit 7db28d0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

apply.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,19 @@ struct image {
284284
struct line *line_allocated;
285285
struct line *line;
286286
};
287+
#define IMAGE_INIT { 0 }
288+
289+
static void image_init(struct image *image)
290+
{
291+
struct image empty = IMAGE_INIT;
292+
memcpy(image, &empty, sizeof(*image));
293+
}
287294

288295
static void image_clear(struct image *image)
289296
{
290297
free(image->buf);
291298
free(image->line_allocated);
299+
image_init(image);
292300
}
293301

294302
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,
322330
{
323331
const char *cp, *ep;
324332

325-
memset(image, 0, sizeof(*image));
333+
image_clear(image);
326334
image->buf = buf;
327335
image->len = len;
328336

@@ -2314,7 +2322,7 @@ static void update_pre_post_images(struct image *preimage,
23142322
{
23152323
int i, ctx, reduced;
23162324
char *new_buf, *old_buf, *fixed;
2317-
struct image fixed_preimage;
2325+
struct image fixed_preimage = IMAGE_INIT;
23182326

23192327
/*
23202328
* Update the preimage with whitespace fixes. Note that we
@@ -2910,11 +2918,9 @@ static int apply_one_fragment(struct apply_state *state,
29102918
int hunk_linenr = frag->linenr;
29112919
unsigned long leading, trailing;
29122920
int pos, applied_pos;
2913-
struct image preimage;
2914-
struct image postimage;
2921+
struct image preimage = IMAGE_INIT;
2922+
struct image postimage = IMAGE_INIT;
29152923

2916-
memset(&preimage, 0, sizeof(preimage));
2917-
memset(&postimage, 0, sizeof(postimage));
29182924
oldlines = xmalloc(size);
29192925
strbuf_init(&newlines, size);
29202926

@@ -3650,7 +3656,7 @@ static int try_threeway(struct apply_state *state,
36503656
size_t len;
36513657
int status;
36523658
char *img;
3653-
struct image tmp_image;
3659+
struct image tmp_image = IMAGE_INIT;
36543660

36553661
/* No point falling back to 3-way merge in these cases */
36563662
if (patch->is_delete ||
@@ -3727,7 +3733,7 @@ static int try_threeway(struct apply_state *state,
37273733
static int apply_data(struct apply_state *state, struct patch *patch,
37283734
struct stat *st, const struct cache_entry *ce)
37293735
{
3730-
struct image image;
3736+
struct image image = IMAGE_INIT;
37313737

37323738
if (load_preimage(state, &image, patch, st, ce) < 0)
37333739
return -1;

0 commit comments

Comments
 (0)