The following commit has been merged in the master branch:
commit 099b7f8f4b02b90bb744ce482a9272ce44fbdda4
Author: Guillem Jover <[email protected]>
Date:   Sun Nov 13 19:53:20 2011 +0100

    libdpkg: Refactor allocation checks into a new must_alloc() function

diff --git a/lib/dpkg/mlib.c b/lib/dpkg/mlib.c
index f31d99b..483bd43 100644
--- a/lib/dpkg/mlib.c
+++ b/lib/dpkg/mlib.c
@@ -32,17 +32,23 @@
 #include <dpkg/i18n.h>
 #include <dpkg/dpkg.h>
 
+static inline void *
+must_alloc(void *ptr)
+{
+  if (ptr)
+    return ptr;
+
+  onerr_abort++;
+  ohshite(_("failed to allocate memory"));
+}
+
 void *m_malloc(size_t amount) {
 #ifdef MDEBUG
   unsigned short *r2, x;
 #endif
   void *r;
 
-  onerr_abort++;
-  r= malloc(amount);
-  if (r == NULL)
-    ohshite(_("malloc failed (%zu bytes)"), amount);
-  onerr_abort--;
+  r = must_alloc(malloc(amount));
 
 #ifdef MDEBUG
   r2= r; x= (unsigned short)amount ^ 0xf000;
@@ -52,27 +58,13 @@ void *m_malloc(size_t amount) {
 }
 
 void *m_realloc(void *r, size_t amount) {
-  onerr_abort++;
-  r= realloc(r,amount);
-  if (r == NULL)
-    ohshite(_("realloc failed (%zu bytes)"), amount);
-  onerr_abort--;
-
-  return r;
+  return must_alloc(realloc(r, amount));
 }
 
 char *
 m_strdup(const char *str)
 {
-  char *new_str;
-
-  onerr_abort++;
-  new_str = strdup(str);
-  if (!new_str)
-    ohshite(_("failed to allocate memory"));
-  onerr_abort--;
-
-  return new_str;
+  return must_alloc(strdup(str));
 }
 
 int

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to