git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fix potential infinite loop given large unsigned integer
@ 2009-08-09  4:41 Ryan Flynn
  2009-08-09  6:19 ` Junio C Hamano
  0 siblings, 1 reply; 14+ messages in thread
From: Ryan Flynn @ 2009-08-09  4:41 UTC (permalink / raw)
  To: git

given n, tried to find i greater than n via i=1, iterate i *= 10.
given n sufficiently close to UINT_MAX this will overflow; which can
produce i==0, which results in an infinite loop. iteratively dividing
n /= 10 does not have this problem, and though division is slower than
multiplication this only runs once per `git format-patch
--cover-letter`

Signed-off-by: pizza <parseerror@gmail.com>
---
 log-tree.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/log-tree.c b/log-tree.c
index 6f73c17..53a1bd2 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -160,9 +160,9 @@ static void append_signoff(struct strbuf *sb,
const char *signoff)

 static unsigned int digits_in_number(unsigned int number)
 {
-       unsigned int i = 10, result = 1;
-       while (i <= number) {
-               i *= 10;
+       unsigned int result = 1;
+       while (number > 9) {
+    number /= 10;
                result++;
        }
        return result;
-- 
1.6.0.4

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

end of thread, other threads:[~2009-08-11  0:55 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-09  4:41 [PATCH] fix potential infinite loop given large unsigned integer Ryan Flynn
2009-08-09  6:19 ` Junio C Hamano
2009-08-09  7:38   ` Junio C Hamano
2009-08-09 12:25     ` Erik Faye-Lund
2009-08-10  5:24       ` Christian Couder
2009-08-10 11:12         ` Erik Faye-Lund
2009-08-10 12:24           ` Johannes Schindelin
2009-08-10 16:14             ` Ryan Flynn
2009-08-10 16:17             ` Ryan Flynn
2009-08-10 16:53               ` Johannes Schindelin
2009-08-11  0:55       ` Jeff Epler
2009-08-10  0:23     ` Ryan Flynn
2009-08-10 18:19     ` Tony Finch
2009-08-09 23:16   ` Ryan Flynn

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 NNTP newsgroup(s).