]> diplodocus.org Git - nmh/commitdiff
Tweak mh_xmalloc(); print size in error, allocate zero bytes.
authorRalph Corderoy <ralph@inputplus.co.uk>
Sun, 16 Oct 2016 16:43:56 +0000 (17:43 +0100)
committerRalph Corderoy <ralph@inputplus.co.uk>
Sun, 16 Oct 2016 16:43:56 +0000 (17:43 +0100)
Using `%zu' for the size_t value.  That might be a problem on older
platforms, but we'll see.  If asked for zero bytes then allocate one
rather than exit;  it's sometimes useful to allocate zero, but some
older platforms might not like it.

h/utils.h
sbr/utils.c

index 9e51598..3a42819 100644 (file)
--- a/h/utils.h
+++ b/h/utils.h
@@ -3,7 +3,9 @@
  * utils.h -- utility prototypes
  */
 
+/* Call malloc(3), exiting on NULL return. */
 void *mh_xmalloc(size_t);
+
 void *mh_xrealloc(void *, size_t);
 void *mh_xcalloc(size_t, size_t);
 char *pwd(void);
index 7ebb418..d322890 100644 (file)
@@ -25,22 +25,18 @@ extern char *mhdocdir;
  */
 #define MAXMSGS 256
 
-/*
- * Safely call malloc
- */
-void *
-mh_xmalloc(size_t size)
+/* Call malloc(3), exiting on NULL return. */
+void *mh_xmalloc(size_t size)
 {
-    void *memory;
+    void *p;
 
     if (size == 0)
-        adios(NULL, "Tried to malloc 0 bytes");
+        size = 1; /* Some mallocs don't like 0. */
+    p = malloc(size);
+    if (!p)
+        adios(NULL, "malloc failed, size wanted: %zu", size);
 
-    memory = malloc(size);
-    if (!memory)
-        adios(NULL, "Malloc failed");
-
-    return memory;
+    return p;
 }
 
 /*