[libc-commits] [libc] 3aec947 - [libc][__support/memory_size] fix missing branch and add related tests (#83016)

via libc-commits libc-commits at lists.llvm.org
Mon Feb 26 10:21:30 PST 2024


Author: Schrodinger ZHU Yifan
Date: 2024-02-26T13:21:27-05:00
New Revision: 3aec947b671bf3a2c00ef7d29557db687af8ff7c

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

LOG: [libc][__support/memory_size] fix missing branch and add related tests (#83016)

fix #82644.

Added: 
    

Modified: 
    libc/src/__support/memory_size.h
    libc/test/src/__support/memory_size_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/memory_size.h b/libc/src/__support/memory_size.h
index 4c7d2079553e88..94aee2520afaa1 100644
--- a/libc/src/__support/memory_size.h
+++ b/libc/src/__support/memory_size.h
@@ -52,9 +52,11 @@ class SafeMemSize {
 
   LIBC_INLINE SafeMemSize operator+(const SafeMemSize &other) {
     type result;
-    if (LIBC_UNLIKELY((value | other.value) < 0))
+    if (LIBC_UNLIKELY((value | other.value) < 0)) {
       result = -1;
-    result = value + other.value;
+    } else {
+      result = value + other.value;
+    }
     return SafeMemSize{result};
   }
 

diff  --git a/libc/test/src/__support/memory_size_test.cpp b/libc/test/src/__support/memory_size_test.cpp
index 93ef3711d40e00..1c8f1ce87415bf 100644
--- a/libc/test/src/__support/memory_size_test.cpp
+++ b/libc/test/src/__support/memory_size_test.cpp
@@ -49,6 +49,13 @@ TEST(LlvmLibcMemSizeTest, Addition) {
   ASSERT_FALSE((max + SafeMemSize{static_cast<size_t>(1)}).valid());
   ASSERT_FALSE((third + third + third + third).valid());
   ASSERT_FALSE((half + half + half).valid());
+
+  ASSERT_FALSE((SafeMemSize{static_cast<size_t>(-1)} +
+                SafeMemSize{static_cast<size_t>(2)})
+                   .valid());
+  ASSERT_FALSE((SafeMemSize{static_cast<size_t>(2)} +
+                SafeMemSize{static_cast<size_t>(-1)})
+                   .valid());
 }
 
 TEST(LlvmLibcMemSizeTest, Multiplication) {


        


More information about the libc-commits mailing list