[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[augeas-devel] augeas: master - * src/pathx.c (expand_tree): ensure we start with a nonempty lp_trace



Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=b58b000de577a818c04a1961428d8831a9f94083
Commit:        b58b000de577a818c04a1961428d8831a9f94083
Parent:        f14e32d980fe181e0199655a5f3797016cf0568a
Author:        David Lutterkort <lutter redhat com>
AuthorDate:    Mon Mar 23 18:17:23 2009 -0700
Committer:     David Lutterkort <lutter redhat com>
CommitterDate: Tue Mar 24 15:54:01 2009 -0700

* src/pathx.c (expand_tree): ensure we start with a nonempty lp_trace

---
 src/pathx.c        |   20 ++++++++++++++++----
 tests/test-xpath.c |   21 +++++++++++++++++++++
 2 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/src/pathx.c b/src/pathx.c
index c9f6dbe..3fe9e96 100644
--- a/src/pathx.c
+++ b/src/pathx.c
@@ -41,7 +41,8 @@ static const char *const errcodes[] = {
     "internal error",   /* PATHX_EINTERNAL */
     "type error",       /* PATHX_ETYPE */
     "undefined variable",               /* PATHX_ENOVAR */
-    "garbage at end of path expression" /* PATHX_EEND */
+    "garbage at end of path expression",/* PATHX_EEND */
+    "can not expand tree from empty nodeset"  /* PATHX_ENONODES */
 };
 
 /*
@@ -2066,14 +2067,27 @@ int pathx_expand_tree(struct pathx *path, struct tree **tree) {
     int r;
     struct step *step;
     struct locpath_trace lpt;
+    struct tree *first_child = NULL;
+    struct value *v = NULL;
 
     MEMZERO(&lpt, 1);
     path->state->locpath_trace = &lpt;
-    pathx_eval(path);
+    v = pathx_eval(path);
     path->state->locpath_trace = NULL;
     if (HAS_ERROR(path->state))
         goto error;
 
+    if (lpt.maxns == 0) {
+        if (v->tag != T_NODESET || v->nodeset->used == 0) {
+            STATE_ERROR(path->state, PATHX_ENONODES);
+            goto error;
+        }
+        if (v->nodeset->used > 1)
+            goto error;
+        *tree = v->nodeset->nodes[0];
+        return 0;
+    }
+
     *tree = path->origin;
     r = locpath_search(&lpt, tree, &step);
     if (r == -1)
@@ -2082,9 +2096,7 @@ int pathx_expand_tree(struct pathx *path, struct tree **tree) {
     if (step == NULL)
         return 0;
 
-    struct tree *first_child = NULL;
     struct tree *parent = *tree;
-
     if (parent == NULL)
         parent = path->origin;
 
diff --git a/tests/test-xpath.c b/tests/test-xpath.c
index d972738..9cfb8ab 100644
--- a/tests/test-xpath.c
+++ b/tests/test-xpath.c
@@ -226,6 +226,24 @@ static int test_rm_var(struct augeas *aug) {
     return -1;
 }
 
+static int test_defvar_nonexistent(struct augeas *aug) {
+    int r;
+
+    printf("%-30s ... ", "defvar_nonexistent");
+    r = aug_defvar(aug, "x", "/foo/bar");
+    if (r < 0)
+        die("aug_defvar failed");
+
+    r = aug_set(aug, "$x", "baz");
+    if (r != -1)
+        goto fail;
+    printf("PASS\n");
+    return 0;
+ fail:
+    printf("FAIL\n");
+    return -1;
+}
+
 static int run_tests(struct test *tests) {
     char *lensdir;
     struct augeas *aug = NULL;
@@ -252,6 +270,9 @@ static int run_tests(struct test *tests) {
     if (test_rm_var(aug) < 0)
         result = EXIT_FAILURE;
 
+    if (test_defvar_nonexistent(aug) < 0)
+        result = EXIT_FAILURE;
+
     aug_close(aug);
 
     return result;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]