git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] fix potential infinite loop given large unsigned integer

From
Ryan Flynn <parseerror@gmail.com>
Date
Aug 9, 2009, 04:41 UTC
Message-ID
<a3f15ee60908082141l7b2134cg5ddcef17c45fc888@mail.gmail.com>

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
Next: Junio C Hamano
Message 1 of 14 in “fix potential infinite loop given large unsigned integer”
  1. fix potential infinite loop given large unsigned integerRyan Flynn, Aug 9, 2009
  2. Junio C HamanoAug 9, 2009
  3. Junio C HamanoAug 9, 2009
  4. Erik Faye-LundAug 9, 2009
  5. Christian CouderAug 10, 2009
  6. Erik Faye-LundAug 10, 2009
  7. Johannes SchindelinAug 10, 2009
  8. Ryan FlynnAug 10, 2009
  9. Ryan FlynnAug 10, 2009
  10. Johannes SchindelinAug 10, 2009
  11. Jeff EplerAug 11, 2009
  12. Ryan FlynnAug 10, 2009
  13. Tony FinchAug 10, 2009
  14. Ryan FlynnAug 9, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.