summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schneider <robert.schneider03@sap.com>2022-05-09 16:17:18 +0000
committerRobert Schneider <robert.schneider03@sap.com>2022-05-09 16:17:18 +0000
commit3d77c47356ef152fcd9345053c6001c861bc5426 (patch)
tree846cd8e85af649ddd1770c89eb5d657217cab69e
parent938065a751c0876eb837a27f8c1443fc7d0d2551 (diff)
Avoid undefined behaviour in short option parsing
The undefined behaviour lead to clashes / misinterpretation of short options on some systems. Note that glibc for example uses the argument of isalnum etc. as an index into a lookup table.
-rw-r--r--tools/tools.c5
1 files changed, 5 insertions, 0 deletions
diff --git a/tools/tools.c b/tools/tools.c
index a14b9ca..e11c336 100644
--- a/tools/tools.c
+++ b/tools/tools.c
@@ -74,6 +74,11 @@ struct {
static char
short_option (int opt)
{
+ // isalpha and isdigit require the argument to be representable as unsigned char
+ // if they're not representable as unsigned char, then we assume they're not printable -
+ // matching for example the enumerator definitions in computer.c
+ if (opt != (int)(unsigned char)opt)
+ return 0;
if (isalpha (opt) || isdigit (opt))
return (char)opt;
return 0;