From af72febebc2e6d3a138b47b800552311d803ef46 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 23 Nov 2018 13:05:38 +0100 Subject: [PATCH] str_itoa for platforms that do not have itoa. remove more static string buffer warning code (bsdstring) from reports. --- src/reports.c | 25 ++++++++++--------------- src/util/strings.c | 6 ++++++ src/util/strings.h | 1 + 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/reports.c b/src/reports.c index 3ce83a7de..db06e81a4 100644 --- a/src/reports.c +++ b/src/reports.c @@ -2198,28 +2198,23 @@ static void eval_resources(struct opstack **stack, const void *userdata) const struct locale *lang = f ? f->locale : default_locale; const struct resource *res = (const struct resource *)opop(stack).v; char buf[1024]; /* but we only use about half of this */ - size_t size = sizeof(buf) - 1; variant var; - - char *bufp = buf; - while (res != NULL && size > 4) { + sbstring sbs; + + sbs_init(&sbs, buf, sizeof(buf)); + while (res != NULL) { const char *rname = resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0); - int result = snprintf(bufp, size, "%d %s", res->number, LOC(lang, rname)); - if (wrptr(&bufp, &size, result) != 0 || size < sizeof(buf) / 2) { - WARN_STATIC_BUFFER(); - break; - } + sbs_strcat(&sbs, str_itoa(res->number)); + sbs_strcat(&sbs, ""); + sbs_strcat(&sbs, LOC(lang, rname)); res = res->next; - if (res != NULL && size > 2) { - strcat(bufp, ", "); - bufp += 2; - size -= 2; + if (res != NULL) { + sbs_strcat(&sbs, ", "); } } - *bufp = 0; - var.v = strcpy(balloc((size_t)(bufp - buf + 1)), buf); + var.v = strcpy(balloc(sbs_length(&sbs)), buf); opush(stack, var); } diff --git a/src/util/strings.c b/src/util/strings.c index e9434fb25..682d92139 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -34,6 +34,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #endif +const char *str_itoa(int n) { + static char buf[12]; + snprintf(buf, sizeof(buf), "%d", n); + return buf; +} + size_t str_strlcpy(char *dst, const char *src, size_t len) { #ifdef HAVE_BSDSTRING diff --git a/src/util/strings.h b/src/util/strings.h index bd58c0eb2..205cf6b52 100644 --- a/src/util/strings.h +++ b/src/util/strings.h @@ -28,6 +28,7 @@ extern "C" { void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value); int str_hash(const char *s); + const char *str_itoa(int i); size_t str_slprintf(char * dst, size_t size, const char * format, ...); size_t str_strlcpy(char *dst, const char *src, size_t len); size_t str_strlcat(char *dst, const char *src, size_t len);