From 2b87c219ca07aebcf6e8ff02009dca88babc04c3 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Tue, 7 May 2019 20:26:05 -0700 Subject: Introduce nni_plat_printf() This permits the stats dump to avoid some extra buffering, and resolves a complaint about possible format buffer overruns. --- src/core/platform.h | 5 +++++ src/core/stats.c | 35 +++++++++++++++------------------- src/platform/posix/posix_debug.c | 12 +++++++++++- src/platform/windows/win_debug.c | 11 ++++++++++- src/supplemental/websocket/websocket.c | 1 + 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/core/platform.h b/src/core/platform.h index e415b438..1418ba4f 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -52,6 +52,11 @@ extern void nni_plat_abort(void); // not contain newlines, but the output will add them. extern void nni_plat_println(const char *); +// nni_plat_printf is like printf. It should conform to C99 standard printf, +// but is a function to allow platform ports to redirect. It should go to +// the same place that nni_plat_println does. +extern void nni_plat_printf(const char *, ...); + // nni_plat_strerror allows the platform to use additional error messages // for additional error codes. The err code passed in should be the // equivalent of errno or GetLastError, without the NNG_ESYSERR component. diff --git a/src/core/stats.c b/src/core/stats.c index afeafff6..f3f969c3 100644 --- a/src/core/stats.c +++ b/src/core/stats.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. +// Copyright 2019 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -453,7 +453,6 @@ nng_stats_dump(nng_stat *stat) { #ifdef NNG_ENABLE_STATS static char buf[128]; // to minimize recursion, not thread safe - static char line[128]; int len; char * scope; char * indent = " "; @@ -472,53 +471,49 @@ nng_stats_dump(nng_stat *stat) } } if (len > 0) { - snprintf(line, sizeof(line), "\n%s:", buf); + nni_plat_printf("\n%s:\n", buf); } break; case NNG_STAT_STRING: - snprintf(line, sizeof(line), "%s%-32s\"%s\"", indent, - nng_stat_name(stat), nng_stat_string(stat)); + nni_plat_printf("%s%-32s\"%s\"\n", indent, nng_stat_name(stat), + nng_stat_string(stat)); break; case NNG_STAT_BOOLEAN: val = nng_stat_value(stat); - snprintf(line, sizeof(line), "%s%-32s%s", indent, - nng_stat_name(stat), val != 0 ? "true" : "false"); + nni_plat_printf("%s%-32s%s\n", indent, nng_stat_name(stat), + val != 0 ? "true" : "false"); break; case NNG_STAT_LEVEL: case NNG_STAT_COUNTER: val = nng_stat_value(stat); + nni_plat_printf( + "%s%-32s%llu", indent, nng_stat_name(stat), val); switch (nng_stat_unit(stat)) { case NNG_UNIT_BYTES: - snprintf(line, sizeof(line), "%s%-32s%llu bytes", - indent, nng_stat_name(stat), val); + nni_plat_printf(" bytes\n"); break; case NNG_UNIT_MESSAGES: - snprintf(line, sizeof(line), "%s%-32s%llu msgs", - indent, nng_stat_name(stat), val); + nni_plat_printf(" msgs\n"); break; case NNG_UNIT_MILLIS: - snprintf(line, sizeof(line), "%s%-32s%llu msec", - indent, nng_stat_name(stat), val); + nni_plat_printf(" msec\n"); break; case NNG_UNIT_NONE: case NNG_UNIT_EVENTS: default: - snprintf(line, sizeof(line), "%s%-32s%llu", indent, - nng_stat_name(stat), val); + nni_plat_printf("\n"); break; } break; case NNG_STAT_ID: val = nng_stat_value(stat); - snprintf(line, (sizeof line), "%s%-32s%llu", indent, - nng_stat_name(stat), val); + nni_plat_printf( + "%s%-32s%llu\n", indent, nng_stat_name(stat), val); break; default: - snprintf(line, (sizeof line), "%s%-32s", indent, - nng_stat_name(stat)); + nni_plat_printf("%s%-32s\n", indent, nng_stat_name(stat)); break; } - nni_plat_println(line); NNI_LIST_FOREACH (&stat->s_children, child) { nng_stats_dump(child); diff --git a/src/platform/posix/posix_debug.c b/src/platform/posix/posix_debug.c index 7619e8e8..80f37225 100644 --- a/src/platform/posix/posix_debug.c +++ b/src/platform/posix/posix_debug.c @@ -1,5 +1,5 @@ // -// Copyright 2017 Garrett D'Amore +// Copyright 2019 Staysail Systems, Inc. // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -12,6 +12,7 @@ #ifdef NNG_PLATFORM_POSIX #include +#include #include #include #include @@ -22,6 +23,15 @@ nni_plat_abort(void) abort(); } +void +nni_plat_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, ap); + (void) vprintf(fmt, ap); + va_end(ap); +} + void nni_plat_println(const char *message) { diff --git a/src/platform/windows/win_debug.c b/src/platform/windows/win_debug.c index a47e7b41..a37411cf 100644 --- a/src/platform/windows/win_debug.c +++ b/src/platform/windows/win_debug.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. +// Copyright 2019 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // This software is supplied under the terms of the MIT License, a @@ -23,6 +23,15 @@ nni_plat_abort(void) abort(); } +void +nni_plat_printf(const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + (void) vprintf(fmt, ap); + va_end(ap); +} + void nni_plat_println(const char *message) { diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c index fcd2eeb3..91affc62 100644 --- a/src/supplemental/websocket/websocket.c +++ b/src/supplemental/websocket/websocket.c @@ -1443,6 +1443,7 @@ ws_listener_free(void *arg) if (l->handler != NULL) { nni_http_handler_fini(l->handler); + l->handler = NULL; } if (l->server != NULL) { nni_http_server_fini(l->server); -- cgit v1.2.3-70-g09d2