[augeas-devel] augeas: master - cutest: fortify against allocation failures

David Lutterkort lutter at fedoraproject.org
Thu Oct 1 00:23:22 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=d3ff2a7ad602e1bc11dcfd88a5618f4d3b7ffa33
Commit:        d3ff2a7ad602e1bc11dcfd88a5618f4d3b7ffa33
Parent:        440c01afef56dbf659a5cf8fef24b14058e65ba3
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Tue Sep 29 17:05:25 2009 -0700
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Wed Sep 30 17:07:48 2009 -0700

cutest: fortify against allocation failures

  - new function die_oom which sets exit status to 2 when
    we die from an oom condition
  - use the safe-alloc macros for allocations
  - abort the test when internal allocations fail
---
 tests/cutest.c |   18 ++++++++++++++----
 tests/cutest.h |    2 ++
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/tests/cutest.c b/tests/cutest.c
index d704459..4cfe0fc 100644
--- a/tests/cutest.c
+++ b/tests/cutest.c
@@ -36,9 +36,8 @@
 #include <math.h>
 
 #include "cutest.h"
+#include "memory.h"
 
-
-#define CU_ALLOC(TYPE)		((TYPE*) malloc(sizeof(TYPE)))
 #define HUGE_STRING_LEN	8192
 #define STRING_MAX		256
 
@@ -48,6 +47,11 @@
         abort();                                                        \
     }
 
+void die_oom(void) {
+    printf("Ran out of memory. Send more\n");
+    exit(2);
+}
+
 /*-------------------------------------------------------------------------*
  * CuTest
  *-------------------------------------------------------------------------*/
@@ -62,7 +66,9 @@ void CuTestInit(CuTest* t, const char* name, TestFunction function) {
 }
 
 CuTest* CuTestNew(const char* name, TestFunction function) {
-	CuTest* tc = CU_ALLOC(CuTest);
+	CuTest* tc = NULL;
+    if (ALLOC(tc) < 0)
+        die_oom();
 	CuTestInit(tc, name, function);
 	return tc;
 }
@@ -180,7 +186,9 @@ void CuSuiteInit(CuSuite* testSuite) {
 }
 
 CuSuite* CuSuiteNew(void) {
-	CuSuite* testSuite = CU_ALLOC(CuSuite);
+	CuSuite* testSuite = NULL;
+    if (ALLOC(testSuite) < 0)
+        die_oom();
 	CuSuiteInit(testSuite);
 	return testSuite;
 }
@@ -222,6 +230,8 @@ static void string_append(char **s, const char *p) {
     } else {
         int len = strlen(*s) + strlen(p) + 1;
         *s = realloc(*s, len);
+        if (*s == NULL)
+            die_oom();
         strcat(*s, p);
     }
 }
diff --git a/tests/cutest.h b/tests/cutest.h
index 390b848..ae9fe0f 100644
--- a/tests/cutest.h
+++ b/tests/cutest.h
@@ -33,6 +33,8 @@
 #include <setjmp.h>
 #include <stdarg.h>
 
+void die_oom(void);
+
 /* CuTest */
 
 typedef struct CuTest CuTest;




More information about the augeas-devel mailing list