From e802821e4d62ee2c53849e031706ed45fa843a1d Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Sun, 2 Feb 2020 07:47:53 -0800 Subject: [PATCH] stb_sprintf: PR 613 --- stb_sprintf.h | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/stb_sprintf.h b/stb_sprintf.h index f55d417..5f278ac 100644 --- a/stb_sprintf.h +++ b/stb_sprintf.h @@ -18,7 +18,7 @@ // Leonard Ritter // Stefano Zanotti // Adam Allison -// Kevin Schmidt +// Arvid Gerstmann // // LICENSE: // @@ -140,37 +140,25 @@ PERFORMANCE vs MSVC 2008 32-/64-bit (GCC is even slower than MSVC): "...512 char string..." ( 35.0x/32.5x faster!) */ -#if defined(__clang__) - #if defined(__has_feature) && defined(__has_attribute) - #if __has_feature(address_sanitizer) - #if __has_attribute(__no_sanitize__) - #define STBSP__ASAN __attribute__((__no_sanitize__("address"))) - #elif __has_attribute(__no_sanitize_address__) - #define STBSP__ASAN __attribute__((__no_sanitize_address__)) - #elif __has_attribute(__no_address_safety_analysis__) - #define STBSP__ASAN __attribute__((__no_address_safety_analysis__)) +#if defined(__has_feature) + #if __has_feature(address_sanitizer) + #define STBI__ASAN __attribute__((no_sanitize("address"))) #endif - #endif - #endif -#elif __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) - #if __SANITIZE_ADDRESS__ - #define STBSP__ASAN __attribute__((__no_sanitize_address__)) - #endif #endif -#ifndef STBSP__ASAN -#define STBSP__ASAN +#ifndef STBI__ASAN +#define STBI__ASAN #endif #ifdef STB_SPRINTF_STATIC #define STBSP__PUBLICDEC static -#define STBSP__PUBLICDEF static STBSP__ASAN +#define STBSP__PUBLICDEF static STBI__ASAN #else #ifdef __cplusplus #define STBSP__PUBLICDEC extern "C" -#define STBSP__PUBLICDEF extern "C" STBSP__ASAN +#define STBSP__PUBLICDEF extern "C" STBI__ASAN #else #define STBSP__PUBLICDEC extern -#define STBSP__PUBLICDEF STBSP__ASAN +#define STBSP__PUBLICDEF STBI__ASAN #endif #endif @@ -1355,12 +1343,14 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, . typedef struct stbsp__context { char *buf; int count; + int length; char tmp[STB_SPRINTF_MIN]; } stbsp__context; static char *stbsp__clamp_callback(char *buf, void *user, int len) { stbsp__context *c = (stbsp__context *)user; + c->length += len; if (len > c->count) len = c->count; @@ -1380,7 +1370,7 @@ static char *stbsp__clamp_callback(char *buf, void *user, int len) } if (c->count <= 0) - return 0; + return c->tmp; return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can } @@ -1388,29 +1378,27 @@ static char * stbsp__count_clamp_callback( char * buf, void * user, int len ) { stbsp__context * c = (stbsp__context*)user; - c->count += len; + c->length += len; return c->tmp; // go direct into buffer if you can } STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va ) { stbsp__context c; - int l; if ( (count == 0) && !buf ) { - c.count = 0; + c.length = 0; STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va ); - l = c.count; } else { - if ( count == 0 ) - return 0; + int l; c.buf = buf; c.count = count; + c.length = 0; STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va ); @@ -1421,7 +1409,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c buf[l] = 0; } - return l; + return c.length; } STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)