From: Daniel Lenski Date: Sat, 20 Feb 2021 17:39:07 +0000 (-0800) Subject: add openconnect__strchrnul function to compat.c X-Git-Tag: v8.20~325^2~13 X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=3c4320f42417af582fc61ffdc62aa1a383a610eb;p=users%2Fdwmw2%2Fopenconnect.git 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 --- diff --git a/fortinet.c b/fortinet.c index b551465c..467fdf7d 100644 --- a/fortinet.c +++ b/fortinet.c @@ -67,13 +67,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, query_sep) ? : f+strlen(f); + endf = strchrnul(f, query_sep); 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; } @@ -115,8 +115,8 @@ int fortinet_obtain_cookie(struct openconnect_info *vpninfo) */ for (realm = strchr(vpninfo->urlpath, '?'); realm && *++realm; realm=strchr(realm, '&')) { if (!strncmp(realm, "realm=", 6)) { - const char *end = strchr(realm+1, '&'); - realm = end ? strndup(realm+6, end-realm) : strdup(realm+6); + const char *end = strchrnul(realm+1, '&'); + realm = strndup(realm+6, end-realm); vpn_progress(vpninfo, PRG_INFO, _("Got login realm '%s'\n"), realm); break; } @@ -221,12 +221,10 @@ int fortinet_obtain_cookie(struct openconnect_info *vpninfo) buf_free(action_buf); if ((prompt = strstr(form_buf, ",chal_msg="))) { - char *end = strchr(prompt, ','); - if (end) - *end = '\0'; + char *end = strchrnul(prompt, ','); prompt += 10; free(form->message); - form->message = strdup(prompt); + form->message = strndup(prompt, end-prompt); } } }