From d12746bc15d83ab821be36975da0179672708bc1 Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Wed, 14 Nov 2018 22:32:58 +0000 Subject: [PATCH] Recast more internal string routines to use growable-strings --- src/src/acl.c | 13 +- src/src/auths/heimdal_gssapi.c | 23 +- src/src/debug.c | 28 ++- src/src/deliver.c | 31 ++- src/src/dummies.c | 20 +- src/src/exim.c | 21 +- src/src/functions.h | 3 +- src/src/header.c | 6 +- src/src/host.c | 14 +- src/src/log.c | 157 +++++++------- src/src/smtp_in.c | 22 +- src/src/smtp_out.c | 20 +- src/src/string.c | 382 +++++++++++++++++++-------------- src/src/transport.c | 6 +- src/src/transports/lmtp.c | 13 +- 15 files changed, 426 insertions(+), 333 deletions(-) diff --git a/src/src/acl.c b/src/src/acl.c index 14079a655..f3b860e4a 100644 --- a/src/src/acl.c +++ b/src/src/acl.c @@ -2173,14 +2173,15 @@ static int ratelimit_error(uschar **log_msgptr, const char *format, ...) { va_list ap; -uschar buffer[STRING_SPRINTF_BUFFER_SIZE]; +gstring * g = + string_cat(NULL, US"error in arguments to \"ratelimit\" condition: "); + va_start(ap, format); -if (!string_vformat(buffer, sizeof(buffer), format, ap)) - log_write(0, LOG_MAIN|LOG_PANIC_DIE, - "string_sprintf expansion was longer than " SIZE_T_FMT, sizeof(buffer)); +g = string_vformat(g, TRUE, format, ap); va_end(ap); -*log_msgptr = string_sprintf( - "error in arguments to \"ratelimit\" condition: %s", buffer); + +gstring_reset_unused(g); +*log_msgptr = string_from_gstring(g); return ERROR; } diff --git a/src/src/auths/heimdal_gssapi.c b/src/src/auths/heimdal_gssapi.c index 381898050..11a7d399d 100644 --- a/src/src/auths/heimdal_gssapi.c +++ b/src/src/auths/heimdal_gssapi.c @@ -534,31 +534,30 @@ exim_gssapi_error_defer(uschar *store_reset_point, const char *format, ...) { va_list ap; - uschar buffer[STRING_SPRINTF_BUFFER_SIZE]; OM_uint32 maj_stat, min_stat; OM_uint32 msgcontext = 0; gss_buffer_desc status_string; + gstring * g; - va_start(ap, format); - if (!string_vformat(buffer, sizeof(buffer), format, ap)) - log_write(0, LOG_MAIN|LOG_PANIC_DIE, - "exim_gssapi_error_defer expansion larger than %lu", - sizeof(buffer)); - va_end(ap); + HDEBUG(D_auth) + { + va_start(ap, format); + g = string_vformat(NULL, TRUE, format, ap); + va_end(ap); + } auth_defer_msg = NULL; do { maj_stat = gss_display_status(&min_stat, - major, GSS_C_GSS_CODE, GSS_C_NO_OID, - &msgcontext, &status_string); + major, GSS_C_GSS_CODE, GSS_C_NO_OID, &msgcontext, &status_string); - if (auth_defer_msg == NULL) { + if (!auth_defer_msg) auth_defer_msg = string_copy(US status_string.value); - } HDEBUG(D_auth) debug_printf("heimdal %s: %.*s\n", - buffer, (int)status_string.length, CS status_string.value); + string_from_gstring(g), (int)status_string.length, + CS status_string.value); gss_release_buffer(&min_stat, &status_string); } while (msgcontext != 0); diff --git a/src/src/debug.c b/src/src/debug.c index 6aa2c41cb..2423a3347 100644 --- a/src/src/debug.c +++ b/src/src/debug.c @@ -233,19 +233,27 @@ if (indent > 0) /* Use the checked formatting routine to ensure that the buffer does not overflow. Ensure there's space for a newline at the end. */ -if (!string_vformat(debug_ptr, - sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1, format, ap)) { - uschar *s = US"**** debug string too long - truncated ****\n"; - uschar *p = debug_buffer + Ustrlen(debug_buffer); - int maxlen = sizeof(debug_buffer) - Ustrlen(s) - 3; - if (p > debug_buffer + maxlen) p = debug_buffer + maxlen; - if (p > debug_buffer && p[-1] != '\n') *p++ = '\n'; - Ustrcpy(p, s); + gstring gs = { .size = (int)sizeof(debug_buffer) - 1, + .ptr = debug_ptr - debug_buffer, + .s = debug_buffer }; + if (!string_vformat(&gs, FALSE, format, ap)) + { + uschar * s = US"**** debug string too long - truncated ****\n"; + uschar * p = gs.s + gs.ptr; + int maxlen = gs.size - Ustrlen(s) - 2; + if (p > gs.s + maxlen) p = gs.s + maxlen; + if (p > gs.s && p[-1] != '\n') *p++ = '\n'; + Ustrcpy(p, s); + while(*debug_ptr) debug_ptr++; + } + else + { + string_from_gstring(&gs); + debug_ptr = gs.s + gs.ptr; + } } -while(*debug_ptr) debug_ptr++; - /* Output the line if it is complete. If we added any prefix data and there are internal newlines, make sure the prefix is on the continuation lines, as long as there is room in the buffer. We want to do just a single fprintf() diff --git a/src/src/deliver.c b/src/src/deliver.c index 9f292fc53..0b403bad2 100644 --- a/src/src/deliver.c +++ b/src/src/deliver.c @@ -761,10 +761,9 @@ d_log_interface(gstring * g) if (LOGGING(incoming_interface) && LOGGING(outgoing_interface) && sending_ip_address) { - g = string_append(g, 2, US" I=[", sending_ip_address); - g = LOGGING(outgoing_port) - ? string_append(g, 2, US"]:", string_sprintf("%d", sending_port)) - : string_catn(g, US"]", 1); + g = string_fmt_append(g, " I=[%s]", sending_ip_address); + if (LOGGING(outgoing_port)) + g = string_fmt_append(g, "%d", sending_port); } return g; } @@ -784,14 +783,14 @@ if (LOGGING(dnssec) && h->dnssec == DS_YES) g = string_append(g, 3, US" [", h->address, US"]"); if (LOGGING(outgoing_port)) - g = string_append(g, 2, US":", string_sprintf("%d", h->port)); + g = string_fmt_append(g, ":%d", h->port); #ifdef SUPPORT_SOCKS if (LOGGING(proxy) && proxy_local_address) { g = string_append(g, 3, US" PRX=[", proxy_local_address, US"]"); if (LOGGING(outgoing_port)) - g = string_append(g, 2, US":", string_sprintf("%d", proxy_local_port)); + g = string_fmt_append(g, ":%d", proxy_local_port); } #endif @@ -1196,8 +1195,7 @@ if (addr->router) g = string_append(g, 2, US" T=", addr->transport->name); if (LOGGING(delivery_size)) - g = string_append(g, 2, US" S=", - string_sprintf("%d", transport_count)); + g = string_fmt_append(g, " S=%d", transport_count); /* Local delivery */ @@ -1345,13 +1343,12 @@ if (driver_name) { if (driver_kind[1] == 't' && addr->router) g = string_append(g, 2, US" R=", addr->router->name); - g = string_cat(g, string_sprintf(" %c=%s", toupper(driver_kind[1]), driver_name)); + g = string_fmt_append(g, " %c=%s", toupper(driver_kind[1]), driver_name); } else if (driver_kind) g = string_append(g, 2, US" ", driver_kind); -/*XXX need an s+s+p sprintf */ -g = string_cat(g, string_sprintf(" defer (%d)", addr->basic_errno)); +g = string_fmt_append(g, " defer (%d)", addr->basic_errno); if (addr->basic_errno > 0) g = string_append(g, 2, US": ", @@ -1365,8 +1362,7 @@ if (addr->host_used) if (LOGGING(outgoing_port)) { int port = addr->host_used->port; - g = string_append(g, 2, - US":", port == PORT_NONE ? US"25" : string_sprintf("%d", port)); + g = string_fmt_append(g, ":%d", port == PORT_NONE ? 25 : port); } } @@ -1792,13 +1788,12 @@ addr->basic_errno = code; if (format) { va_list ap; - uschar buffer[512]; + gstring * g; + va_start(ap, format); - if (!string_vformat(buffer, sizeof(buffer), CS format, ap)) - log_write(0, LOG_MAIN|LOG_PANIC_DIE, - "common_error expansion was longer than " SIZE_T_FMT, sizeof(buffer)); + g = string_vformat(NULL, TRUE, CS format, ap); va_end(ap); - addr->message = string_copy(buffer); + addr->message = string_from_gstring(g); } for (addr2 = addr->next; addr2; addr2 = addr2->next) diff --git a/src/src/dummies.c b/src/src/dummies.c index ab821f1ac..2e1ad11f5 100644 --- a/src/src/dummies.c +++ b/src/src/dummies.c @@ -20,7 +20,7 @@ alternates. */ /* We don't have the full Exim headers dragged in, but this function is used for debugging output. */ -extern int string_vformat(char *, int, char *, va_list); +extern gstring * string_vformat(gstring *, BOOL, const char *, va_list); /************************************************* @@ -69,22 +69,24 @@ void debug_printf(char *format, ...) { va_list ap; -char buffer[1024]; +gstring * g = string_get(1024); +void * reset_point = g; va_start(ap, format); -if (!string_vformat(buffer, sizeof(buffer), format, ap)) +if (!string_vformat(g, FALSE, format, ap)) { - char *s = "**** debug string overflowed buffer ****\n"; - char *p = buffer + (int)strlen(buffer); - int maxlen = sizeof(buffer) - (int)strlen(s) - 3; - if (p > buffer + maxlen) p = buffer + maxlen; - if (p > buffer && p[-1] != '\n') *p++ = '\n'; + char * s = "**** debug string overflowed buffer ****\n"; + char * p = CS g->s + g->ptr; + int maxlen = g->size - (int)strlen(s) - 3; + if (p > g->s + maxlen) p = g->s + maxlen; + if (p > g->s && p[-1] != '\n') *p++ = '\n'; strcpy(p, s); } -fprintf(stderr, "%s", buffer); +fprintf(stderr, "%s", string_from_gstring(g)); fflush(stderr); +store_reset(reset_point); va_end(ap); } diff --git a/src/src/exim.c b/src/src/exim.c index a3d1b9e60..8ab11bb4e 100644 --- a/src/src/exim.c +++ b/src/src/exim.c @@ -174,15 +174,22 @@ Returns: nothing void set_process_info(const char *format, ...) { -int len = sprintf(CS process_info, "%5d ", (int)getpid()); +gstring gs = { .size = PROCESS_INFO_SIZE - 2, .ptr = 0, .s = process_info }; +gstring * g; +int len; va_list ap; + +g = string_fmt_append(&gs, "%5d ", (int)getpid()); +len = g->ptr; va_start(ap, format); -if (!string_vformat(process_info + len, PROCESS_INFO_SIZE - len - 2, format, ap)) - Ustrcpy(process_info + len, "**** string overflowed buffer ****"); -len = Ustrlen(process_info); -process_info[len+0] = '\n'; -process_info[len+1] = '\0'; -process_info_len = len + 1; +if (!string_vformat(g, FALSE, format, ap)) + { + gs.ptr = len; + g = string_cat(&gs, US"**** string overflowed buffer ****"); + } +g = string_catn(g, US"\n", 1); +string_from_gstring(g); +process_info_len = g->ptr; DEBUG(D_process_info) debug_printf("set_process_info: %s", process_info); va_end(ap); } diff --git a/src/src/functions.h b/src/src/functions.h index fbf2cbe5b..cab7a7363 100644 --- a/src/src/functions.h +++ b/src/src/functions.h @@ -492,6 +492,7 @@ extern uschar *string_copy_malloc(const uschar *); extern uschar *string_copylc(const uschar *); extern uschar *string_copynlc(uschar *, int); extern uschar *string_dequote(const uschar **); +extern gstring *string_fmt_append(gstring *, const char *, ...) ALMOST_PRINTF(2,3); extern BOOL string_format(uschar *, int, const char *, ...) ALMOST_PRINTF(3,4); extern uschar *string_format_size(int, uschar *); extern uschar *string_from_gstring(gstring *); @@ -515,7 +516,7 @@ extern uschar *string_domain_utf8_to_alabel(const uschar *, uschar **); extern uschar *string_localpart_alabel_to_utf8(const uschar *, uschar **); extern uschar *string_localpart_utf8_to_alabel(const uschar *, uschar **); #endif -extern BOOL string_vformat(uschar *, int, const char *, va_list); +extern gstring *string_vformat(gstring *, BOOL, const char *, va_list); extern int strcmpic(const uschar *, const uschar *); extern int strncmpic(const uschar *, const uschar *, int); extern uschar *strstric(uschar *, uschar *, BOOL); diff --git a/src/src/header.c b/src/src/header.c index 51aa9f953..74df32ca1 100644 --- a/src/src/header.c +++ b/src/src/header.c @@ -98,12 +98,14 @@ header_line **hptr; uschar *p, *q; uschar buffer[HEADER_ADD_BUFFER_SIZE]; +gstring gs = { .size = HEADER_ADD_BUFFER_SIZE, .ptr = 0, .s = buffer }; if (!header_last) return; -if (!string_vformat(buffer, sizeof(buffer), format, ap)) +if (!string_vformat(&gs, FALSE, format, ap)) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string too long in header_add: " - "%.100s ...", buffer); + "%.100s ...", string_from_gstring(&gs)); +string_from_gstring(&gs); /* Find where to insert this header */ diff --git a/src/src/host.c b/src/src/host.c index 208e567a1..c3694c047 100644 --- a/src/src/host.c +++ b/src/src/host.c @@ -2594,12 +2594,13 @@ characters, so the code below should be safe. */ if (whichrrs & HOST_FIND_BY_SRV) { - uschar buffer[300]; - uschar *temp_fully_qualified_name = buffer; + gstring * g; + uschar * temp_fully_qualified_name; int prefix_length; - (void)sprintf(CS buffer, "_%s._tcp.%n%.256s", srv_service, &prefix_length, - host->name); + g = string_fmt_append(NULL, "_%s._tcp.%n%.256s", + srv_service, &prefix_length, host->name); + temp_fully_qualified_name = string_from_gstring(g); ind_type = T_SRV; /* Search for SRV records. If the fully qualified name is different to @@ -2608,7 +2609,8 @@ if (whichrrs & HOST_FIND_BY_SRV) dnssec = DS_UNK; lookup_dnssec_authenticated = NULL; - rc = dns_lookup_timerwrap(&dnsa, buffer, ind_type, CUSS &temp_fully_qualified_name); + rc = dns_lookup_timerwrap(&dnsa, temp_fully_qualified_name, ind_type, + CUSS &temp_fully_qualified_name); DEBUG(D_dns) if ((dnssec_request || dnssec_require) @@ -2624,7 +2626,7 @@ if (whichrrs & HOST_FIND_BY_SRV) { dnssec = DS_NO; lookup_dnssec_authenticated = US"no"; } } - if (temp_fully_qualified_name != buffer && fully_qualified_name != NULL) + if (temp_fully_qualified_name != g->s && fully_qualified_name != NULL) *fully_qualified_name = temp_fully_qualified_name + prefix_length; /* On DNS failures, we give the "try again" error unless the domain is diff --git a/src/src/log.c b/src/src/log.c index 678c02be7..3fdcbbee9 100644 --- a/src/src/log.c +++ b/src/src/log.c @@ -554,23 +554,18 @@ Arguments: Returns: updated pointer */ -static uschar * -log_config_info(uschar *ptr, int flags) +static gstring * +log_config_info(gstring * g, int flags) { -Ustrcpy(ptr, "Exim configuration error"); -ptr += 24; +g = string_cat(g, US"Exim configuration error"); if (flags & (LOG_CONFIG_FOR & ~LOG_CONFIG)) - { - Ustrcpy(ptr, " for "); - return ptr + 5; - } + return string_cat(g, US" for "); if (flags & (LOG_CONFIG_IN & ~LOG_CONFIG)) - ptr += sprintf(CS ptr, " in line %d of %s", config_lineno, config_filename); + g = string_fmt_append(g, " in line %d of %s", config_lineno, config_filename); -Ustrcpy(ptr, ":\n "); -return ptr + 4; +return string_catn(g, US":\n ", 4); } @@ -742,9 +737,10 @@ void log_write(unsigned int selector, int flags, const char *format, ...) { uschar * ptr; -int length; int paniclogfd; ssize_t written_len; +gstring gs = { .size = LOG_BUFFER_SIZE-1, .ptr = 0, .s = log_buffer }; +gstring * g; va_list ap; /* If panic_recurseflag is set, we have failed to open the panic log. This is @@ -851,10 +847,8 @@ in one go so that it doesn't get split when multi-processing. */ DEBUG(D_any|D_v) { int i; - ptr = log_buffer; - Ustrcpy(ptr, "LOG:"); - ptr += 4; + g = string_catn(&gs, US"LOG:", 4); /* Show the selector that was passed into the call. */ @@ -862,31 +856,34 @@ DEBUG(D_any|D_v) { unsigned int bitnum = log_options[i].bit; if (bitnum < BITWORDSIZE && selector == BIT(bitnum)) - { - *ptr++ = ' '; - Ustrcpy(ptr, log_options[i].name); - while (*ptr) ptr++; - } + g = string_fmt_append(g, " %s", log_options[i].name); } - ptr += sprintf(CS ptr, "%s%s%s%s\n ", + g = string_fmt_append(g, "%s%s%s%s\n ", flags & LOG_MAIN ? " MAIN" : "", flags & LOG_PANIC ? " PANIC" : "", (flags & LOG_PANIC_DIE) == LOG_PANIC_DIE ? " DIE" : "", flags & LOG_REJECT ? " REJECT" : ""); - if (flags & LOG_CONFIG) ptr = log_config_info(ptr, flags); + if (flags & LOG_CONFIG) g = log_config_info(g, flags); va_start(ap, format); - if (!string_vformat(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer)-1, format, ap)) - Ustrcpy(ptr, "**** log string overflowed log buffer ****"); + i = g->ptr; + if (!string_vformat(g, FALSE, format, ap)) + { + g->ptr = i; + g = string_cat(g, US"**** log string overflowed log buffer ****"); + } va_end(ap); - while(*ptr) ptr++; - Ustrcat(ptr, "\n"); - debug_printf("%s", log_buffer); - } + g->size = LOG_BUFFER_SIZE; + g = string_catn(g, US"\n", 1); + debug_printf("%s", string_from_gstring(g)); + gs.size = LOG_BUFFER_SIZE-1; /* Having used the buffer for debug output, */ + gs.ptr = 0; /* reset it for the real use. */ + gs.s = log_buffer; + } /* If no log file is specified, we are in a mess. */ if (!(flags & (LOG_MAIN|LOG_PANIC|LOG_REJECT))) @@ -908,54 +905,59 @@ if (!write_rejectlog) flags &= ~LOG_REJECT; /* Create the main message in the log buffer. Do not include the message id when called by a utility. */ -ptr = log_buffer; -ptr += sprintf(CS ptr, "%s ", tod_stamp(tod_log)); +g = string_fmt_append(&gs, "%s ", tod_stamp(tod_log)); if (LOGGING(pid)) { - if (!syslog_pid) pid_position[0] = ptr - log_buffer; /* remember begin … */ - ptr += sprintf(CS ptr, "[%d] ", (int)getpid()); - if (!syslog_pid) pid_position[1] = ptr - log_buffer; /* … and end+1 of the PID */ + if (!syslog_pid) pid_position[0] = g->ptr; /* remember begin … */ + g = string_fmt_append(g, "[%d] ", (int)getpid()); + if (!syslog_pid) pid_position[1] = g->ptr; /* … and end+1 of the PID */ } if (f.really_exim && message_id[0] != 0) - ptr += sprintf(CS ptr, "%s ", message_id); + g = string_fmt_append(g, "%s ", message_id); -if (flags & LOG_CONFIG) ptr = log_config_info(ptr, flags); +if (flags & LOG_CONFIG) + g = log_config_info(g, flags); va_start(ap, format); -if (!string_vformat(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer)-1, format, ap)) - Ustrcpy(ptr, "**** log string overflowed log buffer ****\n"); -while(*ptr) ptr++; + { + int i = g->ptr; + if (!string_vformat(g, FALSE, format, ap)) + { + g->ptr = i; + g = string_cat(g, US"**** log string overflowed log buffer ****\n"); + } + } va_end(ap); /* Add the raw, unrewritten, sender to the message if required. This is done this way because it kind of fits with LOG_RECIPIENTS. */ if ( flags & LOG_SENDER - && ptr < log_buffer + LOG_BUFFER_SIZE - 10 - Ustrlen(raw_sender)) - ptr += sprintf(CS ptr, " from <%s>", raw_sender); + && g->ptr < LOG_BUFFER_SIZE - 10 - Ustrlen(raw_sender)) + g = string_fmt_append(g, " from <%s>", raw_sender); /* Add list of recipients to the message if required; the raw list, before rewriting, was saved in raw_recipients. There may be none, if an ACL discarded them all. */ if ( flags & LOG_RECIPIENTS - && ptr < log_buffer + LOG_BUFFER_SIZE - 6 + && g->ptr < LOG_BUFFER_SIZE - 6 && raw_recipients_count > 0) { int i; - ptr += sprintf(CS ptr, " for"); + g = string_fmt_append(g, " for"); for (i = 0; i < raw_recipients_count; i++) { uschar * s = raw_recipients[i]; - if (log_buffer + LOG_BUFFER_SIZE - ptr < Ustrlen(s) + 3) break; - ptr += sprintf(CS ptr, " %s", s); + if (LOG_BUFFER_SIZE - g->ptr < Ustrlen(s) + 3) break; + g = string_fmt_append(g, " %s", s); } } -ptr += sprintf(CS ptr, "\n"); -length = ptr - log_buffer; +g = string_catn(g, US"\n", 1); +string_from_gstring(g); /* Handle loggable errors when running a utility, or when address testing. Write to log_stderr unless debugging (when it will already have been written), @@ -1028,10 +1030,10 @@ if ( flags & LOG_MAIN /* Failing to write to the log is disastrous */ - written_len = write_to_fd_buf(mainlogfd, log_buffer, length); - if (written_len != length) + written_len = write_to_fd_buf(mainlogfd, g->s, g->ptr); + if (written_len != g->ptr) { - log_write_failed(US"main log", length, written_len); + log_write_failed(US"main log", g->ptr, written_len); /* That function does not return */ } } @@ -1048,34 +1050,39 @@ if (flags & LOG_REJECT) if (header_list && LOGGING(rejected_header)) { + uschar * p = g->s + g->ptr; + int i; + if (recipients_count > 0) { - int i; - /* List the sender */ - string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + string_format(p, LOG_BUFFER_SIZE - g->ptr, "Envelope-from: <%s>\n", sender_address); - while (*ptr) ptr++; + while (*p) p++; + g->ptr = p - g->s; /* List up to 5 recipients */ - string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + string_format(p, LOG_BUFFER_SIZE - g->ptr, "Envelope-to: <%s>\n", recipients_list[0].address); - while (*ptr) ptr++; + while (*p) p++; + g->ptr = p - g->s; for (i = 1; i < recipients_count && i < 5; i++) { - string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), " <%s>\n", + string_format(p, LOG_BUFFER_SIZE - g->ptr, " <%s>\n", recipients_list[i].address); - while (*ptr) ptr++; + while (*p) p++; + g->ptr = p - g->s; } if (i < recipients_count) { - (void)string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + string_format(p, LOG_BUFFER_SIZE - g->ptr, " ...\n"); - while (*ptr) ptr++; + while (*p) p++; + g->ptr = p - g->s; } } @@ -1083,27 +1090,25 @@ if (flags & LOG_REJECT) for (h = header_list; h; h = h->next) if (h->text) { - BOOL fitted = string_format(ptr, LOG_BUFFER_SIZE - (ptr-log_buffer), + BOOL fitted = string_format(p, LOG_BUFFER_SIZE - g->ptr, "%c %s", h->type, h->text); - while(*ptr) ptr++; + while (*p) p++; + g->ptr = p - g->s; if (!fitted) /* Buffer is full; truncate */ { - ptr -= 100; /* For message and separator */ - if (ptr[-1] == '\n') ptr--; - Ustrcpy(ptr, "\n*** truncated ***\n"); - while (*ptr) ptr++; + g->ptr -= 100; /* For message and separator */ + if (g->s[g->ptr-1] == '\n') g->ptr--; + g = string_cat(g, US"\n*** truncated ***\n"); break; } } - - length = ptr - log_buffer; } /* Write to syslog or to a log file */ if ( logging_mode & LOG_MODE_SYSLOG && (syslog_duplication || !(flags & LOG_PANIC))) - write_syslog(LOG_NOTICE, log_buffer); + write_syslog(LOG_NOTICE, string_from_gstring(g)); /* Check for a change to the rejectlog file name when datestamping is in operation. This happens at midnight, at which point we want to roll over @@ -1147,10 +1152,10 @@ if (flags & LOG_REJECT) if (fstat(rejectlogfd, &statbuf) >= 0) rejectlog_inode = statbuf.st_ino; } - written_len = write_to_fd_buf(rejectlogfd, log_buffer, length); - if (written_len != length) + written_len = write_to_fd_buf(rejectlogfd, g->s, g->ptr); + if (written_len != g->ptr) { - log_write_failed(US"reject log", length, written_len); + log_write_failed(US"reject log", g->ptr, written_len); /* That function does not return */ } } @@ -1165,7 +1170,7 @@ all cases except mua_wrapper, try to write to log_stderr. */ if (flags & LOG_PANIC) { if (log_stderr && log_stderr != debug_file && !mua_wrapper) - fprintf(log_stderr, "%s", CS log_buffer); + fprintf(log_stderr, "%s", CS string_from_gstring(g)); if (logging_mode & LOG_MODE_SYSLOG) write_syslog(LOG_ALERT, log_buffer); @@ -1185,14 +1190,14 @@ if (flags & LOG_PANIC) i = i; /* compiler quietening */ } - written_len = write_to_fd_buf(paniclogfd, log_buffer, length); - if (written_len != length) + written_len = write_to_fd_buf(paniclogfd, g->s, g->ptr); + if (written_len != g->ptr) { int save_errno = errno; write_syslog(LOG_CRIT, log_buffer); sprintf(CS log_buffer, "write failed on panic log: length=%d result=%d " - "errno=%d (%s)", length, (int)written_len, save_errno, strerror(save_errno)); - write_syslog(LOG_CRIT, log_buffer); + "errno=%d (%s)", g->ptr, (int)written_len, save_errno, strerror(save_errno)); + write_syslog(LOG_CRIT, string_from_gstring(g)); flags |= LOG_PANIC_DIE; } diff --git a/src/src/smtp_in.c b/src/src/smtp_in.c index 2e3c9b9ec..54ebf3660 100644 --- a/src/src/smtp_in.c +++ b/src/src/smtp_in.c @@ -913,18 +913,20 @@ call another vararg function, only a function which accepts a va_list. */ void smtp_vprintf(const char *format, BOOL more, va_list ap) { +gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer }; BOOL yield; -yield = string_vformat(big_buffer, big_buffer_size, format, ap); +yield = !! string_vformat(&gs, FALSE, format, ap); +string_from_gstring(&gs); DEBUG(D_receive) { void *reset_point = store_get(0); uschar *msg_copy, *cr, *end; - msg_copy = string_copy(big_buffer); - end = msg_copy + Ustrlen(msg_copy); + msg_copy = string_copy(gs.s); + end = msg_copy + gs.ptr; while ((cr = Ustrchr(msg_copy, '\r')) != NULL) /* lose CRs */ - memmove(cr, cr + 1, (end--) - cr); + memmove(cr, cr + 1, (end--) - cr); debug_printf("SMTP>> %s", msg_copy); store_reset(reset_point); } @@ -957,13 +959,13 @@ if (fl.rcpt_in_progress) #ifdef SUPPORT_TLS if (tls_in.active.sock >= 0) { - if (tls_write(NULL, big_buffer, Ustrlen(big_buffer), more) < 0) + if (tls_write(NULL, gs.s, gs.ptr, more) < 0) smtp_write_error = -1; } else #endif -if (fprintf(smtp_out, "%s", big_buffer) < 0) smtp_write_error = -1; +if (fprintf(smtp_out, "%s", gs.s) < 0) smtp_write_error = -1; } @@ -3518,13 +3520,13 @@ if (code && defaultrespond) smtp_respond(code, 3, TRUE, user_msg); else { - uschar buffer[128]; + gstring * g; va_list ap; + va_start(ap, defaultrespond); - if (!string_vformat(buffer, sizeof(buffer), CS defaultrespond, ap)) - log_write(0, LOG_MAIN|LOG_PANIC, "string too large in smtp_notquit_exit()"); - smtp_printf("%s %s\r\n", FALSE, code, buffer); + g = string_vformat(NULL, TRUE, CS defaultrespond, ap); va_end(ap); + smtp_printf("%s %s\r\n", FALSE, code, string_from_gstring(g)); } mac_smtp_fflush(); } diff --git a/src/src/smtp_out.c b/src/src/smtp_out.c index 1209c7fd9..9bd90c77a 100644 --- a/src/src/smtp_out.c +++ b/src/src/smtp_out.c @@ -512,34 +512,34 @@ int smtp_write_command(void * sx, int mode, const char *format, ...) { smtp_outblock * outblock = &((smtp_context *)sx)->outblock; -int count; int rc = 0; -va_list ap; if (format) { + gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer }; + va_list ap; + va_start(ap, format); - if (!string_vformat(big_buffer, big_buffer_size, CS format, ap)) + if (!string_vformat(&gs, FALSE, CS format, ap)) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing " "SMTP"); va_end(ap); - count = Ustrlen(big_buffer); + string_from_gstring(&gs); - if (count > outblock->buffersize) + if (gs.ptr > outblock->buffersize) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong write_command in outgoing " "SMTP"); - if (count > outblock->buffersize - (outblock->ptr - outblock->buffer)) + if (gs.ptr > outblock->buffersize - (outblock->ptr - outblock->buffer)) { rc = outblock->cmd_count; /* flush resets */ if (!flush_buffer(outblock, SCMD_FLUSH)) return -1; } - Ustrncpy(CS outblock->ptr, big_buffer, count); - outblock->ptr += count; + Ustrncpy(CS outblock->ptr, gs.s, gs.ptr); + outblock->ptr += gs.ptr; outblock->cmd_count++; - count -= 2; - big_buffer[count] = 0; /* remove \r\n for error message */ + gs.ptr -= 2; string_from_gstring(&gs); /* remove \r\n for error message */ /* We want to hide the actual data sent in AUTH transactions from reflections and logs. While authenticating, a flag is set in the outblock to enable this. diff --git a/src/src/string.c b/src/src/string.c index 112c2adc3..d0b8db4ae 100644 --- a/src/src/string.c +++ b/src/src/string.c @@ -10,6 +10,7 @@ utilities and tests, and are cut out by the COMPILE_UTILITY macro. */ #include "exim.h" +#include #ifndef COMPILE_UTILITY @@ -715,16 +716,33 @@ Returns: pointer to fresh piece of store containing sprintf'ed string uschar * string_sprintf(const char *format, ...) { -va_list ap; +#ifdef COMPILE_UTILITY uschar buffer[STRING_SPRINTF_BUFFER_SIZE]; +gstring g = { .size = STRING_SPRINTF_BUFFER_SIZE, .ptr = 0, .s = buffer }; +gstring * gp = &g; +#else +gstring * gp = string_get(STRING_SPRINTF_BUFFER_SIZE); +#endif +gstring * gp2; +va_list ap; + va_start(ap, format); -if (!string_vformat(buffer, sizeof(buffer), format, ap)) - log_write(0, LOG_MAIN|LOG_PANIC_DIE, - "string_sprintf expansion was longer than " SIZE_T_FMT - "; format string was (%s)\nexpansion started '%.32s'", - sizeof(buffer), format, buffer); +gp2 = string_vformat(gp, FALSE, format, ap); +gp->s[gp->ptr] = '\0'; va_end(ap); -return string_copy(buffer); + +if (!gp2) + log_write(0, LOG_MAIN|LOG_PANIC_DIE, + "string_sprintf expansion was longer than %d; format string was (%s)\n" + "expansion started '%.32s'", + gp->size, format, gp->s); + +#ifdef COMPILE_UTILITY +return string_copy(gp->s); +#else +gstring_reset_unused(gp); +return gp->s; +#endif } @@ -830,6 +848,17 @@ return NULL; +#ifdef COMPILE_UTILITY +/* Dummy version for this function; it should never be called */ +static void +gstring_grow(gstring * g, int p, int count) +{ +assert(FALSE); +} +#endif + + + #ifndef COMPILE_UTILITY /************************************************* * Get next string from separated list * @@ -1098,12 +1127,11 @@ gstring_reset_unused(gstring * g) store_reset(g->s + (g->size = g->ptr + 1)); } -/************************************************* -* Add chars to string * -*************************************************/ -/* Arguments: - g the grawable-string +/* Add more space to a growable-string. + +Arguments: + g the growable-string p current end of data count amount to grow by */ @@ -1138,6 +1166,9 @@ if (!store_extend(g->s, oldsize, g->size)) +/************************************************* +* Add chars to string * +*************************************************/ /* This function is used when building up strings of unknown length. Room is always left for a terminating zero to be added to the string that is being built. This function does not require the string that is being added to be NUL @@ -1257,50 +1288,78 @@ Returns: TRUE if the result fitted in the buffer */ BOOL -string_format(uschar *buffer, int buflen, const char *format, ...) +string_format(uschar * buffer, int buflen, const char * format, ...) { -BOOL yield; +gstring g = { .size = buflen, .ptr = 0, .s = buffer }, *gp; va_list ap; va_start(ap, format); -yield = string_vformat(buffer, buflen, format, ap); +gp = string_vformat(&g, FALSE, format, ap); va_end(ap); -return yield; +g.s[g.ptr] = '\0'; +return !!gp; } -BOOL -string_vformat(uschar *buffer, int buflen, const char *format, va_list ap) + + + +/* Bulid or append to a growing-string, sprintf-style. + +If the "extend" argument is true, the string passed in can be NULL, +empty, or non-empty. + +If the "extend" argument is false, the string passed in may not be NULL, +will not be grown, and is usable in the original place after return. +The return value can be NULL to signify overflow. + +Returns the possibly-new (if copy for growth was needed) string, +not nul-terminated. +*/ + +gstring * +string_vformat(gstring * g, BOOL extend, const char *format, va_list ap) { -/* We assume numbered ascending order, C does not guarantee that */ -enum { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 }; +enum ltypes { L_NORMAL=1, L_SHORT=2, L_LONG=3, L_LONGLONG=4, L_LONGDOUBLE=5, L_SIZE=6 }; + +int width, precision, off, lim; +const char * fp = format; /* Deliberately not unsigned */ -BOOL yield = TRUE; -int width, precision; -const char *fp = format; /* Deliberately not unsigned */ -uschar *p = buffer; -uschar *last = buffer + buflen - 1; +string_datestamp_offset = -1; /* Datestamp not inserted */ +string_datestamp_length = 0; /* Datestamp not inserted */ +string_datestamp_type = 0; /* Datestamp not inserted */ -string_datestamp_offset = -1; /* Datestamp not inserted */ -string_datestamp_length = 0; /* Datestamp not inserted */ -string_datestamp_type = 0; /* Datestamp not inserted */ +#ifdef COMPILE_UTILITY +assert(!extend); +assert(g); +#else + +/* Ensure we have a string, to save on checking later */ +if (!g) g = string_get(16); +#endif /*!COMPILE_UTILITY*/ + +lim = g->size - 1; /* leave one for a nul */ +off = g->ptr; /* remember initial offset in gstring */ /* Scan the format and handle the insertions */ -while (*fp != 0) +while (*fp) { int length = L_NORMAL; int *nptr; int slen; - const char *null = "NULL"; /* ) These variables */ - const char *item_start, *s; /* ) are deliberately */ - char newformat[16]; /* ) not unsigned */ + const char *null = "NULL"; /* ) These variables */ + const char *item_start, *s; /* ) are deliberately */ + char newformat[16]; /* ) not unsigned */ + char * gp = CS g->s + g->ptr; /* ) */ /* Non-% characters just get copied verbatim */ if (*fp != '%') { - if (p >= last) { yield = FALSE; break; } - *p++ = (uschar)*fp++; + /* Avoid string_copyn() due to COMPILE_UTILITY */ + if (g->ptr >= lim - 1) + if (extend) gstring_grow(g, g->ptr, 1); else return NULL; + g->s[g->ptr++] = (uschar) *fp++; continue; } @@ -1328,19 +1387,14 @@ while (*fp != 0) } if (*fp == '.') - { if (*(++fp) == '*') { precision = va_arg(ap, int); fp++; } else - { - precision = 0; - while (isdigit((uschar)*fp)) - precision = precision*10 + *fp++ - '0'; - } - } + for (precision = 0; isdigit((uschar)*fp); fp++) + precision = precision*10 + *fp - '0'; /* Skip over 'h', 'L', 'l', 'll' and 'z', remembering the item length */ @@ -1349,18 +1403,10 @@ while (*fp != 0) else if (*fp == 'L') { fp++; length = L_LONGDOUBLE; } else if (*fp == 'l') - { if (fp[1] == 'l') - { - fp += 2; - length = L_LONGLONG; - } + { fp += 2; length = L_LONGLONG; } else - { - fp++; - length = L_LONG; - } - } + { fp++; length = L_LONG; } else if (*fp == 'z') { fp++; length = L_SIZE; } @@ -1369,47 +1415,53 @@ while (*fp != 0) switch (*fp++) { case 'n': - nptr = va_arg(ap, int *); - *nptr = p - buffer; - break; + nptr = va_arg(ap, int *); + *nptr = g->ptr - off; + break; case 'd': case 'o': case 'u': case 'x': case 'X': - if (p >= last - ((length > L_LONG)? 24 : 12)) - { yield = FALSE; goto END_FORMAT; } - strncpy(newformat, item_start, fp - item_start); - newformat[fp - item_start] = 0; + width = length > L_LONG ? 24 : 12; + if (g->ptr >= lim - width) + if (extend) gstring_grow(g, g->ptr, width); else return NULL; + strncpy(newformat, item_start, fp - item_start); + newformat[fp - item_start] = 0; - /* Short int is promoted to int when passing through ..., so we must use - int for va_arg(). */ + /* Short int is promoted to int when passing through ..., so we must use + int for va_arg(). */ - switch(length) - { - case L_SHORT: - case L_NORMAL: p += sprintf(CS p, newformat, va_arg(ap, int)); break; - case L_LONG: p += sprintf(CS p, newformat, va_arg(ap, long int)); break; - case L_LONGLONG: p += sprintf(CS p, newformat, va_arg(ap, LONGLONG_T)); break; - case L_SIZE: p += sprintf(CS p, newformat, va_arg(ap, size_t)); break; - } - break; + switch(length) + { + case L_SHORT: + case L_NORMAL: + g->ptr += sprintf(gp, newformat, va_arg(ap, int)); break; + case L_LONG: + g->ptr += sprintf(gp, newformat, va_arg(ap, long int)); break; + case L_LONGLONG: + g->ptr += sprintf(gp, newformat, va_arg(ap, LONGLONG_T)); break; + case L_SIZE: + g->ptr += sprintf(gp, newformat, va_arg(ap, size_t)); break; + } + break; case 'p': { void * ptr; - if (p >= last - 24) { yield = FALSE; goto END_FORMAT; } + if (g->ptr >= lim - 24) + if (extend) gstring_grow(g, g->ptr, 24); else return NULL; /* sprintf() saying "(nil)" for a null pointer seems unreliable. Handle it explicitly. */ if ((ptr = va_arg(ap, void *))) { strncpy(newformat, item_start, fp - item_start); newformat[fp - item_start] = 0; - p += sprintf(CS p, newformat, ptr); + g->ptr += sprintf(gp, newformat, ptr); } else - p += sprintf(CS p, "(nil)"); + g->ptr += sprintf(gp, "(nil)"); } break; @@ -1425,123 +1477,134 @@ while (*fp != 0) case 'E': case 'g': case 'G': - if (precision < 0) precision = 6; - if (p >= last - precision - 8) { yield = FALSE; goto END_FORMAT; } - strncpy(newformat, item_start, fp - item_start); - newformat[fp-item_start] = 0; - if (length == L_LONGDOUBLE) - p += sprintf(CS p, newformat, va_arg(ap, long double)); - else - p += sprintf(CS p, newformat, va_arg(ap, double)); - break; + if (precision < 0) precision = 6; + if (g->ptr >= lim - precision - 8) + if (extend) gstring_grow(g, g->ptr, precision+8); else return NULL; + strncpy(newformat, item_start, fp - item_start); + newformat[fp-item_start] = 0; + if (length == L_LONGDOUBLE) + g->ptr += sprintf(gp, newformat, va_arg(ap, long double)); + else + g->ptr += sprintf(gp, newformat, va_arg(ap, double)); + break; /* String types */ case '%': - if (p >= last) { yield = FALSE; goto END_FORMAT; } - *p++ = '%'; - break; + if (g->ptr >= lim - 1) + if (extend) gstring_grow(g, g->ptr, 1); else return NULL; + g->s[g->ptr++] = (uschar) '%'; + break; case 'c': - if (p >= last) { yield = FALSE; goto END_FORMAT; } - *p++ = va_arg(ap, int); - break; + if (g->ptr >= lim - 1) + if (extend) gstring_grow(g, g->ptr, 1); else return NULL; + g->s[g->ptr++] = (uschar) va_arg(ap, int); + break; case 'D': /* Insert daily datestamp for log file names */ - s = CS tod_stamp(tod_log_datestamp_daily); - string_datestamp_offset = p - buffer; /* Passed back via global */ - string_datestamp_length = Ustrlen(s); /* Passed back via global */ - string_datestamp_type = tod_log_datestamp_daily; - slen = string_datestamp_length; - goto INSERT_STRING; + s = CS tod_stamp(tod_log_datestamp_daily); + string_datestamp_offset = g->ptr; /* Passed back via global */ + string_datestamp_length = Ustrlen(s); /* Passed back via global */ + string_datestamp_type = tod_log_datestamp_daily; + slen = string_datestamp_length; + goto INSERT_STRING; case 'M': /* Insert monthly datestamp for log file names */ - s = CS tod_stamp(tod_log_datestamp_monthly); - string_datestamp_offset = p - buffer; /* Passed back via global */ - string_datestamp_length = Ustrlen(s); /* Passed back via global */ - string_datestamp_type = tod_log_datestamp_monthly; - slen = string_datestamp_length; - goto INSERT_STRING; + s = CS tod_stamp(tod_log_datestamp_monthly); + string_datestamp_offset = g->ptr; /* Passed back via global */ + string_datestamp_length = Ustrlen(s); /* Passed back via global */ + string_datestamp_type = tod_log_datestamp_monthly; + slen = string_datestamp_length; + goto INSERT_STRING; case 's': case 'S': /* Forces *lower* case */ case 'T': /* Forces *upper* case */ - s = va_arg(ap, char *); + s = va_arg(ap, char *); - if (s == NULL) s = null; - slen = Ustrlen(s); + if (!s) s = null; + slen = Ustrlen(s); INSERT_STRING: /* Come to from %D or %M above */ - /* If the width is specified, check that there is a precision - set; if not, set it to the width to prevent overruns of long - strings. */ - - if (width >= 0) { - if (precision < 0) precision = width; - } + BOOL truncated = FALSE; - /* If a width is not specified and the precision is specified, set - the width to the precision, or the string length if shorted. */ + /* If the width is specified, check that there is a precision + set; if not, set it to the width to prevent overruns of long + strings. */ - else if (precision >= 0) - { - width = (precision < slen)? precision : slen; - } + if (width >= 0) + { + if (precision < 0) precision = width; + } - /* If neither are specified, set them both to the string length. */ + /* If a width is not specified and the precision is specified, set + the width to the precision, or the string length if shorted. */ - else width = precision = slen; + else if (precision >= 0) + width = precision < slen ? precision : slen; - /* Check string space, and add the string to the buffer if ok. If - not OK, add part of the string (debugging uses this to show as - much as possible). */ + /* If neither are specified, set them both to the string length. */ - if (p == last) - { - yield = FALSE; - goto END_FORMAT; - } - if (p >= last - width) - { - yield = FALSE; - width = precision = last - p - 1; - if (width < 0) width = 0; - if (precision < 0) precision = 0; + else + width = precision = slen; + + if (!extend) + { + if (g->ptr == lim) return NULL; + if (g->ptr >= lim - width) + { + truncated = TRUE; + width = precision = lim - g->ptr - 1; + if (width < 0) width = 0; + if (precision < 0) precision = 0; + } + } + else if (g->ptr >= lim - width) + gstring_grow(g, g->ptr, width); + + g->ptr += sprintf(gp, "%*.*s", width, precision, s); + if (fp[-1] == 'S') + while (*gp) { *gp = tolower(*gp); gp++; } + else if (fp[-1] == 'T') + while (*gp) { *gp = toupper(*gp); gp++; } + + if (truncated) return NULL; + break; } - sprintf(CS p, "%*.*s", width, precision, s); - if (fp[-1] == 'S') - while (*p) { *p = tolower(*p); p++; } - else if (fp[-1] == 'T') - while (*p) { *p = toupper(*p); p++; } - else - while (*p) p++; - if (!yield) goto END_FORMAT; - break; /* Some things are never used in Exim; also catches junk. */ default: - strncpy(newformat, item_start, fp - item_start); - newformat[fp-item_start] = 0; - log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type " - "in \"%s\" in \"%s\"", newformat, format); - break; + strncpy(newformat, item_start, fp - item_start); + newformat[fp-item_start] = 0; + log_write(0, LOG_MAIN|LOG_PANIC_DIE, "string_format: unsupported type " + "in \"%s\" in \"%s\"", newformat, format); + break; } } -/* Ensure string is complete; return TRUE if got to the end of the format */ +return g; +} -END_FORMAT: -*p = 0; -return yield; + +#ifndef COMPILE_UTILITY + +gstring * +string_fmt_append(gstring * g, const char *format, ...) +{ +va_list ap; +va_start(ap, format); +g = string_vformat(g, TRUE, format, ap); +va_end(ap); +return g; } -#ifndef COMPILE_UTILITY /************************************************* * Generate an "open failed" message * *************************************************/ @@ -1562,23 +1625,25 @@ uschar * string_open_failed(int eno, const char *format, ...) { va_list ap; -uschar buffer[1024]; +gstring * g = string_get(1024); -Ustrcpy(buffer, "failed to open "); -va_start(ap, format); +g = string_catn(g, US"failed to open ", 15); /* Use the checked formatting routine to ensure that the buffer does not overflow. It should not, since this is called only for internally specified messages. If it does, the message just gets truncated, and there doesn't seem much we can do about that. */ -(void)string_vformat(buffer+15, sizeof(buffer) - 15, format, ap); +va_start(ap, format); +(void) string_vformat(g, FALSE, format, ap); +string_from_gstring(g); +gstring_reset_unused(g); va_end(ap); -return (eno == EACCES)? - string_sprintf("%s: %s (euid=%ld egid=%ld)", buffer, strerror(eno), - (long int)geteuid(), (long int)getegid()) : - string_sprintf("%s: %s", buffer, strerror(eno)); +return eno == EACCES + ? string_sprintf("%s: %s (euid=%ld egid=%ld)", g->s, strerror(eno), + (long int)geteuid(), (long int)getegid()) + : string_sprintf("%s: %s", g->s, strerror(eno)); } #endif /* COMPILE_UTILITY */ @@ -1600,6 +1665,7 @@ return Ustrcmp(* CUSS a, * CUSS b); + /************************************************* ************************************************** * Stand-alone test program * diff --git a/src/src/transport.c b/src/src/transport.c index 48c18abc6..8ccdd0389 100644 --- a/src/src/transport.c +++ b/src/src/transport.c @@ -375,13 +375,15 @@ BOOL transport_write_string(int fd, const char *format, ...) { transport_ctx tctx = {{0}}; +gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer }; va_list ap; + va_start(ap, format); -if (!string_vformat(big_buffer, big_buffer_size, format, ap)) +if (!string_vformat(&gs, FALSE, format, ap)) log_write(0, LOG_MAIN|LOG_PANIC_DIE, "overlong formatted string in transport"); va_end(ap); tctx.u.fd = fd; -return transport_write_block(&tctx, big_buffer, Ustrlen(big_buffer), FALSE); +return transport_write_block(&tctx, gs.s, gs.ptr, FALSE); } diff --git a/src/src/transports/lmtp.c b/src/src/transports/lmtp.c index cea28c943..240d78b21 100644 --- a/src/src/transports/lmtp.c +++ b/src/src/transports/lmtp.c @@ -223,20 +223,21 @@ Returns: TRUE if successful, FALSE if not, with errno set static BOOL lmtp_write_command(int fd, const char *format, ...) { -int count, rc; +gstring gs = { .size = big_buffer_size, .ptr = 0, .s = big_buffer }; +int rc; va_list ap; + va_start(ap, format); -if (!string_vformat(big_buffer, big_buffer_size, CS format, ap)) +if (!string_vformat(&gs, FALSE, CS format, ap)) { va_end(ap); errno = ERRNO_SMTPFORMAT; return FALSE; } va_end(ap); -count = Ustrlen(big_buffer); -DEBUG(D_transport|D_v) debug_printf(" LMTP>> %s", big_buffer); -rc = write(fd, big_buffer, count); -big_buffer[count-2] = 0; /* remove \r\n for debug and error message */ +DEBUG(D_transport|D_v) debug_printf(" LMTP>> %s", string_from_gstring(&gs)); +rc = write(fd, gs.s, gs.ptr); +gs.ptr -= 2; string_from_gstring(&gs); /* remove \r\n for debug and error message */ if (rc > 0) return TRUE; DEBUG(D_transport) debug_printf("write failed: %s\n", strerror(errno)); return FALSE; -- 2.30.2