From b841da5085a41642d5ce2bfe3faea596b6d15db1 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sun, 11 Dec 2016 14:54:44 +0100 Subject: [PATCH] Moved httplib_vsnprintf to own file --- Makefile | 1 + src/httplib_vsnprintf.c | 63 +++++++++++++++++++++++++++++++++++++++++ src/libhttp.c | 35 ----------------------- 3 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 src/httplib_vsnprintf.c diff --git a/Makefile b/Makefile index f4eafd27..1e67bb83 100644 --- a/Makefile +++ b/Makefile @@ -200,6 +200,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_url_encode.c \ src/httplib_version.c \ src/httplib_vprintf.c \ + src/httplib_vsnprintf.c \ src/httplib_websocket_client_thread.c \ src/httplib_websocket_client_write.c \ src/httplib_websocket_write.c \ diff --git a/src/httplib_vsnprintf.c b/src/httplib_vsnprintf.c new file mode 100644 index 00000000..ca7d5de4 --- /dev/null +++ b/src/httplib_vsnprintf.c @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#include "libhttp-private.h" + + + +/* Return null terminated string of given maximum length. + * Report errors if length is exceeded. */ +void XX_httplib_vsnprintf( const struct mg_connection *conn, int *truncated, char *buf, size_t buflen, const char *fmt, va_list ap ) { + + int n; + int ok; + + if (buflen == 0) return; + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wformat-nonliteral" +/* Using fmt as a non-literal is intended here, since it is mostly called + * indirectly by XX_httplib_snprintf */ +#endif + + n = (int)vsnprintf_impl(buf, buflen, fmt, ap); + ok = (n >= 0) && ((size_t)n < buflen); + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + + if (ok) { + if (truncated) *truncated = 0; + } else { + if (truncated) *truncated = 1; + mg_cry(conn, "truncating vsnprintf buffer: [%.*s]", (int)((buflen > 200) ? 200 : (buflen - 1)), buf); + n = (int)buflen - 1; + } + buf[n] = '\0'; + +} /* XX_httplib_vsnprintf */ diff --git a/src/libhttp.c b/src/libhttp.c index 66304932..62cb2be9 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -828,41 +828,6 @@ void XX_httplib_set_thread_name(const char *threadName) { -/* Return null terminated string of given maximum length. - * Report errors if length is exceeded. */ -void XX_httplib_vsnprintf( const struct mg_connection *conn, int *truncated, char *buf, size_t buflen, const char *fmt, va_list ap ) { - - int n; - int ok; - - if (buflen == 0) return; - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wformat-nonliteral" -/* Using fmt as a non-literal is intended here, since it is mostly called - * indirectly by XX_httplib_snprintf */ -#endif - - n = (int)vsnprintf_impl(buf, buflen, fmt, ap); - ok = (n >= 0) && ((size_t)n < buflen); - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - - if (ok) { - if (truncated) *truncated = 0; - } else { - if (truncated) *truncated = 1; - mg_cry(conn, "truncating vsnprintf buffer: [%.*s]", (int)((buflen > 200) ? 200 : (buflen - 1)), buf); - n = (int)buflen - 1; - } - buf[n] = '\0'; - -} /* XX_httplib_vsnprintf */ - - void XX_httplib_snprintf( const struct mg_connection *conn, int *truncated, char *buf, size_t buflen, const char *fmt, ... ) { va_list ap;