mergesort: avoid left shift overflow
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
@@ -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
Hi René,
Quoted 15 lines · René Scharfe
Use
size_tto match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.Signed-off-by René Scharfe <[email protected]>---
Ugh, sorry. :(mergesort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mergesort.c +0
-0
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
-1list =llist_merge (ranks[i], list, get_next_fn, set_next_fn, compare_fn); n++; --2.33.1
Quoted 16 lines · Johannes Schindelin
mergesort.c void *llist_mergesort(void *list, · line 66
Quoted 10 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.
Quoted 3 lines
I would have preferred an extra pair of parentheses around
(size_t)1so 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(-)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@@ -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)1so 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(-)Quoted 24 lines · René Scharfe
Use
size_tto match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.Signed-off-by René Scharfe <[email protected]>---
Ugh, sorry. :(mergesort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mergesort.c +1
-2@@ -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
Hi Philip,
Quoted 28 lines · Philip Oakley
Quoted 24 lines · René Scharfe
Use
size_tto match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.Signed-off-by René Scharfe <[email protected]>---
Ugh, sorry. :(mergesort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mergesort.c +1
-2@@ -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) , andrepack.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
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) , andrepack.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 ofrs/mergesortand 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é
Quoted 35 lines · Johannes Schindelin
Hi Philip,
Quoted 31 lines · Philip Oakley
Quoted 24 lines · René Scharfe
Use
size_tto match n when building the bitmask for checking whether a rank is occupied, instead of the default signed int.Signed-off-by René Scharfe <[email protected]>---
Ugh, sorry. :(mergesort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mergesort.c +1
-2@@ -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) , andrepack.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 ofrs/mergesortand 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.
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 ofrs/mergesortand then submitting them, to avoid duplicate efforts?I'm not finding a
rs/mergesortat 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.
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 ofrs/mergesortand then submitting them, to avoid duplicate efforts?I'm not finding a
rs/mergesortat 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/mergesortThe 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