From e6ce815e112415e8a00cf838bcaf9121be3503de Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Sat, 20 Feb 2021 09:39:07 -0800 Subject: [PATCH] add openconnect__strchrnul function to compat.c GNU strchrnul() is trivial to implement, and makes a bunch of string parsing functions simpler and less error-prone. Signed-off-by: Daniel Lenski --- compat.c | 8 ++++++++ configure.ac | 1 + gpst.c | 4 ++-- openconnect-internal.h | 5 +++++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/compat.c b/compat.c index 14e7202f..5a9406ca 100644 --- a/compat.c +++ b/compat.c @@ -202,6 +202,14 @@ char *openconnect__strndup(const char *s, size_t n) } #endif +#ifndef HAVE_STRCHRNUL +const char *openconnect__strchrnul(const char *s, int c) +{ + while (*s && *s++ != c); + return s; +} +#endif + #ifndef HAVE_INET_ATON /* XX: unlike "real" inet_aton(), inet_pton() only accepts dotted-decimal notation, not * looser/rarer formats like 32-bit decimal values. For example, inet_aton() accepts both diff --git a/configure.ac b/configure.ac index 8dc46994..6133ae23 100644 --- a/configure.ac +++ b/configure.ac @@ -141,6 +141,7 @@ AC_CHECK_FUNC(getline, [AC_DEFINE(HAVE_GETLINE, 1, [Have getline() function])], [symver_getline="openconnect__getline;"]) AC_CHECK_FUNC(strcasestr, [AC_DEFINE(HAVE_STRCASESTR, 1, [Have strcasestr() function])], []) AC_CHECK_FUNC(strndup, [AC_DEFINE(HAVE_STRNDUP, 1, [Have strndup() function])], []) +AC_CHECK_FUNC(strchrnul, [AC_DEFINE(HAVE_STRCHRNUL, 1, [Have strchrnul() function])], []) AC_CHECK_FUNC(asprintf, [AC_DEFINE(HAVE_ASPRINTF, 1, [Have asprintf() function])], [symver_asprintf="openconnect__asprintf;"]) AC_CHECK_FUNC(vasprintf, [AC_DEFINE(HAVE_VASPRINTF, 1, [Have vasprintf() function])], diff --git a/gpst.c b/gpst.c index 345d5143..17903d18 100644 --- a/gpst.c +++ b/gpst.c @@ -105,13 +105,13 @@ static int filter_opts(struct oc_text_buf *buf, const char *query, const char *i const char *found, *comma; for (f = query; *f; f=(*endf) ? endf+1 : endf) { - endf = strchr(f, '&') ? : f+strlen(f); + endf = strchrnul(f, '&'); eq = strchr(f, '='); if (!eq || eq > endf) eq = endf; for (found = incexc; *found; found=(*comma) ? comma+1 : comma) { - comma = strchr(found, ',') ? : found+strlen(found); + comma = strchrnul(found, ','); if (!strncmp(found, f, MAX(comma-found, eq-f))) break; } diff --git a/openconnect-internal.h b/openconnect-internal.h index 974248dc..feaff658 100644 --- a/openconnect-internal.h +++ b/openconnect-internal.h @@ -788,6 +788,11 @@ char *openconnect__strcasestr(const char *haystack, const char *needle); #define strndup openconnect__strndup char *openconnect__strndup(const char *s, size_t n); #endif +#ifndef HAVE_STRCHRNUL +#undef strchrnul +#define strchrnul openconnect__strchrnul +const char *openconnect__strchrnul(const char *s, int c); +#endif #ifndef HAVE_INET_ATON #define inet_aton openconnect__inet_aton -- 2.49.0