Refactor check_bounds (#756)

This changes the shape of check_bounds to take a continuation to call if the bounds check succeeds.  This is designed to allow for easily wrapping existing code with a bounds check, e.g.

```
void* memcpy(void* dest, const void* src, size_t n) {
  return check_bounds(dest, n, [&] {
    return memcpy_impl(dest, src, n);
  });
}
```
This commit is contained in:
Matthew Parkinson
2025-03-14 16:09:09 +00:00
committed by GitHub
parent dfda7a8442
commit a106a2e69d
4 changed files with 43 additions and 45 deletions

View File

@@ -94,15 +94,13 @@ void memcpy_unchecked(void* dst, const void* src, size_t size)
}
NOINLINE
void memcpy_platform_checked(void* dst, const void* src, size_t size)
void* memcpy_platform_checked(void* dst, const void* src, size_t size)
{
if (SNMALLOC_UNLIKELY(!check_bounds(dst, size)))
{
report_fatal_bounds_error(dst, size, "");
return;
}
memcpy(dst, src, size);
return check_bound(
dst,
size,
"memcpy with destination out of bounds of heap allocation",
[&]() { return memcpy(dst, src, size); });
}
int main(int argc, char** argv)