diff mbox series

[v2,1/3] merge-base, xdiff: zero out xpparam_t structures

Message ID 20201012091751.19594-2-michal@isc.org (mailing list archive)
State New, archived
Headers show
Series [v2,1/3] merge-base, xdiff: zero out xpparam_t structures | expand

Commit Message

Michał Kępień Oct. 12, 2020, 9:17 a.m. UTC
xpparam_t structures are usually zero-initialized before their specific
fields are assigned to, but there are three locations in the tree where
that does not happen.  Add the missing memset() calls in order to make
initialization of xpparam_t structures consistent tree-wide and to
prevent stack garbage from being used as field values.

Signed-off-by: Michał Kępień <michal@isc.org>
---
 builtin/merge-tree.c | 1 +
 xdiff/xhistogram.c   | 2 ++
 xdiff/xpatience.c    | 2 ++
 3 files changed, 5 insertions(+)

Comments

Johannes Schindelin Oct. 12, 2020, 11:14 a.m. UTC | #1
Hi Michał,

On Mon, 12 Oct 2020, Michał Kępień wrote:


> diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
> index c7b35a9667..e694bfd9e3 100644
> --- a/xdiff/xhistogram.c
> +++ b/xdiff/xhistogram.c
> @@ -235,6 +235,8 @@ static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env,
>  		int line1, int count1, int line2, int count2)
>  {
>  	xpparam_t xpparam;
> +
> +	memset(&xpparam, 0, sizeof(xpparam));

A slightly more elegant way to do this would be

	xpparam_t xpparam = { 0l };

Or even

	xpparam_t xpparam = XPPARAM_T_INIT;

with

	#define XPPARAM_T_INIT { 0l, NULL, 0 }

in `xdiff/xdiff.h`.

Thanks,
Dscho

>  	xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
>
>  	return xdl_fall_back_diff(env, &xpparam,
> diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c
> index 3c5601b602..20699a6f60 100644
> --- a/xdiff/xpatience.c
> +++ b/xdiff/xpatience.c
> @@ -318,6 +318,8 @@ static int fall_back_to_classic_diff(struct hashmap *map,
>  		int line1, int count1, int line2, int count2)
>  {
>  	xpparam_t xpp;
> +
> +	memset(&xpp, 0, sizeof(xpp));
>  	xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
>
>  	return xdl_fall_back_diff(map->env, &xpp,
> --
> 2.28.0
>
>
>
Junio C Hamano Oct. 12, 2020, 5:09 p.m. UTC | #2
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>>  	xpparam_t xpparam;
>> +
>> +	memset(&xpparam, 0, sizeof(xpparam));
>
> A slightly more elegant way to do this would be
>
> 	xpparam_t xpparam = { 0l };
>
> Or even
>
> 	xpparam_t xpparam = XPPARAM_T_INIT;
>
> with
>
> 	#define XPPARAM_T_INIT { 0l, NULL, 0 }
>
> in `xdiff/xdiff.h`.

Let's not suggest any of the above.

I think we had a recent thread [*1*] on which we agreed that "{ 0 }"
is the way to spell "a naturally zero" initialization like this one,
if we were to spell it out.

TBH, I'd say that memset() is also OK as-is in this codebase as an
established way (git grep 'memset([^,]*, 0, sizeof' gives too many
hits).

Thanks.


[Reference]

*1* https://lore.kernel.org/git/xmqq4knca7c6.fsf@gitster.c.googlers.com/
Junio C Hamano Oct. 12, 2020, 7:52 p.m. UTC | #3
Michał Kępień <michal@isc.org> writes:

> diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
> index e72714a5a8..de8520778d 100644
> --- a/builtin/merge-tree.c
> +++ b/builtin/merge-tree.c
> @@ -109,6 +109,7 @@ static void show_diff(struct merge_list *entry)
>  	xdemitconf_t xecfg;
>  	xdemitcb_t ecb;
>  
> +	memset(&xpp, 0, sizeof(xpp));
>  	xpp.flags = 0;
>  	memset(&xecfg, 0, sizeof(xecfg));
>  	xecfg.ctxlen = 3;

The existing "xpp.flags=0" can then go away, and as Dscho hinted,

	xpparam_t xpp = {
		.flags = 0,
	};

may also be a valid way to write this.  What it says is that we want
the entirety of xpp to be reasonably initialized, but we do care
that its .flags field to be exactly zero.

> diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
> index c7b35a9667..e694bfd9e3 100644
> --- a/xdiff/xhistogram.c
> +++ b/xdiff/xhistogram.c
> @@ -235,6 +235,8 @@ static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env,
>  		int line1, int count1, int line2, int count2)
>  {
>  	xpparam_t xpparam;
> +
> +	memset(&xpparam, 0, sizeof(xpparam));
>  	xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;

Likewise.  Giving an initializer to the local variable def, e.g.

	xpparam_t xpparam = {
		.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK,
	};

would allow us to lose one line.

>  	return xdl_fall_back_diff(env, &xpparam,
> diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c
> index 3c5601b602..20699a6f60 100644
> --- a/xdiff/xpatience.c
> +++ b/xdiff/xpatience.c
> @@ -318,6 +318,8 @@ static int fall_back_to_classic_diff(struct hashmap *map,
>  		int line1, int count1, int line2, int count2)
>  {
>  	xpparam_t xpp;
> +
> +	memset(&xpp, 0, sizeof(xpp));
>  	xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;

Likewise.

>  	return xdl_fall_back_diff(map->env, &xpp,

But the patch is good as-is, given especially the way how xecfg is
cleared the same way in builtin/merge-tree.c::show_diff().

Will queue.  Thanks.
Michał Kępień Oct. 13, 2020, 6:35 a.m. UTC | #4
Junio, Johannes,

Thank you for your feedback.

> But the patch is good as-is, given especially the way how xecfg is
> cleared the same way in builtin/merge-tree.c::show_diff().

I just wanted to explain that I initially planned to take the "= { 0 }"
approach and I started checking whether all xpparam_t structures in the
source tree are stack-allocated, but I quickly noticed that, as Junio
pointed out, existing code showed an inclination towards memset(), so I
settled for that instead.

> Will queue.  Thanks.

I have a process question: since the other two patches in this patch
series will need a v3, would you like me to keep bundling patch 1 in
this series or should I only send revised version of patches 2 & 3 in
v3?
diff mbox series

Patch

diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index e72714a5a8..de8520778d 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -109,6 +109,7 @@  static void show_diff(struct merge_list *entry)
 	xdemitconf_t xecfg;
 	xdemitcb_t ecb;
 
+	memset(&xpp, 0, sizeof(xpp));
 	xpp.flags = 0;
 	memset(&xecfg, 0, sizeof(xecfg));
 	xecfg.ctxlen = 3;
diff --git a/xdiff/xhistogram.c b/xdiff/xhistogram.c
index c7b35a9667..e694bfd9e3 100644
--- a/xdiff/xhistogram.c
+++ b/xdiff/xhistogram.c
@@ -235,6 +235,8 @@  static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env,
 		int line1, int count1, int line2, int count2)
 {
 	xpparam_t xpparam;
+
+	memset(&xpparam, 0, sizeof(xpparam));
 	xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
 
 	return xdl_fall_back_diff(env, &xpparam,
diff --git a/xdiff/xpatience.c b/xdiff/xpatience.c
index 3c5601b602..20699a6f60 100644
--- a/xdiff/xpatience.c
+++ b/xdiff/xpatience.c
@@ -318,6 +318,8 @@  static int fall_back_to_classic_diff(struct hashmap *map,
 		int line1, int count1, int line2, int count2)
 {
 	xpparam_t xpp;
+
+	memset(&xpp, 0, sizeof(xpp));
 	xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK;
 
 	return xdl_fall_back_diff(map->env, &xpp,