From db1d45ff1e5f3d83d24e75400a157d5fbc86d1c7 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Sat, 7 Sep 2024 15:36:23 +0100 Subject: [PATCH 1/1] tidying: formatted listmaker --- src/src/daemon.c | 11 ++++++----- src/src/expand.c | 8 +++----- src/src/functions.h | 2 +- src/src/miscmods/arc.c | 3 ++- src/src/string.c | 31 +++++++++++++++++++++++++++---- src/src/tlscert-gnu.c | 5 +++-- src/src/tlscert-openssl.c | 5 +++-- 7 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/src/daemon.c b/src/src/daemon.c index fc8c7fdd2..20e6bc05d 100644 --- a/src/src/daemon.c +++ b/src/src/daemon.c @@ -1976,16 +1976,17 @@ if (f.daemon_listen && !f.inetd_wait_mode) tls_in.on_connect_ports = NULL; sep = 0; while ((s = string_nextinlist(&list, &sep, big_buffer, big_buffer_size))) - { - if (!isdigit(*s)) + if (isdigit(*s)) + g = string_append_listele(g, ':', s); + else { struct servent * smtp_service = getservbyname(CS s, "tcp"); if (!smtp_service) log_write(0, LOG_PANIC_DIE|LOG_CONFIG, "TCP port \"%s\" not found", s); - s = string_sprintf("%d", (int)ntohs(smtp_service->s_port)); + g = string_append_listele_fmt(g, ':', FALSE, "%d", + (int)ntohs(smtp_service->s_port)); } - g = string_append_listele(g, ':', s); - } + if (g) tls_in.on_connect_ports = g->s; break; diff --git a/src/src/expand.c b/src/src/expand.c index 521a30d49..cdfe93cdc 100644 --- a/src/src/expand.c +++ b/src/src/expand.c @@ -5535,8 +5535,7 @@ while (*s) /* First option has no tag and is timeout */ if ((item = string_nextinlist(&list, &sep, NULL, 0))) - g = string_append_listele(g, ',', - string_sprintf("timeout=%s", item)); + g = string_append_listele_fmt(g, ',', TRUE, "timeout=%s", item); /* The rest of the options from the expansion */ while ((item = string_nextinlist(&list, &sep, NULL, 0))) @@ -5547,9 +5546,8 @@ while (*s) options is the readsock expansion. */ if (sub_arg[3] && *sub_arg[3]) - g = string_append_listele(g, ',', - string_sprintf("eol=%s", - string_printing2(sub_arg[3], SP_TAB|SP_SPACE))); + g = string_append_listele_fmt(g, ',', TRUE, + "eol=%s", string_printing2(sub_arg[3], SP_TAB|SP_SPACE)); } /* Gat a (possibly cached) handle for the connection */ diff --git a/src/src/functions.h b/src/src/functions.h index ac0c31097..493b2287e 100644 --- a/src/src/functions.h +++ b/src/src/functions.h @@ -555,7 +555,7 @@ extern void store_writeprotect(int); extern gstring *string_append(gstring *, int, ...) WARN_UNUSED_RESULT; extern gstring *string_append_listele(gstring *, uschar, const uschar *) WARN_UNUSED_RESULT; extern gstring *string_append_listele_n(gstring *, uschar, const uschar *, unsigned) WARN_UNUSED_RESULT; -extern gstring *string_append_listele_fmt(gstring *, uschar, const char *, ...) WARN_UNUSED_RESULT; +extern gstring *string_append_listele_fmt(gstring *, uschar, BOOL, const char *, ...) WARN_UNUSED_RESULT; extern gstring *string_append2_listele_n(gstring *, const uschar *, const uschar *, unsigned) WARN_UNUSED_RESULT; extern uschar *string_base62_32(unsigned long int); extern uschar *string_base62_64(unsigned long int); diff --git a/src/src/miscmods/arc.c b/src/src/miscmods/arc.c index a48d1987b..b35c17277 100644 --- a/src/src/miscmods/arc.c +++ b/src/src/miscmods/arc.c @@ -2105,7 +2105,8 @@ if (arc_state) arc_line * line = as->hdr_as; if (line) { - g = string_append_listele_fmt(g, ',', " (\"i\":%u" /*)*/ + g = string_append_listele_fmt(g, ',', FALSE, + " (\"i\":%u" /*)*/ ", \"d\":\"%#b\"" ", \"s\":\"%#b\"", as->instance, &line->d, &line->s); diff --git a/src/src/string.c b/src/src/string.c index 33c0332aa..1169f0e2c 100644 --- a/src/src/string.c +++ b/src/src/string.c @@ -1093,22 +1093,45 @@ return list; /* Listmaker that takes a format string and args for the element. -Currently no checking of the element content for sep chars */ +A flag arg is required to handle embedded sep chars in the (expanded) element; +if false then no check is done */ gstring * -string_append_listele_fmt(gstring * list, uschar sep, const char * fmt, ...) +string_append_listele_fmt(gstring * list, uschar sep, BOOL check, + const char * fmt, ...) { +va_list ap; +unsigned start; +gstring * g; + if (list && list->ptr) + { list = string_catn(list, &sep, 1); + start = list->ptr; + } +else + start = 0; -va_list ap; va_start(ap, fmt); list = string_vformat_trc(list, US __FUNCTION__, __LINE__, STRING_SPRINTF_BUFFER_SIZE, SVFMT_REBUFFER|SVFMT_EXTEND, fmt, ap); va_end(ap); (void) string_from_gstring(list); -return list; + +/* if the appended element turns out to have an embedded sep char, rewind +and do the lazy-coded separate string method */ + +if (!check || !Ustrchr(&list->s[start], sep)) + return list; + +va_start(ap, fmt); +g = string_vformat_trc(NULL, US __FUNCTION__, __LINE__, + STRING_SPRINTF_BUFFER_SIZE, SVFMT_REBUFFER|SVFMT_EXTEND, fmt, ap); +va_end(ap); + +list->ptr = start; +return string_append_listele_n(list, sep, g->s, g->ptr); } diff --git a/src/src/tlscert-gnu.c b/src/src/tlscert-gnu.c index a3f6d4434..b130ef06f 100644 --- a/src/src/tlscert-gnu.c +++ b/src/src/tlscert-gnu.c @@ -334,8 +334,9 @@ for (int index = 0;; index++) case GNUTLS_SAN_RFC822NAME: tag = US"MAIL"; break; default: continue; /* ignore unrecognised types */ } - list = string_append_listele(list, sep, - match == -1 ? string_sprintf("%s=%s", tag, ele) : ele); + list = match == -1 + ? string_append_listele_fmt(list, sep, TRUE, "%s=%s", tag, ele) + : string_append_listele(list, sep, ele); } /*NOTREACHED*/ } diff --git a/src/src/tlscert-openssl.c b/src/src/tlscert-openssl.c index 343f3d3fc..d4b9de359 100644 --- a/src/src/tlscert-openssl.c +++ b/src/src/tlscert-openssl.c @@ -411,8 +411,9 @@ while (sk_GENERAL_NAME_num(san) > 0) ele = string_copyn(ele, len); if (Ustrlen(ele) == len) /* ignore any with embedded nul */ - list = string_append_listele(list, osep, - match == -1 ? string_sprintf("%s=%s", tag, ele) : ele); + list = match == -1 + ? string_append_listele_fmt(list, osep, TRUE, "%s=%s", tag, ele) + : string_append_listele(list, osep, ele); } sk_GENERAL_NAME_free(san); -- 2.30.2