5 files changed, 97 insertions(+), 21 deletions(-) lenses/tests/test_aptsource.aug | 2 - src/put.c | 62 +++++++++++++++++++++---------- tests/Makefile.am | 3 + tests/modules/pass_put_invalid_star.aug | 15 +++++++ tests/test-bug-1.sh | 36 ++++++++++++++++++
# HG changeset patch # User David Lutterkort <[EMAIL PROTECTED]> # Date 1218143813 25200 # Node ID d389dde421fc17e1c3e5076487c13350cb1c12a8 # Parent c5f757e445fc72d5d2cebaec2dc90e7e7dc237cd Check that 'put' fails when invalid tree entries are present put_quant_star and create_quant_star did not compare the part of the tree they should transform against the part of the tree they actually transformed. This lead to parts of the tree getting ignored during 'put' if the tree contained invalid nodes, and ultimately to files getting truncated. This is now checked and causes a 'Short iteration' error, avoiding file truncation. The fix also uncovered an error in the test for Aptsources.lns, where an invalid tree node was put into the tree, but did not lead to a test failure. Fixes bug https://fedorahosted.org/augeas/ticket/1 diff -r c5f757e445fc -r d389dde421fc lenses/tests/test_aptsource.aug --- a/lenses/tests/test_aptsource.aug Thu Aug 07 14:47:54 2008 -0700 +++ b/lenses/tests/test_aptsource.aug Thu Aug 07 14:16:53 2008 -0700 @@ -60,7 +60,7 @@ (* Should be a noop; makes sure that we preserve the trailing comment *) test Aptsources.lns put trailing_comment after - set "type" "deb" + set "1/type" "deb" = trailing_comment (* Local Variables: *) diff -r c5f757e445fc -r d389dde421fc src/put.c --- a/src/put.c Thu Aug 07 14:47:54 2008 -0700 +++ b/src/put.c Thu Aug 07 14:16:53 2008 -0700 @@ -58,6 +58,7 @@ struct dict *dict; struct skel *skel; char *path; /* Position in the tree, for errors */ + size_t pos; struct lns_error *error; }; @@ -92,7 +93,11 @@ CALLOC(state->error, 1); state->error->lens = ref(lens); state->error->pos = -1; - state->error->path = strdup(state->path); + if (strlen(state->path) == 0) { + state->error->path = strdup("(root)"); + } else { + state->error->path = strdup(state->path); + } va_start(ap, format); r = vasprintf(&state->error->message, format, ap); @@ -143,6 +148,22 @@ sp->start = start; sp->end = end; list_append(*split, sp); +} + +static struct split *next_split(struct state *state) { + if (state->split != NULL) { + state->split = state->split->next; + if (state->split != NULL) + state->pos = state->split->end; + } + return state->split; +} + +static struct split *set_split(struct state *state, struct split *split) { + state->split = split; + if (split != NULL) + state->pos = split->end; + return split; } /* Refine a tree split OUTER according to the L_CONCAT lens LENS */ @@ -396,7 +417,8 @@ state->value = tree->value; pathjoin(&state->path, 1, state->key); - state->split = split = make_split(tree->children); + split = make_split(tree->children); + set_split(state, split); if (entry == NULL) { state->skel = NULL; @@ -449,7 +471,7 @@ struct split *split = split_concat(state, lens); state->skel = state->skel->skels; - state->split = split; + set_split(state, split); for (int i=0; i < lens->nchildren; i++) { if (state->split == NULL) { put_error(state, lens, @@ -458,11 +480,11 @@ return; } put_lens(lens->children[i], state); - state->split = state->split->next; state->skel = state->skel->next; + next_split(state); } list_free(split); - state->split = oldsplit; + set_split(state, oldsplit); state->skel = oldskel; } @@ -473,22 +495,23 @@ struct skel *oldskel = state->skel; struct split *split = split_iter(lens, state->split); - if (split == NULL) - return; state->skel = state->skel->skels; - state->split = split; + set_split(state, split); while (state->split != NULL && state->skel != NULL) { put_lens(lens->child, state); - state->split = state->split->next; state->skel = state->skel->next; + next_split(state); } while (state->split != NULL) { create_lens(lens->child, state); - state->split = state->split->next; + next_split(state); } list_free(split); - state->split = oldsplit; + if (state->pos != oldsplit->end) { + put_error(state, lens, "Short iteration"); + } + set_split(state, oldsplit); state->skel = oldskel; } @@ -592,7 +615,7 @@ struct split *split = split_concat(state, lens); - state->split = split; + set_split(state, split); for (int i=0; i < lens->nchildren; i++) { if (state->split == NULL) { syntax_error(lens->info, "Short split for concat"); @@ -600,10 +623,10 @@ return; } create_lens(lens->children[i], state); - state->split = state->split->next; + next_split(state); } list_free(split); - state->split = oldsplit; + set_split(state, oldsplit); } static void create_quant_star(struct lens *lens, struct state *state) { @@ -611,16 +634,17 @@ struct split *oldsplit = state->split; struct split *split = split_iter(lens, state->split); - if (split == NULL) - return; - state->split = split; + set_split(state, split); while (state->split != NULL) { create_lens(lens->child, state); - state->split = state->split->next; + next_split(state); } list_free(split); - state->split = oldsplit; + if (state->pos != oldsplit->end) { + put_error(state, lens, "Short iteration"); + } + set_split(state, oldsplit); } static void create_quant_maybe(struct lens *lens, struct state *state) { diff -r c5f757e445fc -r d389dde421fc tests/Makefile.am --- a/tests/Makefile.am Thu Aug 07 14:47:54 2008 -0700 +++ b/tests/Makefile.am Thu Aug 07 14:16:53 2008 -0700 @@ -10,7 +10,8 @@ libtool --mode=execute valgrind --quiet --leak-check=full ./fatest check_SCRIPTS=test-lenses.sh test-interpreter.sh test-get.sh \ - test-put-symlink.sh test-save-empty.sh test-mv.sh + test-put-symlink.sh test-save-empty.sh test-mv.sh \ + test-bug-1.sh EXTRA_DIST=augtest $(AUGTESTS) root \ $(check_SCRIPTS) modules diff -r c5f757e445fc -r d389dde421fc tests/modules/pass_put_invalid_star.aug --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/modules/pass_put_invalid_star.aug Thu Aug 07 14:16:53 2008 -0700 @@ -0,0 +1,15 @@ +module Pass_put_invalid_star = + + let lns = [ key /[a-z]+/ . del "=" "=" . store /[0-9]+/ . del "\n" "\n" ]* + + test lns put "a=1\nb=2\n" after + set "c2" "3" + = * + + test lns put "a=1\n" after + insb "x1" "a" ; + set "x1" "1" + = * + + + diff -r c5f757e445fc -r d389dde421fc tests/test-bug-1.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test-bug-1.sh Thu Aug 07 14:16:53 2008 -0700 @@ -0,0 +1,36 @@ +#! /bin/sh + +# Test for bug https://fedorahosted.org/augeas/ticket/1 +# +# Check that putting an invalid node into the tree and saving +# leads to failure, and therefore the original file being preserved + +root=$abs_top_builddir/build/test-bug-1 +file=$root/etc/logrotate.d/test + +rm -rf $root +mkdir -p $(dirname $file) + +cat > $file <<EOF +myfile { + weekly +} +EOF +ln $file $file.orig + +augtool -I $abs_top_srcdir/lenses -r $root > /dev/null <<EOF +ins invalid before /files/etc/logrotate.d/test/rule +save +EOF + +result=$? + +if [ $result -eq 0 ] ; then + echo "augtool succeeded, but should have failed" + exit 1 +fi + +if [ ! $file -ef $file.orig ] ; then + echo "File was changed, but should not have been" + exit 1 +fi _______________________________________________ augeas-devel mailing list [email protected] https://www.redhat.com/mailman/listinfo/augeas-devel
