[libc-commits] [PATCH] D150529: [libc] Make the bump pointer explicitly return null on buffer oveerrun

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon May 15 04:16:13 PDT 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rG9417d9fc38c8: [libc] Make the bump pointer explicitly return null on buffer oveerrun (authored by jhuber6).

Changed prior to commit:
  https://reviews.llvm.org/D150529?vs=522013&id=522120#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150529/new/

https://reviews.llvm.org/D150529

Files:
  libc/test/IntegrationTest/test.cpp
  libc/test/UnitTest/HermeticTestUtils.cpp


Index: libc/test/UnitTest/HermeticTestUtils.cpp
===================================================================
--- libc/test/UnitTest/HermeticTestUtils.cpp
+++ libc/test/UnitTest/HermeticTestUtils.cpp
@@ -29,7 +29,8 @@
 // requires. Hence, as a work around for this problem, we use a simple allocator
 // which just hands out continuous blocks from a statically allocated chunk of
 // memory.
-static uint8_t memory[16384];
+static constexpr uint64_t MEMORY_SIZE = 16384;
+static uint8_t memory[MEMORY_SIZE];
 static uint8_t *ptr = memory;
 
 } // anonymous namespace
@@ -68,7 +69,7 @@
   s = ((s + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
   void *mem = ptr;
   ptr += s;
-  return mem;
+  return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
 }
 
 void free(void *) {}
Index: libc/test/IntegrationTest/test.cpp
===================================================================
--- libc/test/IntegrationTest/test.cpp
+++ libc/test/IntegrationTest/test.cpp
@@ -57,7 +57,8 @@
 // which just hands out continuous blocks from a statically allocated chunk of
 // memory.
 
-static uint8_t memory[16384];
+static constexpr uint64_t MEMORY_SIZE = 16384;
+static uint8_t memory[MEMORY_SIZE];
 static uint8_t *ptr = memory;
 
 extern "C" {
@@ -65,7 +66,7 @@
 void *malloc(size_t s) {
   void *mem = ptr;
   ptr += s;
-  return mem;
+  return static_cast<uint64_t>(ptr - memory) >= MEMORY_SIZE ? nullptr : mem;
 }
 
 void free(void *) {}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150529.522120.patch
Type: text/x-patch
Size: 1458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230515/ea7056d2/attachment-0001.bin>


More information about the libc-commits mailing list