]> sourceware.org Git - lvm2.git/commitdiff
Add checks for allocation errors in config node clonning.
authorZdenek Kabelac <zkabelac@redhat.com>
Mon, 20 Dec 2010 13:53:10 +0000 (13:53 +0000)
committerZdenek Kabelac <zkabelac@redhat.com>
Mon, 20 Dec 2010 13:53:10 +0000 (13:53 +0000)
Add checks for clonning allocation a fail-out when something is
not allocated correctly.

Also move var declaration to the begining of the function
and fix log_error messages.

WHATS_NEW
lib/config/config.c

index 9e809a1a4940b2e639d7cbb07d256b385fc76093..f7691193b3584f7ec39f70a9189ed74ac6c295bd 100644 (file)
--- a/WHATS_NEW
+++ b/WHATS_NEW
@@ -1,5 +1,6 @@
 Version 2.02.79 -  
 ===================================
+  Add checks for allocation errors in config node clonning.
   Fix error path if regex engine cannot be created in _build_matcher().
   Use char* arithmetic in target_version(), _process_all(), _targets().
   Fixing const cast gcc warnings in the code.
index 1df8d09e964dc3e7e391565536aac321dde45242..72908f2a3ede350405087c04f8896b173a9d7716 100644 (file)
@@ -1330,30 +1330,53 @@ unsigned maybe_config_section(const char *str, unsigned len)
 
 static struct config_value *_clone_config_value(struct dm_pool *mem, const struct config_value *v)
 {
+       struct config_value *new_cv;
+
        if (!v)
                return NULL;
-       struct config_value *new = _create_value(mem);
-       new->type = v->type;
-       if (v->type == CFG_STRING)
-               new->v.str = dm_pool_strdup(mem, v->v.str);
-       else
-               new->v = v->v;
-       new->next = _clone_config_value(mem, v->next);
-       return new;
+
+       if (!(new_cv = _create_value(mem))) {
+               log_error("Failed to clone config value.");
+               return NULL;
+       }
+
+       new_cv->type = v->type;
+       if (v->type == CFG_STRING) {
+               if (!(new_cv->v.str = dm_pool_strdup(mem, v->v.str))) {
+                       log_error("Failed to clone config string value.");
+                       return NULL;
+               }
+       } else
+               new_cv->v = v->v;
+
+       if (v->next && !(new_cv->next = _clone_config_value(mem, v->next)))
+               return_NULL;
+
+       return new_cv;
 }
 
 struct config_node *clone_config_node(struct dm_pool *mem, const struct config_node *cn,
                                      int siblings)
 {
+       struct config_node *new_cn;
+
        if (!cn)
                return NULL;
-       struct config_node *new = _create_node(mem);
-       new->key = dm_pool_strdup(mem, cn->key);
-       new->child = clone_config_node(mem, cn->child, 1);
-       new->v = _clone_config_value(mem, cn->v);
-       if (siblings)
-               new->sib = clone_config_node(mem, cn->sib, siblings);
-       else
-               new->sib = NULL;
-       return new;
+
+       if (!(new_cn = _create_node(mem))) {
+               log_error("Failed to clone config node.");
+               return NULL;
+       }
+
+       if ((cn->key && !(new_cn->key = dm_pool_strdup(mem, cn->key)))) {
+               log_error("Failed to clone config node key.");
+               return NULL;
+       }
+
+       if ((cn->v && !(new_cn->v = _clone_config_value(mem, cn->v))) ||
+           (cn->child && !(new_cn->child = clone_config_node(mem, cn->child, 1))) ||
+           (siblings && cn->sib && !(new_cn->sib = clone_config_node(mem, cn->sib, siblings))))
+               return_NULL; /* 'new_cn' released with mem pool */
+
+       return new_cn;
 }
This page took 0.045704 seconds and 5 git commands to generate.