From b4345f7521cb4c3f4af7894f5d6d840fda85e82f Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Fri, 20 Aug 2021 09:13:01 +0200 Subject: wkd: Fix client issue with leading or trailing spaces in user-ids. * common/recsel.c (recsel_parse_expr): Add flag -t. * common/stringhelp.c: Remove assert.h. (strtokenize): Factor code out to do_strtokenize. (strtokenize_nt): New. (do_strtokenize): Add arg trim to support the strtokenize_nt. * common/t-stringhelp.c (test_strtokenize_nt): New test cases. * tools/wks-util.c (wks_list_key): Use strtokenize_nt and the recsel flag -t. -- This fixes a bug with user ids with leading spaces because: wks-client lists all mail addresses from the key and matches them to the requested mail address. If there are several user-ids all with the same mail address wks-client picks one of them and then extracts exactly that user id. However, here it does not match by the mail address but by the full user-id so that we can be sure that there will be only one user-id in the final key. The filter built expression unfortunately strips leading blanks but requires a verbatim match. Thus it won't find the user id again and errors out. The new -t flag and a non-trimming strtokenize solves the problem. Signed-off-by: Werner Koch --- common/t-stringhelp.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 143 insertions(+), 4 deletions(-) (limited to 'common/t-stringhelp.c') diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c index 6f7f6f7af..5eca5a092 100644 --- a/common/t-stringhelp.c +++ b/common/t-stringhelp.c @@ -1,6 +1,6 @@ /* t-stringhelp.c - Regression tests for stringhelp.c * Copyright (C) 2007 Free Software Foundation, Inc. - * 2015 g10 Code GmbH + * 2015, 2021 g10 Code GmbH * * This file is part of GnuPG. * @@ -27,6 +27,7 @@ * You should have received a copies of the GNU General Public License * and the GNU Lesser General Public License along with this program; * if not, see . + * SPDX-License-Identifier: (LGPL-3.0-or-later OR GPL-2.0-or-later) */ #include @@ -34,7 +35,6 @@ #include #include #include -#include #ifdef HAVE_PWD_H # include #endif @@ -686,6 +686,144 @@ test_strtokenize (void) } +static void +test_strtokenize_nt (void) +{ + struct { + const char *s; + const char *delim; + const char *fields_expected[10]; + } tv[] = { + { + "", ":", + { "", NULL } + }, + { + "a", ":", + { "a", NULL } + }, + { + ":", ":", + { "", "", NULL } + }, + { + "::", ":", + { "", "", "", NULL } + }, + { + "a:b:c", ":", + { "a", "b", "c", NULL } + }, + { + "a:b:", ":", + { "a", "b", "", NULL } + }, + { + "a:b", ":", + { "a", "b", NULL } + }, + { + "aa:b:cd", ":", + { "aa", "b", "cd", NULL } + }, + { + "aa::b:cd", ":", + { "aa", "", "b", "cd", NULL } + }, + { + "::b:cd", ":", + { "", "", "b", "cd", NULL } + }, + { + "aa: : b:cd ", ":", + { "aa", " ", " b", "cd ", NULL } + }, + { + " aa: : b: cd ", ":", + { " aa", " ", " b", " cd ", NULL } + }, + { + " ", ":", + { " ", NULL } + }, + { + " :", ":", + { " ", "", NULL } + }, + { + " : ", ":", + { " ", " ", NULL } + }, + { + ": ", ":", + { "", " ", NULL } + }, + { + ": x ", ":", + { "", " x ", NULL } + }, + { + "a:bc:cde:fghi:jklmn::foo:", ":", + { "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL } + }, + { + ",a,bc,,def,", ",", + { "", "a", "bc", "", "def", "", NULL } + }, + { + " a ", " ", + { "", "a", "", NULL } + }, + { + " ", " ", + { "", "", NULL } + }, + { + "", " ", + { "", NULL } + } + }; + + int tidx; + + for (tidx = 0; tidx < DIM(tv); tidx++) + { + char **fields; + int field_count; + int field_count_expected; + int i; + + for (field_count_expected = 0; + tv[tidx].fields_expected[field_count_expected]; + field_count_expected ++) + ; + + fields = strtokenize_nt (tv[tidx].s, tv[tidx].delim); + if (!fields) + fail (tidx * 1000); + else + { + for (field_count = 0; fields[field_count]; field_count++) + ; + if (field_count != field_count_expected) + fail (tidx * 1000); + else + { + for (i = 0; i < field_count_expected; i++) + if (strcmp (tv[tidx].fields_expected[i], fields[i])) + { + printf ("For field %d, expected '%s', but got '%s'\n", + i, tv[tidx].fields_expected[i], fields[i]); + fail (tidx * 1000 + i + 1); + } + } + } + + xfree (fields); + } +} + + static void test_split_fields (void) { @@ -724,7 +862,7 @@ test_split_fields (void) for (tidx = 0; tidx < DIM(tv); tidx++) { nfields = tv[tidx].nfields; - assert (nfields <= DIM (fields)); + log_assert (nfields <= DIM (fields)); /* Count the fields. */ for (field_count_expected = 0; @@ -799,7 +937,7 @@ test_split_fields_colon (void) for (tidx = 0; tidx < DIM(tv); tidx++) { nfields = tv[tidx].nfields; - assert (nfields <= DIM (fields)); + log_assert (nfields <= DIM (fields)); /* Count the fields. */ for (field_count_expected = 0; @@ -1070,6 +1208,7 @@ main (int argc, char **argv) test_make_absfilename_try (); test_strsplit (); test_strtokenize (); + test_strtokenize_nt (); test_split_fields (); test_split_fields_colon (); test_compare_version_strings (); -- cgit v1.2.3