[compiler-rt] 9a42715 - [sanitizer] Always initialize the regex in the regcomp() interceptor

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 15 14:31:33 PDT 2022


Author: Tavian Barnes
Date: 2022-03-15T14:30:57-07:00
New Revision: 9a42715ae887e496df9122b29e86a6d8ebe5d53f

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

LOG: [sanitizer] Always initialize the regex in the regcomp() interceptor

When regcomp() fails, the same regex_t* should be passed to regerror()
for potentially better error messages.  But doing that with msan would
report a use-of-uninitialized-value.

Fixes https://github.com/google/sanitizers/issues/1496

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D120591

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/test/sanitizer_common/TestCases/Posix/regex.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 0e59c48619dd4..eeeeb1622ff5b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -7974,7 +7974,7 @@ INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
   if (pattern)
     COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
   int res = REAL(regcomp)(preg, pattern, cflags);
-  if (!res)
+  if (preg)
     COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
   return res;
 }

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Posix/regex.cpp b/compiler-rt/test/sanitizer_common/TestCases/Posix/regex.cpp
index 3727f01325f87..63408dee2b637 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Posix/regex.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/Posix/regex.cpp
@@ -42,23 +42,30 @@ void test_print_matches(const regex_t *preg, const char *string) {
 int main(void) {
   printf("regex\n");
 
-  regex_t regex;
-  int rv = regcomp(&regex, "[[:upper:]]\\([[:upper:]]\\)", 0);
-  assert(!rv);
+  {
+    regex_t regex;
+    int rv = regcomp(&regex, "[[:upper:]]\\([[:upper:]]\\)", 0);
+    assert(!rv);
 
-  test_matched(&regex, "abc");
-  test_matched(&regex, "ABC");
+    test_matched(&regex, "abc");
+    test_matched(&regex, "ABC");
 
-  test_print_matches(&regex, "ABC");
+    test_print_matches(&regex, "ABC");
 
-  regfree(&regex);
+    regfree(&regex);
+  }
 
-  rv = regcomp(&regex, "[[:upp:]]", 0);
-  assert(rv);
+  {
+    regex_t regex;
+    int rv = regcomp(&regex, "[[:upp:]]", 0);
+    assert(rv);
 
-  char errbuf[1024];
-  regerror(rv, &regex, errbuf, sizeof errbuf);
-  printf("error: %s\n", errbuf);
+    char errbuf[1024];
+    regerror(rv, &regex, errbuf, sizeof errbuf);
+    printf("error: %s\n", errbuf);
+
+    regfree(&regex);
+  }
 
   // CHECK: regex
   // CHECK: abc: not-matched


        


More information about the llvm-commits mailing list