From 4f653e8f897068fc803607a5975fa18ab1519bcf Mon Sep 17 00:00:00 2001 From: Peter Maydell Date: Sun, 3 Aug 2008 13:32:17 +0000 Subject: [PATCH] When doing an if-test on the result of a function which returns a string result, check whether the string is non-empty (as the documentation says we do). Previously we were always testing the integer 'value'. --- ChangeLog | 9 +++++++++ sbr/fmt_compile.c | 12 +++++++++++- test/tests/repl/test-if-str | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 test/tests/repl/test-if-str diff --git a/ChangeLog b/ChangeLog index 7e6b2eb..d76efca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-08-03 Peter Maydell + + * sbr/fmt_compile.c: when doing an if-test on the result + of a function which returns a string result, check whether + the string is non-empty (as the documentation says we do). + Previously we were always testing the integer 'value'. Bug + spotted by Eric Gillespie. + * test/tests/repl/test-if-str: test case for this bug. + 2008-07-24 Eric Gillespie * test/setup-test: Run 'make clean' before building diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index d699916..c42f1e3 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -53,6 +53,9 @@ extern struct mailname fmt_mnull; #define TF_NOP 8 /* like expr but no result */ /* ftable->flags */ +/* NB that TFL_PUTS is also used to decide whether the test + * in a "%<(function)..." should be a string or numeric one. + */ #define TFL_PUTS 1 /* implicit putstr if top level */ #define TFL_PUTN 2 /* implicit putnum if top level */ @@ -610,7 +613,14 @@ do_if(char *sp) if (ftbl->f_type >= IF_FUNCS) fp->f_type = ftbl->extra; else { - LV (FT_IF_V_NE, 0); + /* Put out a string test or a value test depending + * on what this function's return type is. + */ + if (ftbl->flags & TFL_PUTS) { + LV (FT_IF_S, 0); + } else { + LV (FT_IF_V_NE, 0); + } } } else { diff --git a/test/tests/repl/test-if-str b/test/tests/repl/test-if-str new file mode 100644 index 0000000..7582a7d --- /dev/null +++ b/test/tests/repl/test-if-str @@ -0,0 +1,36 @@ +#!/bin/sh +###################################################### +# +# Test that an '%<(function)...' if-construct correctly +# tests whether 'str' is empty if the function returns +# a string, and tests 'value' if the function returns +# an integer. +# +###################################################### + +# create test replgroupcomps +form=$MH_TEST_DIR/$$.replgroupcomps +cat > $form <\n\ +X-NUMBER: %(num)%(lit)%<(num 3) yes%| no%>\n\ +X-NOSTRING: %(num 3)%(lit x)%<(lit) found%| missing%>\n\ +X-NONUMBER: %(num 3)%(lit x)%<(num) yes%| no%>\n\ +-------- +EOF + +expected=$MH_TEST_DIR/$$.expected +actual=$MH_TEST_DIR/Mail/draft + +cat > $expected < This is message number 1 +EOF + +repl -editor true -format -form $form -group -nocc me -nowhatnowproc 1 + +diff -u $expected $actual -- 1.7.10.4