Skip to contentSkip to conversation

mergesort: avoid left shift overflow

PATCH In review René Scharfe 4 participants 10 messages Nov 15, 2021 – Nov 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mergesort: avoid left shift overflow

Use size_t to match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.

---
Ugh, sorry. :(

 mergesort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mergesort.c +1 -1
index 6216835566..bd9c6ef8ee 100644
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)
 			list = llist_merge(ranks[i], list, get_next_fn,
 					   set_next_fn, compare_fn);
 		n++;

--
2.33.1

#1 lore raw

Hi René,

Quoted 15 lines · René Scharfe

Use size_t to match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.

---
Ugh, sorry. :(

 mergesort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mergesort.c +0 -0
index 6216835566..bd9c6ef8ee 100644

mergesort.c void *llist_mergesort(void *list, · line 66

Quoted 5 lines
mergesort.c +1 -1
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)

I was a bit concerned about the operator precedence (some of which I remember by heart, some not), but according to
https://en.cppreference.com/w/c/language/operator_precedence the cast has a higher precedence than the shift operator.

I would have preferred an extra pair of parentheses around (size_t)1 so that I (and other readers) do not have to remember or look up the operator precedence, but it _is_ correct.

Ciao,
Dscho

Quoted 6 lines
mergesort.c +0 -1
 			list = llist_merge(ranks[i], list, get_next_fn,
 					   set_next_fn, compare_fn);
 		n++;
--

2.33.1

#2 lore raw
Quoted 16 lines · Johannes Schindelin

mergesort.c void *llist_mergesort(void *list, · line 66

Quoted 10 lines
mergesort.c +1 -1
index 6216835566..bd9c6ef8ee 100644
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)

I was a bit concerned about the operator precedence (some of which I remember by heart, some not), but according to
https://en.cppreference.com/w/c/language/operator_precedence the cast has a higher precedence than the shift operator.

Quoted 3 lines

I would have preferred an extra pair of parentheses around (size_t)1 so that I (and other readers) do not have to remember or look up the operator precedence, but it _is_ correct.

Interesting.

I do not quite see the need for it myself, but if we wanted to, we can smoke them out with this, I think.

	$ cat >contrib/coccinelle/cast.cocci <<-\EOF
	@@
	type T;
	expression V, C;
	@@
	-(T) V << C
	+((T) V) << C
	EOF
	$ make contrib/coccinelle/cast.cocci.patch
	$ git apply --stat contrib/coccinelle/cast.cocci.patch
         compat/mingw.c     |    2 +-
         compat/mingw.c     |    2 +-
         ewah/bitmap.c      |    2 +-
         ewah/ewok_rlw.h    |    6 +++---
         ewah/ewah_bitmap.c |    8 ++++----
         ewah/ewok_rlw.h    |    6 +++---
         ppc/sha1.c         |    2 +-
         wrapper.c          |    2 +-
         8 files changed, 15 insertions(+), 15 deletions(-)
#3 lore raw

Hi Junio,

Quoted 27 lines · Junio C Hamano
Quoted 16 lines · Johannes Schindelin

mergesort.c void *llist_mergesort(void *list, · line 66

Quoted 10 lines
mergesort.c +1 -1
index 6216835566..bd9c6ef8ee 100644
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)

I was a bit concerned about the operator precedence (some of which I remember by heart, some not), but according to
https://en.cppreference.com/w/c/language/operator_precedence the cast has a higher precedence than the shift operator.

Quoted 3 lines

I would have preferred an extra pair of parentheses around (size_t)1 so that I (and other readers) do not have to remember or look up the operator precedence, but it _is_ correct.

Interesting.

I do not quite see the need for it myself, but if we wanted to, we can smoke them out with this, I think.

Quoted 8 lines
	$ cat >contrib/coccinelle/cast.cocci <<-\EOF
	@@
	type T;
	expression V, C;
	@@
	-(T) V << C
	+((T) V) << C
	EOF

Cute.

However, I am not a fan of fixing what ain't broken (the many refactorings in v2.34.0 did cause quite some fall-out work in git-for-windows/git and microsoft/git, after all, and at least _I_ do not yet see much benefit to show for that price we paid for those refactorings).

And the primary benefit of enclosing the left operand in parentheses is to make reviews easier, so I would prefer to leave existing (read:
_already-reviewed_) code alone.

Thanks,
Dscho

Quoted 14 lines
	$ make contrib/coccinelle/cast.cocci.patch
	$ git apply --stat contrib/coccinelle/cast.cocci.patch
         compat/mingw.c     |    2 +-
         compat/mingw.c     |    2 +-
         ewah/bitmap.c      |    2 +-
         ewah/ewok_rlw.h    |    6 +++---
         ewah/ewah_bitmap.c |    8 ++++----
         ewah/ewok_rlw.h    |    6 +++---
         ppc/sha1.c         |    2 +-
         wrapper.c          |    2 +-
         8 files changed, 15 insertions(+), 15 deletions(-)
#4 lore raw
In reply to René Scharfe
Quoted 24 lines · René Scharfe

Use size_t to match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.

---
Ugh, sorry. :(

 mergesort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mergesort.c +1 -2
index 6216835566..bd9c6ef8ee 100644
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)
 			list = llist_merge(ranks[i], list, get_next_fn,
 					   set_next_fn, compare_fn);
 		n++;
--
Quoted 1 line

2.33.1

Looks good to me.

I already had this locally as part of an MSVC (Visual Studio) fix for the various C4334 warnings.

The other cases are in object-file.c, diffcore-delta.c (2 occurrences) , and repack.c.

My patches are in https://github.com/PhilipOakley/git/tree/oneU_t

Philip

#5 lore raw

Hi Philip,

Quoted 28 lines · Philip Oakley
Quoted 24 lines · René Scharfe

Use size_t to match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.

---
Ugh, sorry. :(

 mergesort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mergesort.c +1 -2
index 6216835566..bd9c6ef8ee 100644
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)
 			list = llist_merge(ranks[i], list, get_next_fn,
 					   set_next_fn, compare_fn);
 		n++;
--
Quoted 1 line

2.33.1

Looks good to me.

Quoted 7 lines

I already had this locally as part of an MSVC (Visual Studio) fix for the various C4334 warnings.

The other cases are in object-file.c, diffcore-delta.c (2 occurrences) , and repack.c.

My patches are in https://github.com/PhilipOakley/git/tree/oneU_t

How about rebasing the remaining patches from
https://github.com/git-for-windows/git/compare/main...PhilipOakley:oneU_t
on top of rs/mergesort and then submitting them, to avoid duplicate efforts?

Ciao,
Dscho

#6 lore raw

Am 19.11.21 um 17:51 schrieb Johannes Schindelin:

Quoted 11 lines

Hi Philip,

Quoted 7 lines · Philip Oakley

I already had this locally as part of an MSVC (Visual Studio) fix for the various C4334 warnings.

The other cases are in object-file.c, diffcore-delta.c (2 occurrences) , and repack.c.

My patches are in https://github.com/PhilipOakley/git/tree/oneU_t

Good warning, that. And scary stuff.

builtin/repack.c shifts by 5, diffcore-delta.c by at most 17 IIUC, and object-file.c by at most 31, which should all be safe. Shifting by 32 or more would push the 1 off the right end, leaving only a surprising 0. Phew! Definitely worth fixing regardless.

Quoted 4 lines

How about rebasing the remaining patches from
https://github.com/git-for-windows/git/compare/main...PhilipOakley:oneU_t
on top of rs/mergesort and then submitting them, to avoid duplicate efforts?

That would be nice. I'd also don't mind if Junio took the whole set incl. the mergesort.c change instead.

René

#7 lore raw
In reply to Johannes Schindelin
Quoted 35 lines · Johannes Schindelin

Hi Philip,

Quoted 31 lines · Philip Oakley
Quoted 24 lines · René Scharfe

Use size_t to match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.

---
Ugh, sorry. :(

 mergesort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mergesort.c +1 -2
index 6216835566..bd9c6ef8ee 100644
@@ -63,7 +63,7 @@ void *llist_mergesort(void *list,
 		void *next = get_next_fn(list);
 		if (next)
 			set_next_fn(list, NULL);
-		for (i = 0; n & (1 << i); i++)
+		for (i = 0; n & ((size_t)1 << i); i++)
 			list = llist_merge(ranks[i], list, get_next_fn,
 					   set_next_fn, compare_fn);
 		n++;
--
Quoted 1 line

2.33.1

Looks good to me.

I already had this locally as part of an MSVC (Visual Studio) fix for the various C4334 warnings.

Quoted 9 lines
Quoted 4 lines

The other cases are in object-file.c, diffcore-delta.c (2 occurrences) , and repack.c.

My patches are in https://github.com/PhilipOakley/git/tree/oneU_t

How about rebasing the remaining patches from
https://github.com/git-for-windows/git/compare/main...PhilipOakley:oneU_t
on top of rs/mergesort and then submitting them, to avoid duplicate efforts?

I'm not finding a rs/mergesort at the  moment. Any particular remote I should find it on, or maybe a different spelling?
P.

#8 lore raw
Quoted 9 lines · Philip Oakley
Quoted 6 lines
Quoted 1 line

My patches are in https://github.com/PhilipOakley/git/tree/oneU_t

How about rebasing the remaining patches from
https://github.com/git-for-windows/git/compare/main...PhilipOakley:oneU_t
on top of rs/mergesort and then submitting them, to avoid duplicate efforts?

I'm not finding a rs/mergesort at the moment. Any particular remote I should find it on, or maybe a different spelling?
P.

At https://github.com/gitster/git/, I publish the "broken out" branches.

But resurrecting the tips of the topics should be fairly easy.

$ git log --first-parent --oneline origin/master..origin/seen |
  grep rs/mergesort

The second parent of that merge is the tip of rs/mergesort topic back when the 'seen' integration branch was created.

#9 lore raw
Quoted 14 lines · Junio C Hamano
Quoted 9 lines · Philip Oakley
Quoted 6 lines
Quoted 1 line

My patches are in https://github.com/PhilipOakley/git/tree/oneU_t

How about rebasing the remaining patches from
https://github.com/git-for-windows/git/compare/main...PhilipOakley:oneU_t
on top of rs/mergesort and then submitting them, to avoid duplicate efforts?

I'm not finding a rs/mergesort at the moment. Any particular remote I should find it on, or maybe a different spelling?
P.

At https://github.com/gitster/git/, I publish the "broken out" branches.

Quoted 8 lines

But resurrecting the tips of the topics should be fairly easy.

$ git log --first-parent --oneline origin/master..origin/seen |
  grep rs/mergesort

The second parent of that merge is the tip of rs/mergesort topic back when the 'seen' integration branch was created.

Thanks. I'd been browsing via gitk and hadn't included all the tips, so couldn't find it at that point. I've got it now:  42c456ff81 (mergesort: avoid left shift overflow, 2021-11-16).

Philip