From: Ralph Corderoy Date: Sun, 16 Oct 2016 13:19:56 +0000 (+0100) Subject: Fix bug where single-character headers were sometimes missed. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/41a82a7ecba04be5b27b3ffc813e2f34d2786a38 Fix bug where single-character headers were sometimes missed. The hash function used to check if a header was of interest always used its first three characters. For a single-character header that third byte wasn't particularly set and so it was pot luck if the two hashes matched, swayed by what was previously in the format string and in the email's headers. Alter it to cope, including with an empty string. Comment that it never gives 127 so leaves the last element of the hash-table array unused. Alter test-header-parsing to stop declaring it expects to fail. --- diff --git a/sbr/fmt_compile.c b/sbr/fmt_compile.c index fbb12742..b1788ab6 100644 --- a/sbr/fmt_compile.c +++ b/sbr/fmt_compile.c @@ -268,13 +268,15 @@ static struct colormap colortable[] = { { NULL, 0, 0 } }; -/* - * Hash function for component name. The function should be - * case independent and probably shouldn't involve a routine - * call. This function is pretty good but will not work on - * single character component names. - */ -#define CHASH(nm) (((((nm)[0]) - ((nm)[1])) & 0x1f) + (((nm)[2]) & 0x5f)) +/* Hash function for component name. Deliberately avoids a function + * call. Is case independent. Covers interval [0, 126] so never uses + * the last element of wantcomp[]. This function is "pretty good". */ +#define CHASH(nm) ( \ + (( \ + ((nm)[0]) - ((nm)[0] ? ((nm)[1]) : 0) \ + ) & 0x1f) + \ + ((nm[1]) ? (((nm)[2]) & 0x5f) : 0) \ + ) /* * Find a component in the hash table. diff --git a/test/scan/test-header-parsing b/test/scan/test-header-parsing index 752fdb9e..093d6772 100755 --- a/test/scan/test-header-parsing +++ b/test/scan/test-header-parsing @@ -25,6 +25,6 @@ sed 's/.*://' "$mail" >"$expected" f="`sed 's/:.*/}/; s/^/%{/' "$mail"`" run_prog scan -forma "$f" -file "$mail" >"$actual" || exit 1 -check "$expected" "$actual" expectfail +check "$expected" "$actual" exit $failed