On 12/9/24 10:15 AM, Richard Biener wrote:
Can you send the patch in git format-patch format please so that
it includes the commit message and Author tag and I can git am
for pushing?  At least you are not listed in MAINTAINERS and thus
likely do not have git commit access?
Yes, currently I do not have no commit access.
Here it comes:

        PR c/114541
        * gimple-parser.cc (c_parser_gimple_parse_bb_spec):
        Use strtoul with ERANGE check instead of atoi to avoid UB
        and detect invalid __BB#. The full treatment of these invalid
        values was considered out of scope for this patch.
From ff0185eda60bec92f0e850090d2e5b8ab54710a9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Heiko=20Ei=C3=9Ffeldt?= <he...@hexco.de>
Date: Mon, 9 Dec 2024 10:39:50 +0100
Subject: [PATCH] replace atoi with stroul in c_parser_gimple_parse_bb_spec
 [PR114541]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Heiko Eißfeldt <he...@hexco.de>
---
 gcc/c/gimple-parser.cc | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc
index 78e85d93487..1a677fc26c7 100644
--- a/gcc/c/gimple-parser.cc
+++ b/gcc/c/gimple-parser.cc
@@ -133,11 +133,21 @@ c_parser_gimple_parse_bb_spec (tree val, int *index)
 {
   if (!startswith (IDENTIFIER_POINTER (val), "__BB"))
     return false;
-  for (const char *p = IDENTIFIER_POINTER (val) + 4; *p; ++p)
-    if (!ISDIGIT (*p))
-      return false;
-  *index = atoi (IDENTIFIER_POINTER (val) + 4);
-  return *index > 0;
+
+  const char *bb = IDENTIFIER_POINTER (val) + 4;
+  if (! ISDIGIT (*bb))
+    return false;
+
+  char *pend;
+  errno = 0;
+  const unsigned long number = strtoul (bb, &pend, 10);
+  if (errno == ERANGE
+      || *pend != '\0'
+      || number > INT_MAX)
+    return false;
+
+  *index = number;
+  return true;
 }
 
 /* See if VAL is an identifier matching __BB<num> and return <num>
-- 
2.47.1

Reply via email to