[augeas-devel] augeas: master - * src/get.c: check for valid match before calling token()

David Lutterkort lutter at fedoraproject.org
Sat Feb 21 06:36:25 UTC 2009


Gitweb:        http://git.fedorahosted.org/git/augeas.git?p=augeas.git;a=commitdiff;h=7518841124128ed0ca917ea61c375d8531b44155
Commit:        7518841124128ed0ca917ea61c375d8531b44155
Parent:        e6ebc1c7fabb4da598ff516b77903f8beef12ebb
Author:        David Lutterkort <lutter at redhat.com>
AuthorDate:    Fri Feb 20 22:20:22 2009 -0800
Committer:     David Lutterkort <lutter at redhat.com>
CommitterDate: Fri Feb 20 22:29:53 2009 -0800

* src/get.c: check for valid match before calling token()

---
 src/get.c |   44 ++++++++++++++++++++++++++++++++++++--------
 1 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/src/get.c b/src/get.c
index 44d10c9..47e4210 100644
--- a/src/get.c
+++ b/src/get.c
@@ -208,6 +208,7 @@ static void get_expected_error(struct state *state, struct lens *l) {
  */
 
 static char *token(struct state *state) {
+    assert(REG_MATCHED(state));
     return strndup(REG_POS(state), REG_SIZE(state));
 }
 
@@ -229,6 +230,23 @@ static void regexp_match_error(struct state *state, struct lens *lens,
     free(text);
 }
 
+static void no_match_error(struct state *state, struct lens *lens) {
+    assert(lens->tag == L_KEY || lens->tag == L_DEL
+           || lens->tag == L_STORE);
+    char *pat = regexp_escape(lens->ctype);
+    const char *lname;
+    if (lens->tag == L_KEY)
+        lname = "key";
+    else if (lens->tag == L_DEL)
+        lname = "del";
+    else if (lens->tag == L_STORE)
+        lname = "store";
+    else
+        assert(0);
+    get_error(state, lens, "no match for %s /%s/", lname, pat);
+    free(pat);
+}
+
 /* Modifies STATE->REGS and STATE->NREG. The caller must save these
  * if they are still needed
  *
@@ -316,10 +334,13 @@ static struct skel *parse_counter(struct lens *lens, struct state *state) {
     return make_skel(lens);
 }
 
-static struct tree *get_del(struct lens *lens,
-                            ATTRIBUTE_UNUSED struct state *state) {
+static struct tree *get_del(struct lens *lens, struct state *state) {
     assert(lens->tag == L_DEL);
-
+    if (! REG_MATCHED(state)) {
+        char *pat = regexp_escape(lens->ctype);
+        get_error(state, lens, "no match for del /%s/", pat);
+        free(pat);
+    }
     return NULL;
 }
 
@@ -328,7 +349,10 @@ static struct skel *parse_del(struct lens *lens, struct state *state) {
     struct skel *skel = NULL;
 
     skel = make_skel(lens);
-    skel->text = token(state);
+    if (! REG_MATCHED(state))
+        no_match_error(state, lens);
+    else
+        skel->text = token(state);
     return skel;
 }
 
@@ -337,11 +361,12 @@ static struct tree *get_store(struct lens *lens, struct state *state) {
     struct tree *tree = NULL;
 
     assert(state->value == NULL);
-    if (state->value != NULL) {
+    if (state->value != NULL)
         get_error(state, lens, "More than one store in a subtree");
-    } else {
+    else if (! REG_MATCHED(state))
+        no_match_error(state, lens);
+    else
         state->value = token(state);
-    }
     return tree;
 }
 
@@ -353,7 +378,10 @@ static struct skel *parse_store(struct lens *lens,
 
 static struct tree *get_key(struct lens *lens, struct state *state) {
     assert(lens->tag == L_KEY);
-    state->key = token(state);
+    if (! REG_MATCHED(state))
+        no_match_error(state, lens);
+    else
+        state->key = token(state);
     return NULL;
 }
 




More information about the augeas-devel mailing list