aboutsummaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authorHeiko Eißfeldt <heiko@hexco.de>2024-12-09 10:39:50 +0100
committerRichard Biener <rguenth@gcc.gnu.org>2024-12-09 13:16:49 +0100
commit2b8ecbfe2ce6558637d42fdcb7efb3f878feb56b (patch)
treef475ca9771e92abc0f79a31110ce62a0d7c972eb /gcc/c
parent2c9b69594665a87f74c6d5cb39fc6e075d75d771 (diff)
downloadgcc-2b8ecbfe2ce6558637d42fdcb7efb3f878feb56b.zip
gcc-2b8ecbfe2ce6558637d42fdcb7efb3f878feb56b.tar.gz
gcc-2b8ecbfe2ce6558637d42fdcb7efb3f878feb56b.tar.bz2
replace atoi with stroul in c_parser_gimple_parse_bb_spec [PR114541]
The full treatment of these invalid values was considered out of scope for this patch. 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#. Signed-off-by: Heiko Eißfeldt <heiko@hexco.de>
Diffstat (limited to 'gcc/c')
-rw-r--r--gcc/c/gimple-parser.cc20
1 files changed, 15 insertions, 5 deletions
diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc
index 78e85d9..1a677fc 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>