summaryrefslogtreecommitdiff
path: root/osi
diff options
context:
space:
mode:
authorAndre Eisenbach <eisenbach@google.com>2016-01-11 12:24:14 -0800
committerAndre Eisenbach <eisenbach@google.com>2016-01-11 22:21:37 +0000
commitc02acb7761d2539ab83c6eff22b7de5b5da51b78 (patch)
tree2de66e7b1c474dff7caf279ff613eccc48217d22 /osi
parent9f41ecbcb4a89ce64dbf1f046501ed78931b6db3 (diff)
downloadandroid-system-bt-c02acb7761d2539ab83c6eff22b7de5b5da51b78.tar.gz
android-system-bt-c02acb7761d2539ab83c6eff22b7de5b5da51b78.tar.xz
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
Diffstat (limited to 'osi')
-rw-r--r--osi/src/allocator.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/osi/src/allocator.c b/osi/src/allocator.c
index 4bafc8d..3fbe4c8 100644
--- a/osi/src/allocator.c
+++ b/osi/src/allocator.c
@@ -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) {