From 52f12a7cec769b679305bb9ba23534dfd155d46a Mon Sep 17 00:00:00 2001 From: Jeremy Harris Date: Sat, 17 Nov 2018 19:40:01 +0000 Subject: [PATCH] Lose more string-copy operations --- src/src/arc.c | 26 +++++--------- src/src/daemon.c | 12 +++---- src/src/dkim.c | 23 ++++++------- src/src/expand.c | 72 +++++++++++++-------------------------- src/src/lookups/redis.c | 4 +-- src/src/receive.c | 7 ++-- src/src/smtp_in.c | 16 +++------ src/src/transports/smtp.c | 16 ++++----- 8 files changed, 66 insertions(+), 110 deletions(-) diff --git a/src/src/arc.c b/src/src/arc.c index aa0d1c958..6c4bcc641 100644 --- a/src/src/arc.c +++ b/src/src/arc.c @@ -1194,7 +1194,7 @@ arc_line * al = (arc_line *)(as+1); header_line * h = (header_line *)(al+1); g = string_catn(g, ARC_HDR_AAR, ARC_HDRLEN_AAR); -g = string_cat(g, string_sprintf(" i=%d; %s;\r\n\t", instance, identity)); +g = string_fmt_append(g, " i=%d; %s;\r\n\t", instance, identity); g = string_catn(g, US ar->data, ar->len); h->slen = g->ptr - aar_off; @@ -1307,20 +1307,14 @@ header_line * h = (header_line *)(al+1); /* Construct the to-be-signed AMS pseudo-header: everything but the sig. */ ams_off = g->ptr; -g = string_append(g, 7, - ARC_HDR_AMS, - US" i=", string_sprintf("%d", instance), - US"; a=rsa-sha256; c=relaxed; d=", identity, /*XXX hardwired */ - US"; s=", selector); +g = string_fmt_append(g, "%s i=%d; a=rsa-sha256; c=relaxed; d=%s; s=%s", + ARC_HDR_AMS, instance, identity, selector); /*XXX hardwired a= */ if (options & ARC_SIGN_OPT_TSTAMP) - g = string_append(g, 2, - US"; t=", string_sprintf("%lu", (u_long)now)); + g = string_fmt_append(g, "; t=%lu", (u_long)now); if (options & ARC_SIGN_OPT_EXPIRE) - g = string_append(g, 2, - US"; x=", string_sprintf("%lu", (u_long)expire)); -g = string_append(g, 3, - US";\r\n\tbh=", pdkim_encode_base64(bodyhash), - US";\r\n\th="); + g = string_fmt_append(g, "; x=%lu", (u_long)expire); +g = string_fmt_append(g, ";\r\n\tbh=%s;\r\n\th=", + pdkim_encode_base64(bodyhash)); for(col = 3; rheaders; rheaders = rheaders->prev) { @@ -1828,16 +1822,14 @@ if (arc_state) g = string_append(g, 2, US";\n\tarc=", arc_state); if (arc_received_instance > 0) { - g = string_append(g, 3, US" (i=", - string_sprintf("%d", arc_received_instance), US")"); + g = string_fmt_append(g, " (i=%d)", arc_received_instance); if (arc_state_reason) g = string_append(g, 3, US"(", arc_state_reason, US")"); g = string_catn(g, US" header.s=", 10); highest_ams = arc_received->hdr_ams; g = string_catn(g, highest_ams->s.data, highest_ams->s.len); - g = string_append(g, 2, - US" arc.oldest-pass=", string_sprintf("%d", arc_oldest_pass)); + g = string_fmt_append(g, " arc.oldest-pass=%d", arc_oldest_pass); if (sender_host_address) g = string_append(g, 2, US" smtp.remote-ip=", sender_host_address); diff --git a/src/src/daemon.c b/src/src/daemon.c index 1a15d46c0..a852192ea 100644 --- a/src/src/daemon.c +++ b/src/src/daemon.c @@ -106,10 +106,10 @@ Returns: nothing static void never_error(uschar *log_msg, uschar *smtp_msg, int was_errno) { -uschar *emsg = (was_errno <= 0)? US"" : - string_sprintf(": %s", strerror(was_errno)); +uschar *emsg = was_errno <= 0 + ? US"" : string_sprintf(": %s", strerror(was_errno)); log_write(0, LOG_MAIN|LOG_PANIC, "%s%s", log_msg, emsg); -if (smtp_out != NULL) smtp_printf("421 %s\r\n", FALSE, smtp_msg); +if (smtp_out) smtp_printf("421 %s\r\n", FALSE, smtp_msg); } @@ -202,11 +202,11 @@ memory is reclaimed. */ whofrom = string_append(NULL, 3, "[", sender_host_address, "]"); if (LOGGING(incoming_port)) - whofrom = string_append(whofrom, 2, ":", string_sprintf("%d", sender_host_port)); + whofrom = string_fmt_append(whofrom, ":%d", sender_host_port); if (LOGGING(incoming_interface)) - whofrom = string_append(whofrom, 4, " I=[", - interface_address, "]:", string_sprintf("%d", interface_port)); + whofrom = string_fmt_append(whofrom, " I=[%s]:%d", + interface_address, interface_port); (void) string_from_gstring(whofrom); /* Terminate the newly-built string */ diff --git a/src/src/dkim.c b/src/src/dkim.c index 4d1822e2e..5209cd983 100644 --- a/src/src/dkim.c +++ b/src/src/dkim.c @@ -192,19 +192,18 @@ if (!(s = sig->domain)) s = US""; logmsg = string_append(logmsg, 2, "d=", s); if (!(s = sig->selector)) s = US""; logmsg = string_append(logmsg, 2, " s=", s); -logmsg = string_append(logmsg, 7, - " c=", sig->canon_headers == PDKIM_CANON_SIMPLE ? "simple" : "relaxed", - "/", sig->canon_body == PDKIM_CANON_SIMPLE ? "simple" : "relaxed", - " a=", dkim_sig_to_a_tag(sig), -string_sprintf(" b=" SIZE_T_FMT, - (int)sig->sighash.len > -1 ? sig->sighash.len * 8 : 0)); +logmsg = string_fmt_append(logmsg, " c=%s/%s a=%s b=" SIZE_T_FMT, + sig->canon_headers == PDKIM_CANON_SIMPLE ? "simple" : "relaxed", + sig->canon_body == PDKIM_CANON_SIMPLE ? "simple" : "relaxed", + dkim_sig_to_a_tag(sig), + (int)sig->sighash.len > -1 ? sig->sighash.len * 8 : (size_t)0); if ((s= sig->identity)) logmsg = string_append(logmsg, 2, " i=", s); -if (sig->created > 0) logmsg = string_cat(logmsg, - string_sprintf(" t=%lu", sig->created)); -if (sig->expires > 0) logmsg = string_cat(logmsg, - string_sprintf(" x=%lu", sig->expires)); -if (sig->bodylength > -1) logmsg = string_cat(logmsg, - string_sprintf(" l=%lu", sig->bodylength)); +if (sig->created > 0) logmsg = string_fmt_append(logmsg, " t=%lu", + sig->created); +if (sig->expires > 0) logmsg = string_fmt_append(logmsg, " x=%lu", + sig->expires); +if (sig->bodylength > -1) logmsg = string_fmt_append(logmsg, " l=%lu", + sig->bodylength); if (sig->verify_status & PDKIM_VERIFY_POLICY) logmsg = string_append(logmsg, 5, diff --git a/src/src/expand.c b/src/src/expand.c index fcf170dc3..0edd95dff 100644 --- a/src/src/expand.c +++ b/src/src/expand.c @@ -6674,7 +6674,6 @@ while (*s != 0) case EOP_BASE62D: { - uschar buf[16]; uschar *tt = sub; unsigned long int n = 0; while (*tt != 0) @@ -6689,8 +6688,7 @@ while (*s != 0) } n = n * BASE_62 + (t - base62_chars); } - (void)sprintf(CS buf, "%ld", n); - yield = string_cat(yield, buf); + yield = string_fmt_append(yield, "%ld", n); continue; } @@ -6739,11 +6737,10 @@ while (*s != 0) md5 base; uschar digest[16]; int j; - char st[33]; md5_start(&base); md5_end(&base, sub, Ustrlen(sub), digest); - for(j = 0; j < 16; j++) sprintf(st+2*j, "%02x", digest[j]); - yield = string_cat(yield, US st); + for (j = 0; j < 16; j++) + yield = string_fmt_append(yield, "%02x", digest[j]); } continue; @@ -6760,11 +6757,10 @@ while (*s != 0) hctx h; uschar digest[20]; int j; - char st[41]; sha1_start(&h); sha1_end(&h, sub, Ustrlen(sub), digest); - for(j = 0; j < 20; j++) sprintf(st+2*j, "%02X", digest[j]); - yield = string_catn(yield, US st, 40); + for (j = 0; j < 20; j++) + yield = string_fmt_append(yield, "%02X", digest[j]); } continue; @@ -6779,7 +6775,6 @@ while (*s != 0) { hctx h; blob b; - char st[3]; if (!exim_sha_init(&h, HASH_SHA2_256)) { @@ -6789,10 +6784,7 @@ while (*s != 0) exim_sha_update(&h, sub, Ustrlen(sub)); exim_sha_finish(&h, &b); while (b.len-- > 0) - { - sprintf(st, "%02X", *b.data++); - yield = string_catn(yield, US st, 2); - } + yield = string_fmt_append(yield, "%02X", *b.data++); } #else expand_string_message = US"sha256 only supported with TLS"; @@ -6804,7 +6796,6 @@ while (*s != 0) { hctx h; blob b; - char st[3]; hashmethod m = !arg ? HASH_SHA3_256 : Ustrcmp(arg, "224") == 0 ? HASH_SHA3_224 : Ustrcmp(arg, "256") == 0 ? HASH_SHA3_256 @@ -6821,10 +6812,7 @@ while (*s != 0) exim_sha_update(&h, sub, Ustrlen(sub)); exim_sha_finish(&h, &b); while (b.len-- > 0) - { - sprintf(st, "%02X", *b.data++); - yield = string_catn(yield, US st, 2); - } + yield = string_fmt_append(yield, "%02X", *b.data++); } continue; #else @@ -6888,7 +6876,7 @@ while (*s != 0) while (*(++t) != 0) { if (*t < 0x21 || 0x7E < *t) - yield = string_catn(yield, string_sprintf("\\x%02x", *t), 4); + yield = string_fmt_append(yield, "\\x%02x", *t); else yield = string_catn(yield, t, 1); } @@ -6901,12 +6889,10 @@ while (*s != 0) { int cnt = 0; int sep = 0; - uschar * cp; uschar buffer[256]; while (string_nextinlist(CUSS &sub, &sep, buffer, sizeof(buffer)) != NULL) cnt++; - cp = string_sprintf("%d", cnt); - yield = string_cat(yield, cp); + yield = string_fmt_append(yield, "%d", cnt); continue; } @@ -7481,7 +7467,7 @@ while (*s != 0) for (s = sub; (c = *s); s++) yield = c < 127 && c != '\\' ? string_catn(yield, s, 1) - : string_catn(yield, string_sprintf("\\%03o", c), 4); + : string_fmt_append(yield, "\\%03o", c); continue; } @@ -7493,15 +7479,14 @@ while (*s != 0) uschar *save_sub = sub; uschar *error = NULL; int_eximarith_t n = eval_expr(&sub, (c == EOP_EVAL10), &error, FALSE); - if (error != NULL) + if (error) { expand_string_message = string_sprintf("error in expression " "evaluation: %s (after processing \"%.*s\")", error, (int)(sub-save_sub), save_sub); goto EXPAND_FAILED; } - sprintf(CS var_buffer, PR_EXIM_ARITH, n); - yield = string_cat(yield, var_buffer); + yield = string_fmt_append(yield, PR_EXIM_ARITH, n); continue; } @@ -7516,8 +7501,7 @@ while (*s != 0) "Exim time interval in \"%s\" operator", sub, name); goto EXPAND_FAILED; } - sprintf(CS var_buffer, "%d", n); - yield = string_cat(yield, var_buffer); + yield = string_fmt_append(yield, "%d", n); continue; } @@ -7569,12 +7553,8 @@ while (*s != 0) /* strlen returns the length of the string */ case EOP_STRLEN: - { - uschar buff[24]; - (void)sprintf(CS buff, "%d", Ustrlen(sub)); - yield = string_cat(yield, buff); + yield = string_fmt_append(yield, "%d", Ustrlen(sub)); continue; - } /* length_n or l_n takes just the first n characters or the whole string, whichever is the shorter; @@ -7607,7 +7587,7 @@ while (*s != 0) int len; uschar *ret; - if (arg == NULL) + if (!arg) { expand_string_message = string_sprintf("missing values after %s", name); @@ -7671,14 +7651,13 @@ while (*s != 0) case EOP_STAT: { - uschar *s; uschar smode[12]; uschar **modetable[3]; int i; mode_t mode; struct stat st; - if ((expand_forbid & RDO_EXISTS) != 0) + if (expand_forbid & RDO_EXISTS) { expand_string_message = US"Use of the stat() expansion is not permitted"; goto EXPAND_FAILED; @@ -7712,13 +7691,13 @@ while (*s != 0) } smode[10] = 0; - s = string_sprintf("mode=%04lo smode=%s inode=%ld device=%ld links=%ld " + yield = string_fmt_append(yield, + "mode=%04lo smode=%s inode=%ld device=%ld links=%ld " "uid=%ld gid=%ld size=" OFF_T_FMT " atime=%ld mtime=%ld ctime=%ld", (long)(st.st_mode & 077777), smode, (long)st.st_ino, (long)st.st_dev, (long)st.st_nlink, (long)st.st_uid, (long)st.st_gid, st.st_size, (long)st.st_atime, (long)st.st_mtime, (long)st.st_ctime); - yield = string_cat(yield, s); continue; } @@ -7726,14 +7705,11 @@ while (*s != 0) case EOP_RANDINT: { - int_eximarith_t max; - uschar *s; + int_eximarith_t max = expanded_string_integer(sub, TRUE); - max = expanded_string_integer(sub, TRUE); - if (expand_string_message != NULL) + if (expand_string_message) goto EXPAND_FAILED; - s = string_sprintf("%d", vaguely_random_number((int)max)); - yield = string_cat(yield, s); + yield = string_fmt_append(yield, "%d", vaguely_random_number((int)max)); continue; } @@ -7759,9 +7735,9 @@ while (*s != 0) /* Unknown operator */ default: - expand_string_message = - string_sprintf("unknown expansion operator \"%s\"", name); - goto EXPAND_FAILED; + expand_string_message = + string_sprintf("unknown expansion operator \"%s\"", name); + goto EXPAND_FAILED; } } diff --git a/src/src/lookups/redis.c b/src/src/lookups/redis.c index e9ddf884c..a4b672a5e 100644 --- a/src/src/lookups/redis.c +++ b/src/src/lookups/redis.c @@ -291,7 +291,7 @@ switch (redis_reply->type) switch (entry->type) { case REDIS_REPLY_INTEGER: - result = string_cat(result, string_sprintf("%d", entry->integer)); + result = string_fmt_append(result, "%d", entry->integer); break; case REDIS_REPLY_STRING: result = string_catn(result, US entry->str, entry->len); @@ -307,7 +307,7 @@ switch (redis_reply->type) switch (tentry->type) { case REDIS_REPLY_INTEGER: - result = string_cat(result, string_sprintf("%d", tentry->integer)); + result = string_fmt_append(result, "%d", tentry->integer); break; case REDIS_REPLY_STRING: result = string_catn(result, US tentry->str, tentry->len); diff --git a/src/src/receive.c b/src/src/receive.c index c86ad2f48..e9877d3a0 100644 --- a/src/src/receive.c +++ b/src/src/receive.c @@ -1306,11 +1306,8 @@ if (sender_fullhost) if (LOGGING(dnssec) && sender_host_dnssec) /*XXX sender_helo_dnssec? */ g = string_catn(g, US" DS", 3); g = string_append(g, 2, US" H=", sender_fullhost); - if (LOGGING(incoming_interface) && interface_address != NULL) - { - g = string_cat(g, - string_sprintf(" I=[%s]:%d", interface_address, interface_port)); - } + if (LOGGING(incoming_interface) && interface_address) + g = string_fmt_append(g, " I=[%s]:%d", interface_address, interface_port); } if (f.tcp_in_fastopen && !f.tcp_in_fastopen_logged) { diff --git a/src/src/smtp_in.c b/src/src/smtp_in.c index 54ebf3660..dbd428011 100644 --- a/src/src/smtp_in.c +++ b/src/src/smtp_in.c @@ -1777,7 +1777,7 @@ if (f.sender_host_unknown || f.sender_host_notsocket) if (f.is_inetd) return string_sprintf("SMTP connection from %s (via inetd)", hostname); -if (LOGGING(incoming_interface) && interface_address != NULL) +if (LOGGING(incoming_interface) && interface_address) return string_sprintf("SMTP connection from %s I=[%s]:%d", hostname, interface_address, interface_port); @@ -4262,20 +4262,15 @@ while (done <= 0) smtp_code = US"250 "; /* Default response code plus space*/ if (!user_msg) { - s = string_sprintf("%.3s %s Hello %s%s%s", + g = string_fmt_append(NULL, "%.3s %s Hello %s%s%s", smtp_code, smtp_active_hostname, sender_ident ? sender_ident : US"", sender_ident ? US" at " : US"", sender_host_name ? sender_host_name : sender_helo_name); - g = string_cat(NULL, s); if (sender_host_address) - { - g = string_catn(g, US" [", 2); - g = string_cat (g, sender_host_address); - g = string_catn(g, US"]", 1); - } + g = string_fmt_append(g, " [%s]", sender_host_address); } /* A user-supplied EHLO greeting may not contain more than one line. Note @@ -4313,11 +4308,8 @@ while (done <= 0) till then, VRFY and EXPN can be used after EHLO when space is short. */ if (thismessage_size_limit > 0) - { - sprintf(CS big_buffer, "%.3s-SIZE %d\r\n", smtp_code, + g = string_fmt_append(g, "%.3s-SIZE %d\r\n", smtp_code, thismessage_size_limit); - g = string_cat(g, big_buffer); - } else { g = string_catn(g, smtp_code, 3); diff --git a/src/src/transports/smtp.c b/src/src/transports/smtp.c index 7eb6abaeb..1e4cd2cc9 100644 --- a/src/src/transports/smtp.c +++ b/src/src/transports/smtp.c @@ -668,22 +668,22 @@ Returns: nothing static void write_logs(const host_item *host, const uschar *suffix, int basic_errno) { -uschar *message = LOGGING(outgoing_port) - ? string_sprintf("H=%s [%s]:%d", host->name, host->address, +gstring * message = LOGGING(outgoing_port) + ? string_fmt_append(NULL, "H=%s [%s]:%d", host->name, host->address, host->port == PORT_NONE ? 25 : host->port) - : string_sprintf("H=%s [%s]", host->name, host->address); + : string_fmt_append(NULL, "H=%s [%s]", host->name, host->address); if (suffix) { - message = string_sprintf("%s: %s", message, suffix); + message = string_fmt_append(message, ": %s", suffix); if (basic_errno > 0) - message = string_sprintf("%s: %s", message, strerror(basic_errno)); + message = string_fmt_append(message, ": %s", strerror(basic_errno)); } else - message = string_sprintf("%s %s", message, exim_errstr(basic_errno)); + message = string_fmt_append(message, " %s", exim_errstr(basic_errno)); -log_write(0, LOG_MAIN, "%s", message); -deliver_msglog("%s %s\n", tod_stamp(tod_log), message); +log_write(0, LOG_MAIN, "%s", string_from_gstring(message)); +deliver_msglog("%s %s\n", tod_stamp(tod_log), message->s); } static void -- 2.30.2