Browse Source

check malloc/calloc

Francois Perrad 10 years ago
parent
commit
82e82ae7ae
7 changed files with 11 additions and 1 deletions
  1. 1 0
      tests/test15.c
  2. 1 0
      tests/test16.c
  3. 1 0
      tests/test20.c
  4. 1 0
      tests/test21.c
  5. 2 0
      tests/test22.c
  6. 3 0
      tests/test23.c
  7. 2 1
      tests/test38.c

+ 1 - 0
tests/test15.c

@@ -17,6 +17,7 @@ int main(int argc, char *argv[]) {
 
     for (n = names; *n != NULL; n++) {
         s = (struct my_struct*)malloc(sizeof(struct my_struct));
+        if (s == NULL) exit(-1);
         strncpy(s->name, *n,10UL);
         s->id = i++;
         HASH_ADD_STR( users, name, s );

+ 1 - 0
tests/test16.c

@@ -27,6 +27,7 @@ int main(int argc, char *argv[]) {
 
     for(i = 0; i < 10; i++) {
         e = (struct my_event*)malloc(sizeof(struct my_event));
+        if (e == NULL) exit(-1);
         memset(e,0,sizeof(struct my_event));
         e->is.a = i * (60*60*24*365);          /* i years (sec)*/
         e->is.b = 0;

+ 1 - 0
tests/test20.c

@@ -15,6 +15,7 @@ int main(int argc, char *argv[]) {
 
     /* allocate our structure. initialize to some values */
     s = (struct my_struct*)calloc(1UL,sizeof(struct my_struct));
+    if (s == NULL) exit(-1);
     memcpy(s->bkey, binary, sizeof(binary));
 
     /* add to hash table using general macro */

+ 1 - 0
tests/test21.c

@@ -17,6 +17,7 @@ int main(int argc, char *argv[]) {
     record_t l, *p, *r, *tmp, *records = NULL;
 
     r = (record_t*)malloc( sizeof(record_t) );
+    if (r == NULL) exit(-1);
     memset(r, 0, sizeof(record_t));
     r->key.a = 'a';
     r->key.b = 1;

+ 2 - 0
tests/test22.c

@@ -27,6 +27,7 @@ int main(int argc, char *argv[]) {
 
     /* allocate and initialize our structure */
     msg = (msg_t*)malloc( sizeof(msg_t) + sizeof(beijing) );
+    if (msg == NULL) exit(-1);
     memset(msg, 0, sizeof(msg_t)+sizeof(beijing)); /* zero fill */
     msg->len = sizeof(beijing);
     msg->encoding = UTF32;
@@ -44,6 +45,7 @@ int main(int argc, char *argv[]) {
     msg=NULL;
 
     lookup_key = (lookup_key_t*)malloc(sizeof(*lookup_key) + sizeof(beijing));
+    if (lookup_key == NULL) exit(-1);
     memset(lookup_key, 0, sizeof(*lookup_key) + sizeof(beijing));
     lookup_key->encoding = UTF32;
     memcpy(lookup_key->text, beijing, sizeof(beijing));

+ 3 - 0
tests/test23.c

@@ -15,18 +15,21 @@ int main() {
   /* first item */
   k = 12345;
   i = (item*)malloc(sizeof(item));
+  if (i == NULL) exit(-1);
   i->key = k; i->data = 0;
   HASH_ADD_INT(items,key,i);
 
   /* second item */
   k = 6789;
   i = (item*)malloc(sizeof(item));
+  if (i == NULL) exit(-1);
   i->key = k; i->data = 0;
   HASH_ADD_INT(items,key,i);
 
   /* third item */
   k = 98765;
   i = (item*)malloc(sizeof(item));
+  if (i == NULL) exit(-1);
   i->key = k; i->data = 0;
   HASH_ADD_INT(items,key,i);
 

+ 2 - 1
tests/test38.c

@@ -14,8 +14,9 @@ int main(void) {
     for (a=0; a < 10; a++) {
       test = NULL;
       HASH_FIND(hh, tests, &a, sizeof(a), test);
-      if (! test) {
+      if (test == NULL) {
         test = (struct test_t*)malloc(sizeof(struct test_t));
+        if (test == NULL) exit(-1);
         memset(test, 0, sizeof(struct test_t));
         test->a = a;
         HASH_ADD(hh, tests, a, sizeof(a), test);