File shared-Make-new0-abort-on-failure.patch of Package bluez.27456

From 60ee57f899bfe75b7bd2db7f4b0b4b2f8a83441f Mon Sep 17 00:00:00 2001
From: Szymon Janc <szymon.janc@codecoup.pl>
Date: Fri, 16 Oct 2015 12:11:10 +0200
Subject: [PATCH] shared: Make new0 abort on failure

New is used to allocate small (typically much less than 1 page) chunks
and if such allocation fails system is most likely in state where
recovery is unlikely. Also by default Linux follows an optimistic memory
allocation strategy with OOM killer.

Aborting on allocationg failure allows to significantly simplify error
paths (which were most likely never tested anyway) and thus makes code
easier to understand.

btd_malloc name is used as malloc wrapper so that it can be exported
by bluetoothd and used also in external plugins.
---
 src/shared/util.c | 16 ++++++++++++++++
 src/shared/util.h | 14 +++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

Index: bluez-5.13/src/shared/util.c
===================================================================
--- bluez-5.13.orig/src/shared/util.c
+++ bluez-5.13/src/shared/util.c
@@ -31,6 +31,22 @@
 
 #include "src/shared/util.h"
 
+void *btd_malloc(size_t size)
+{
+	if (__builtin_expect(!!size, 1)) {
+		void *ptr;
+
+		ptr = malloc(size);
+		if (ptr)
+			return ptr;
+
+		fprintf(stderr, "failed to allocate %zu bytes\n", size);
+		abort();
+	}
+
+	return NULL;
+}
+
 void util_debug(util_debug_func_t function, void *user_data,
 						const char *format, ...)
 {
Index: bluez-5.13/src/shared/util.h
===================================================================
--- bluez-5.13.orig/src/shared/util.h
+++ bluez-5.13/src/shared/util.h
@@ -23,6 +23,7 @@
 
 #include <stdlib.h>
 #include <alloca.h>
+#include <string.h>
 
 #define PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
 #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u)))
@@ -30,10 +31,21 @@
 #define PTR_TO_INT(p) ((int) ((intptr_t) (p)))
 #define INT_TO_PTR(u) ((void *) ((intptr_t) (u)))
 
-#define new0(t, n) ((t*) calloc((n), sizeof(t)))
+#define new0(type, count)			\
+	(type *) (__extension__ ({		\
+		size_t __n = (size_t) (count);	\
+		size_t __s = sizeof(type);	\
+		void *__p;			\
+		__p = btd_malloc(__n * __s);	\
+		memset(__p, 0, __n * __s);	\
+		__p;				\
+	}))
+
 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
 #define malloc0(n) (calloc((n), 1))
 
+void *btd_malloc(size_t size);
+
 typedef void (*util_debug_func_t)(const char *str, void *user_data);
 
 void util_debug(util_debug_func_t function, void *user_data,
openSUSE Build Service is sponsored by