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/stringhelp.c | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'common/stringhelp.c') diff --git a/common/stringhelp.c b/common/stringhelp.c index 24524e805..4dc179738 100644 --- a/common/stringhelp.c +++ b/common/stringhelp.c @@ -2,7 +2,7 @@ * Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, * 2008, 2009, 2010 Free Software Foundation, Inc. * Copyright (C) 2014 Werner Koch - * Copyright (C) 2015 g10 Code GmbH + * Copyright (C) 2015, 2021 g10 Code GmbH * * This file is part of GnuPG. * @@ -29,6 +29,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 @@ -48,7 +49,6 @@ # endif # include #endif -#include #include #include "util.h" @@ -216,7 +216,7 @@ trim_spaces( char *str ) } -/* Same as trim_spaces but only condider, space, tab, cr and lf as space. */ +/* Same as trim_spaces but only consider, space, tab, cr and lf as space. */ char * ascii_trim_spaces (char *str) { @@ -1293,8 +1293,8 @@ strsplit (char *string, char delim, char replacement, int *count) * Returns: A malloced and NULL delimited array with the tokens. On * memory error NULL is returned and ERRNO is set. */ -char ** -strtokenize (const char *string, const char *delim) +static char ** +do_strtokenize (const char *string, const char *delim, int trim) { const char *s; size_t fields; @@ -1333,24 +1333,51 @@ strtokenize (const char *string, const char *delim) for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1) { *pend = 0; + if (trim) + { + while (spacep (p)) + p++; + for (px = pend - 1; px >= p && spacep (px); px--) + *px = 0; + } + result[n++] = p; + } + if (trim) + { while (spacep (p)) p++; - for (px = pend - 1; px >= p && spacep (px); px--) + for (px = p + strlen (p) - 1; px >= p && spacep (px); px--) *px = 0; - result[n++] = p; } - while (spacep (p)) - p++; - for (px = p + strlen (p) - 1; px >= p && spacep (px); px--) - *px = 0; result[n++] = p; result[n] = NULL; - assert ((char*)(result + n + 1) == buffer); + log_assert ((char*)(result + n + 1) == buffer); return result; } +/* Tokenize STRING using the set of delimiters in DELIM. Leading + * spaces and tabs are removed from all tokens. The caller must xfree + * the result. + * + * Returns: A malloced and NULL delimited array with the tokens. On + * memory error NULL is returned and ERRNO is set. + */ +char ** +strtokenize (const char *string, const char *delim) +{ + return do_strtokenize (string, delim, 1); +} + +/* Same as strtokenize but does not trim leading and trailing spaces + * from the fields. */ +char ** +strtokenize_nt (const char *string, const char *delim) +{ + return do_strtokenize (string, delim, 0); +} + /* Split a string into space delimited fields and remove leading and * trailing spaces from each field. A pointer to each field is stored -- cgit v1.2.3