1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved httplib_vsnprintf to own file

This commit is contained in:
Lammert Bies
2016-12-11 14:54:44 +01:00
parent 1154a5c0de
commit b841da5085
3 changed files with 64 additions and 35 deletions

View File

@@ -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 \

63
src/httplib_vsnprintf.c Normal file
View File

@@ -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 */

View File

@@ -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;