[compiler-rt] fd8e854 - [scudo] simplify flag parser out of bounds logic (#72371)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 16:36:02 PST 2023


Author: Florian Mayer
Date: 2023-12-14T16:35:58-08:00
New Revision: fd8e854a86d86c62064951ea8e34d1ea50ea1b56

URL: https://github.com/llvm/llvm-project/commit/fd8e854a86d86c62064951ea8e34d1ea50ea1b56
DIFF: https://github.com/llvm/llvm-project/commit/fd8e854a86d86c62064951ea8e34d1ea50ea1b56.diff

LOG: [scudo] simplify flag parser out of bounds logic (#72371)

almost NFC, just that now we accept INT_MIN and INT_MAX

as discussed in https://r.android.com/2831100, but I didn't add the
*ValueEnd != Value check because I want to keep this change
behaviour-keeping.

Added: 
    

Modified: 
    compiler-rt/lib/scudo/standalone/flags_parser.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/scudo/standalone/flags_parser.cpp b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
index 6f9b23ea90e23c..3d8c6f3789b4cc 100644
--- a/compiler-rt/lib/scudo/standalone/flags_parser.cpp
+++ b/compiler-rt/lib/scudo/standalone/flags_parser.cpp
@@ -10,6 +10,7 @@
 #include "common.h"
 #include "report.h"
 
+#include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
@@ -143,19 +144,18 @@ bool FlagParser::runHandler(const char *Name, const char *Value,
       break;
     case FlagType::FT_int:
       char *ValueEnd;
+      errno = 0;
       long V = strtol(Value, &ValueEnd, 10);
-      // strtol returns LONG_MAX on overflow and LONG_MIN on underflow.
-      // This is why we compare-equal here (and lose INT_MIN and INT_MAX as a
-      // value, but that's okay)
-      if (V >= INT_MAX || V <= INT_MIN) {
+      if (errno != 0 ||                 // strtol failed (over or underflow)
+          V > INT_MAX || V < INT_MIN || // overflows integer
+          // contains unexpected characters
+          (*ValueEnd != '"' && *ValueEnd != '\'' &&
+           !isSeparatorOrNull(*ValueEnd))) {
         reportInvalidFlag("int", Value);
-        return false;
+        break;
       }
       *reinterpret_cast<int *>(Flags[I].Var) = static_cast<int>(V);
-      Ok =
-          *ValueEnd == '"' || *ValueEnd == '\'' || isSeparatorOrNull(*ValueEnd);
-      if (!Ok)
-        reportInvalidFlag("int", Value);
+      Ok = true;
       break;
     }
     return Ok;


        


More information about the llvm-commits mailing list