[augeas-devel] augeas: master - Do not choke on non-existing lens during save

David Lutterkort lutter at fedoraproject.org
Wed Jan 6 01:53:59 UTC 2010


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=f21048a577af51806a0b91921c19aab2be5bc917
Commit:        f21048a577af51806a0b91921c19aab2be5bc917
Parent:        85da153a2ba8dca7c0c7047526e4431a5ab971bb
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Tue Jan 5 17:24:31 2010 -0800
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Tue Jan 5 17:46:26 2010 -0800

Do not choke on non-existing lens during save

  * src/transform.c (add_file_info): check that lens is non-NULL;
    (lens_from_name): provide detailed error message when lens lookup
    failed; (transform_validate): copy error from lens_from_name into tree
  * tests/test-save.c (testNonExistentLens): new test

Fixes bug #98
---
 src/transform.c   |   32 ++++++++++++++++++++------------
 tests/test-save.c |   21 +++++++++++++++++++++
 2 files changed, 41 insertions(+), 12 deletions(-)

diff --git a/src/transform.c b/src/transform.c
index e55cc5e..2b8677d 100644
--- a/src/transform.c
+++ b/src/transform.c
@@ -36,6 +36,7 @@
 #include "augeas.h"
 #include "syntax.h"
 #include "transform.h"
+#include "errcode.h"
 
 static const int fnm_flags = FNM_PATHNAME;
 static const int glob_flags = GLOB_NOSORT;
@@ -331,6 +332,9 @@ static int add_file_info(struct augeas *aug,
     int end = 0;
     int result = -1;
 
+    if (lens == NULL)
+        return -1;
+
     r = pathjoin(&p, 2, AUGEAS_META_TREE, node);
     if (r < 0)
         goto done;
@@ -436,17 +440,26 @@ static int load_file(struct augeas *aug, struct lens *lens, char *filename) {
  * autoload transform for Module
  */
 static struct lens *lens_from_name(struct augeas *aug, const char *name) {
+    struct lens *result = NULL;
+
     if (name[0] == '@') {
         struct module *modl = NULL;
         for (modl = aug->modules;
              modl != NULL && !streqv(modl->name, name + 1);
              modl = modl->next);
-        if (modl == NULL || modl->autoload == NULL)
-            return NULL;
-        return modl->autoload->lens;
+        ERR_THROW(modl == NULL, aug, AUG_ENOLENS,
+                  "Could not find module %s", name + 1);
+        ERR_THROW(modl->autoload == NULL, aug, AUG_ENOLENS,
+                  "No autoloaded lens in module %s", name + 1);
+        result = modl->autoload->lens;
     } else {
-        return lens_lookup(aug, name);
+        result = lens_lookup(aug, name);
     }
+    ERR_THROW(result == NULL, aug, AUG_ENOLENS,
+              "Can not find lens %s", name);
+    return result;
+ error:
+    return NULL;
 }
 
 static struct lens *xfm_lens(struct augeas *aug, struct tree *xfm) {
@@ -494,14 +507,9 @@ int transform_validate(struct augeas *aug, struct tree *xfm) {
         xfm_error(xfm, "the 'lens' node does not contain a lens name");
         return -1;
     }
-    if (lens_from_name(aug, l->value) == NULL) {
-        char *msg;
-        if (asprintf(&msg, "the lens '%s' does not exist", l->value) < 0) {
-            xfm_error(xfm, "the lens does not exist");
-        } else {
-            xfm_error(xfm, msg);
-            free(msg);
-        }
+    lens_from_name(aug, l->value);
+    if (HAS_ERR(aug)) {
+        xfm_error(xfm, aug->error->details);
         return -1;
     }
     return 0;
diff --git a/tests/test-save.c b/tests/test-save.c
index a1a9052..3bef883 100644
--- a/tests/test-save.c
+++ b/tests/test-save.c
@@ -103,6 +103,26 @@ static void testSaveNewFile(CuTest *tc) {
     CuAssertIntEquals(tc, 1, r);
 }
 
+ATTRIBUTE_UNUSED
+static void testNonExistentLens(CuTest *tc) {
+    int r;
+
+    r = aug_rm(aug, "/augeas/load/*");
+    CuAssertTrue(tc, r >= 0);
+
+    r = aug_set(aug, "/augeas/load/Fake/lens", "Fake.lns");
+    CuAssertIntEquals(tc, 0, r);
+    r = aug_set(aug, "/augeas/load/Fake/incl", "/fake");
+    CuAssertIntEquals(tc, 0, r);
+    r = aug_set(aug, "/files/fake/entry", "value");
+    CuAssertIntEquals(tc, 0, r);
+
+    r = aug_save(aug);
+    CuAssertIntEquals(tc, -1, r);
+    r = aug_error(aug);
+    CuAssertIntEquals(tc, AUG_ENOLENS, r);
+}
+
 int main(void) {
     char *output = NULL;
     CuSuite* suite = CuSuiteNew();
@@ -122,6 +142,7 @@ int main(void) {
     CuSuiteSetup(suite, setup, teardown);
 
     SUITE_ADD_TEST(suite, testSaveNewFile);
+    SUITE_ADD_TEST(suite, testNonExistentLens);
 
     CuSuiteRun(suite);
     CuSuiteSummary(suite, &output);




More information about the augeas-devel mailing list