]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix UB in string_slice::operator== (PR 121261)
authorAlfie Richards <alfie.richards@arm.com>
Mon, 28 Jul 2025 13:32:45 +0000 (13:32 +0000)
committerAlfie Richards <alfie.richards@arm.com>
Tue, 29 Jul 2025 08:25:45 +0000 (08:25 +0000)
This adds a nullptr check to fix a regression where it is possible to call
`memcmp (NULL, NULL, 0)` which is UB prior to C26.

This fixes the bootstrap-ubsan build.

gcc/ChangeLog:
PR middle-end/121261
* vec.h: Add null ptr check.

gcc/vec.h

index 9604edb1c3cf3680a264e1bf1c67e83b0b2decf4..0ea7a49573400f4f767f3aa9443d9306745dc7e1 100644 (file)
--- a/gcc/vec.h
+++ b/gcc/vec.h
@@ -2514,6 +2514,10 @@ public:
       return false;
     if (lhs.size () != rhs.size ())
       return false;
+    /* Case where either is a NULL pointer and therefore, as both are valid,
+       both are empty slices with length 0.  */
+    if (lhs.size () == 0)
+      return true;
     return memcmp (lhs.begin (), rhs.begin (), lhs.size ()) == 0;
   }