Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5a10b58d authored by Andre Eisenbach's avatar Andre Eisenbach
Browse files

Assert on memory allocation failure

This CL is a pre-cursor to cleaning up repeated memory allocation code
full of null-pointer checks. In the vast majority of cases in the
Bluetooth stack, a failed allocation is not recoverable and results in
undefined behaviour. By asserting, we can ensure we have a shot at
catching and fixing any issues not caused by OOM conditions.

Bug: 26494317
Change-Id: I53dff98c596068211934a1808f90de2d4484f952
parent a467e5c9
Loading
Loading
Loading
Loading
+12 −10
Original line number Diff line number Diff line
@@ -27,10 +27,12 @@ static const allocator_id_t alloc_allocator_id = 42;
char *osi_strdup(const char *str) {
  size_t size = strlen(str) + 1;  // + 1 for the null terminator
  size_t real_size = allocation_tracker_resize_for_canary(size);
  void *ptr = malloc(real_size);
  assert(ptr);

  char *new_string = allocation_tracker_notify_alloc(
      alloc_allocator_id,
      malloc(real_size),
      ptr,
      size);
  if (!new_string)
    return NULL;
@@ -45,10 +47,12 @@ char *osi_strndup(const char *str, size_t len) {
    size = len;

  size_t real_size = allocation_tracker_resize_for_canary(size + 1);
  void *ptr = malloc(real_size);
  assert(ptr);

  char *new_string = allocation_tracker_notify_alloc(
      alloc_allocator_id,
      malloc(real_size),
      ptr,
      size + 1);
  if (!new_string)
    return NULL;
@@ -60,18 +64,16 @@ char *osi_strndup(const char *str, size_t len) {

void *osi_malloc(size_t size) {
  size_t real_size = allocation_tracker_resize_for_canary(size);
  return allocation_tracker_notify_alloc(
    alloc_allocator_id,
    malloc(real_size),
    size);
  void *ptr = malloc(real_size);
  assert(ptr);
  return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
}

void *osi_calloc(size_t size) {
  size_t real_size = allocation_tracker_resize_for_canary(size);
  return allocation_tracker_notify_alloc(
    alloc_allocator_id,
    calloc(1, real_size),
    size);
  void *ptr = calloc(1, real_size);
  assert(ptr);
  return allocation_tracker_notify_alloc(alloc_allocator_id, ptr, size);
}

void osi_free(void *ptr) {