bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
* [PATCH] tests: fix signed overflow issues
@ 2021-03-21 21:52 Paul Eggert
  2021-03-22  0:28 ` Bruno Haible
  0 siblings, 1 reply; 2+ messages in thread
From: Paul Eggert @ 2021-03-21 21:52 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Paul Eggert

* tests/test-dynarray.c (value_at): Avoid undefined behavior
in signed integer multiplication.
* tests/test-scratch-buffer.c (byte_at): Likewise, for the
theoretically-possible case where size_t is narrower than int.
---
 ChangeLog                   | 8 ++++++++
 tests/test-dynarray.c       | 2 +-
 tests/test-scratch-buffer.c | 2 +-
 3 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 76ea9c3eb..6c4d4b5f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2021-03-21  Paul Eggert  <eggert@cs.ucla.edu>
+
+	tests: fix signed overflow issues
+	* tests/test-dynarray.c (value_at): Avoid undefined behavior
+	in signed integer multiplication.
+	* tests/test-scratch-buffer.c (byte_at): Likewise, for the
+	theoretically-possible case where size_t is narrower than int.
+
 2021-03-21  Bruno Haible  <bruno@clisp.org>
 
 	doc: More updates.
diff --git a/tests/test-dynarray.c b/tests/test-dynarray.c
index 4308148f8..c3f9a29b5 100644
--- a/tests/test-dynarray.c
+++ b/tests/test-dynarray.c
@@ -28,7 +28,7 @@
 #define N 100000
 
 static int
-value_at (int i)
+value_at (long long int i)
 {
   return (i % 13) + ((i * i) % 251);
 }
diff --git a/tests/test-scratch-buffer.c b/tests/test-scratch-buffer.c
index f05c84fa4..517d73fd7 100644
--- a/tests/test-scratch-buffer.c
+++ b/tests/test-scratch-buffer.c
@@ -24,7 +24,7 @@
 #include "macros.h"
 
 static int
-byte_at (size_t i)
+byte_at (unsigned long long int i)
 {
   return ((i % 13) + ((i * i) % 251)) & 0xff;
 }
-- 
2.30.2



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] tests: fix signed overflow issues
  2021-03-21 21:52 [PATCH] tests: fix signed overflow issues Paul Eggert
@ 2021-03-22  0:28 ` Bruno Haible
  0 siblings, 0 replies; 2+ messages in thread
From: Bruno Haible @ 2021-03-22  0:28 UTC (permalink / raw)
  To: bug-gnulib; +Cc: Paul Eggert

> * tests/test-dynarray.c (value_at): Avoid undefined behavior
> in signed integer multiplication.

Thanks. Indeed with N=100000 the multiplication would overflow.
If I had used CC="gcc -ftrapv", I would have noticed.

Bruno



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-03-22  0:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-21 21:52 [PATCH] tests: fix signed overflow issues Paul Eggert
2021-03-22  0:28 ` Bruno Haible

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 read-only IMAP folder(s) and NNTP newsgroup(s).