Changeset: 1f05311feb6c for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1f05311feb6c
Modified Files:
        sql/backends/monet5/rel_bin.c
        sql/common/sql_hash.c
        sql/common/sql_list.c
        sql/server/rel_exp.c
        sql/storage/sql_catalog.c
Branch: Dec2016
Log Message:

Add checks: I've seen allocation fail in hash_add.  It's not academic.


diffs (191 lines):

diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -62,12 +62,19 @@ list_find_column(sql_allocator *sa, list
        MT_lock_set(&l->ht_lock);
        if (!l->ht && list_length(l) > HASH_MIN_SIZE) {
                l->ht = hash_new(l->sa, MAX(list_length(l), l->expected_cnt), 
(fkeyvalue)&stmt_key);
+               if (l->ht == NULL) {
+                       MT_lock_unset(&l->ht_lock);
+                       return NULL;
+               }
 
                for (n = l->h; n; n = n->next) {
                        const char *nme = column_name(sa, n->data);
                        int key = hash_key(nme);
 
-                       hash_add(l->ht, key, n->data);
+                       if (hash_add(l->ht, key, n->data) == NULL) {
+                               MT_lock_unset(&l->ht_lock);
+                               return NULL;
+                       }
                }
        }
        if (l->ht) {
diff --git a/sql/common/sql_hash.c b/sql/common/sql_hash.c
--- a/sql/common/sql_hash.c
+++ b/sql/common/sql_hash.c
@@ -27,6 +27,8 @@ hash_new(sql_allocator *sa, int size, fk
        int i;
        sql_hash *ht = SA_ZNEW(sa, sql_hash);
 
+       if (ht == NULL)
+               return NULL;
        ht->sa = sa;
        ht->size = (1<<log_base2(size-1));
        ht->key = key;
@@ -41,6 +43,8 @@ hash_add(sql_hash *h, int key, void *val
 {
        sql_hash_e *e = SA_ZNEW(h->sa, sql_hash_e);
 
+       if (e == NULL)
+               return NULL;
        e->chain = h->buckets[key&(h->size-1)];
        h->buckets[key&(h->size-1)] = e;
        e->key = key;
diff --git a/sql/common/sql_list.c b/sql/common/sql_list.c
--- a/sql/common/sql_list.c
+++ b/sql/common/sql_list.c
@@ -15,6 +15,8 @@ node_create(sql_allocator *sa, void *dat
 {
        node *n = (sa)?SA_NEW(sa, node):MNEW(node);
 
+       if (n == NULL)
+               return NULL;
        n->next = NULL;
        n->data = data;
        return n;
@@ -130,6 +132,8 @@ list_append(list *l, void *data)
 {
        node *n = node_create(l->sa, data);
 
+       if (n == NULL)
+               return NULL;
        if (l->cnt) {
                l->t->next = n;
        } else {
@@ -141,7 +145,10 @@ list_append(list *l, void *data)
        if (l->ht) {
                int key = l->ht->key(data);
        
-               hash_add(l->ht, key, data);
+               if (hash_add(l->ht, key, data) == NULL) {
+                       MT_lock_unset(&l->ht_lock);
+                       return NULL;
+               }
        }
        MT_lock_unset(&l->ht_lock);
        return l;
@@ -153,6 +160,8 @@ list_append_before(list *l, node *m, voi
        node *p = l->h;
        node *n = node_create(l->sa, data);
 
+       if (n == NULL)
+               return NULL;
        n->next = m;
        if (p == m){
                l->h = n;
@@ -166,7 +175,10 @@ list_append_before(list *l, node *m, voi
        if (l->ht) {
                int key = l->ht->key(data);
        
-               hash_add(l->ht, key, data);
+               if (hash_add(l->ht, key, data) == NULL) {
+                       MT_lock_unset(&l->ht_lock);
+                       return NULL;
+               }
        }
        MT_lock_unset(&l->ht_lock);
        return l;
@@ -177,6 +189,8 @@ list_prepend(list *l, void *data)
 {
        node *n = node_create(l->sa, data);
 
+       if (n == NULL)
+               return NULL;
        if (!l->cnt) {
                l->t = n;
        }
@@ -187,7 +201,10 @@ list_prepend(list *l, void *data)
        if (l->ht) {
                int key = l->ht->key(data);
        
-               hash_add(l->ht, key, data);
+               if (hash_add(l->ht, key, data) == NULL) {
+                       MT_lock_unset(&l->ht_lock);
+                       return NULL;
+               }
        }
        MT_lock_unset(&l->ht_lock);
        return l;
diff --git a/sql/server/rel_exp.c b/sql/server/rel_exp.c
--- a/sql/server/rel_exp.c
+++ b/sql/server/rel_exp.c
@@ -1423,13 +1423,19 @@ exps_bind_column( list *exps, const char
                        MT_lock_set(&exps->ht_lock);
                        if (!exps->ht && list_length(exps) > HASH_MIN_SIZE) {
                                exps->ht = hash_new(exps->sa, 
list_length(exps), (fkeyvalue)&exp_key);
-
+                               if (exps->ht == NULL) {
+                                       MT_lock_unset(&exps->ht_lock);
+                                       return NULL;
+                               }
                                for (en = exps->h; en; en = en->next ) {
                                        sql_exp *e = en->data;
                                        if (e->name) {
                                                int key = exp_key(e);
 
-                                               hash_add(exps->ht, key, e);
+                                               if (hash_add(exps->ht, key, e) 
== NULL) {
+                                                       
MT_lock_unset(&exps->ht_lock);
+                                                       return NULL;
+                                               }
                                        }
                                }
                        }
@@ -1480,13 +1486,20 @@ exps_bind_column2( list *exps, const cha
                        MT_lock_set(&exps->ht_lock);
                        if (!exps->ht && list_length(exps) > HASH_MIN_SIZE) {
                                exps->ht = hash_new(exps->sa, 
list_length(exps), (fkeyvalue)&exp_key);
+                               if (exps->ht == NULL) {
+                                       MT_lock_unset(&exps->ht_lock);
+                                       return NULL;
+                               }
 
                                for (en = exps->h; en; en = en->next ) {
                                        sql_exp *e = en->data;
                                        if (e->name) {
                                                int key = exp_key(e);
 
-                                               hash_add(exps->ht, key, e);
+                                               if (hash_add(exps->ht, key, e) 
== NULL) {
+                                                       
MT_lock_unset(&exps->ht_lock);
+                                                       return NULL;
+                                               }
                                        }
                                }
                        }
diff --git a/sql/storage/sql_catalog.c b/sql/storage/sql_catalog.c
--- a/sql/storage/sql_catalog.c
+++ b/sql/storage/sql_catalog.c
@@ -26,12 +26,19 @@ static void *
                MT_lock_set(&l->ht_lock);
                if ((!l->ht || l->ht->size*16 < list_length(l)) && 
list_length(l) > HASH_MIN_SIZE && l->sa) {
                        l->ht = hash_new(l->sa, list_length(l), 
(fkeyvalue)&base_key);
+                       if (l->ht == NULL) {
+                               MT_lock_unset(&l->ht_lock);
+                               return NULL;
+                       }
 
                        for (n = l->h; n; n = n->next ) {
                                sql_base *b = n->data;
                                int key = base_key(b);
 
-                               hash_add(l->ht, key, b);
+                               if (hash_add(l->ht, key, b) == NULL) {
+                                       MT_lock_unset(&l->ht_lock);
+                                       return NULL;
+                               }
                        }
                }
                if (l->ht) {
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to