From 412e1ecc1620fd842b0d3ade2238c8918f5fb373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Sj=C3=B6strand?= Date: Tue, 15 Sep 2015 19:17:42 +0200 Subject: [PATCH 1/9] compile fix for MinGW --- stb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb.h b/stb.h index d8aaf37..2e98e9d 100644 --- a/stb.h +++ b/stb.h @@ -206,7 +206,7 @@ CREDITS #endif #endif -#ifdef _WIN32 +#if defined(_WIN32) && !defined(__MINGW32__) #define _CRT_SECURE_NO_WARNINGS #define _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NON_CONFORMING_SWPRINTFS From ba406835155888c048da2c41eeb909fe204123cf Mon Sep 17 00:00:00 2001 From: Michael Sartain Date: Fri, 30 Oct 2015 19:10:03 -0700 Subject: [PATCH 2/9] readdir_raw was skipping all dirnames starting with dots. Only skip . and .. --- stb.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/stb.h b/stb.h index d8aaf37..c57079e 100644 --- a/stb.h +++ b/stb.h @@ -5863,6 +5863,13 @@ void stb_readdir_free(char **files) stb_arr_free(f2); } +static int isdotdirname(char *name) +{ + if (name[0] == '.') + return (name[1] == '.') ? !name[2] : !name[1]; + return 0; +} + STB_EXTERN int stb_wildmatchi(char *expr, char *candidate); static char **readdir_raw(char *dir, int return_subdirs, char *mask) { @@ -5931,7 +5938,7 @@ static char **readdir_raw(char *dir, int return_subdirs, char *mask) #endif if (is_subdir == return_subdirs) { - if (!is_subdir || name[0] != '.') { + if (!is_subdir || !isdotdirname(name)) { if (!mask || stb_wildmatchi(mask, name)) { char buffer[512],*p=buffer; sprintf(buffer, "%s%s", with_slash, name); From 8d9302ab05294b5fecc4bd445651521b8cc7fc38 Mon Sep 17 00:00:00 2001 From: Michael Sartain Date: Sun, 1 Nov 2015 17:47:11 -0800 Subject: [PATCH 3/9] readdir_raw buffer overrun fixes. - Add stb_vsnprintf, stb_snprintf functions. - stb snprintf routines should return -1 on buffer truncation on all platforms. - Add stb_strscpy string copy function which should behave the same as Linux kernel strscpy. - Bump readdir_raw buffer sizes up to 4k, add checks for path truncations. - Use d_type to check for directory instead of opendir/closedir. This should be faster and fix recursive symlnk death. --- stb.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 58 insertions(+), 27 deletions(-) diff --git a/stb.h b/stb.h index c57079e..187619d 100644 --- a/stb.h +++ b/stb.h @@ -714,20 +714,41 @@ STB_EXTERN char * stb_sstrdup(char *s); STB_EXTERN void stbprint(const char *fmt, ...); STB_EXTERN char *stb_sprintf(const char *fmt, ...); STB_EXTERN char *stb_mprintf(const char *fmt, ...); +STB_EXTERN int stb_snprintf(char *s, size_t n, const char *fmt, ...); +STB_EXTERN int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v); #ifdef STB_DEFINE +int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v) +{ + int res; + #ifdef _WIN32 + // Could use "_vsnprintf_s(s, n, _TRUNCATE, fmt, v)" ? + res = _vsnprintf(s,n,fmt,v); + #else + res = vsnprintf(s,n,fmt,v); + #endif + if (n) s[n-1] = 0; + // Unix returns length output would require, Windows returns negative when truncated. + return (res >= n || res < 0) ? -1 : res; +} + +int stb_snprintf(char *s, size_t n, const char *fmt, ...) +{ + int res; + va_list v; + va_start(v,fmt); + res = stb_vsnprintf(s, n, fmt, v); + va_end(v); + return res; +} + char *stb_sprintf(const char *fmt, ...) { static char buffer[1024]; va_list v; va_start(v,fmt); - #ifdef _WIN32 - _vsnprintf(buffer, 1024, fmt, v); - #else - vsnprintf(buffer, 1024, fmt, v); - #endif + stb_vsnprintf(buffer,1024,fmt,v); va_end(v); - buffer[1023] = 0; return buffer; } @@ -736,13 +757,8 @@ char *stb_mprintf(const char *fmt, ...) static char buffer[1024]; va_list v; va_start(v,fmt); - #ifdef _WIN32 - _vsnprintf(buffer, 1024, fmt, v); - #else - vsnprintf(buffer, 1024, fmt, v); - #endif + stb_vsnprintf(buffer,1024,fmt,v); va_end(v); - buffer[1023] = 0; return strdup(buffer); } @@ -842,9 +858,8 @@ void stbprint(const char *fmt, ...) va_list v; va_start(v,fmt); - res = _vsnprintf(buffer, sizeof(buffer), fmt, v); + res = stb_vsnprintf(buffer, sizeof(buffer), fmt, v); va_end(v); - buffer[sizeof(buffer)-1] = 0; if (res < 0) { tbuf = (char *) malloc(16384); @@ -1765,6 +1780,7 @@ STB_EXTERN char * stb_strichr(char *s, char t); STB_EXTERN char * stb_stristr(char *s, char *t); STB_EXTERN int stb_prefix_count(char *s, char *t); STB_EXTERN char * stb_plural(int n); // "s" or "" +STB_EXTERN size_t stb_strscpy(char *d, const char *s, size_t n); STB_EXTERN char **stb_tokens(char *src, char *delimit, int *count); STB_EXTERN char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out); @@ -1779,6 +1795,17 @@ STB_EXTERN char **stb_tokens_quoted(char *src, char *delimit, int *count); #ifdef STB_DEFINE +size_t stb_strscpy(char *d, const char *s, size_t n) +{ + size_t len = strlen(s); + if (len >= n) { + if (n) d[0] = 0; + return 0; + } + strcpy(d,s); + return len + 1; +} + char *stb_plural(int n) { return n == 1 ? "" : "s"; @@ -5874,7 +5901,7 @@ STB_EXTERN int stb_wildmatchi(char *expr, char *candidate); static char **readdir_raw(char *dir, int return_subdirs, char *mask) { char **results = NULL; - char buffer[512], with_slash[512]; + char buffer[4096], with_slash[4096]; size_t n; #ifdef _MSC_VER @@ -5892,25 +5919,28 @@ static char **readdir_raw(char *dir, int return_subdirs, char *mask) DIR *z; #endif - strcpy(buffer,dir); + n = stb_strscpy(buffer,dir,sizeof(buffer)); + if (!n || n >= sizeof(buffer)) + return NULL; stb_fixpath(buffer); - n = strlen(buffer); + n--; if (n > 0 && (buffer[n-1] != '/')) { buffer[n++] = '/'; } buffer[n] = 0; - strcpy(with_slash, buffer); + if (!stb_strscpy(with_slash,buffer,sizeof(with_slash))) + return NULL; #ifdef _MSC_VER - strcpy(buffer+n, "*.*"); + if (!stb_strscpy(buffer+n,"*.*",sizeof(buffer)-n)) + return NULL; ws = stb__from_utf8(buffer); z = _wfindfirst((const wchar_t *)ws, &data); #else z = opendir(dir); #endif - if (z != none) { int nonempty = STB_TRUE; #ifndef _MSC_VER @@ -5931,17 +5961,18 @@ static char **readdir_raw(char *dir, int return_subdirs, char *mask) is_subdir = !!(data.attrib & _A_SUBDIR); #else char *name = data->d_name; - strcpy(buffer+n,name); - DIR *y = opendir(buffer); - is_subdir = (y != NULL); - if (y != NULL) closedir(y); + if (!stb_strscpy(buffer+n,name,sizeof(buffer)-n)) + break; + // Could follow DT_LNK, but would need to check for recursive links. + is_subdir = !!(data->d_type & DT_DIR); #endif - + if (is_subdir == return_subdirs) { if (!is_subdir || !isdotdirname(name)) { if (!mask || stb_wildmatchi(mask, name)) { - char buffer[512],*p=buffer; - sprintf(buffer, "%s%s", with_slash, name); + char buffer[4096],*p=buffer; + if ( stb_snprintf(buffer, sizeof(buffer), "%s%s", with_slash, name) < 0 ) + break; if (buffer[0] == '.' && buffer[1] == '/') p = buffer+2; stb_arr_push(results, strdup(p)); From cbfa0c44184748728a600fd69e14c5937497618c Mon Sep 17 00:00:00 2001 From: blackpawn Date: Wed, 2 Dec 2015 01:16:29 -0600 Subject: [PATCH 4/9] Fix stb_arr_insertn and stb_arr_deleten memmove lengths They were moving memory beyond the array bounds. --- stb.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb.h b/stb.h index d8aaf37..2d9dab6 100644 --- a/stb.h +++ b/stb.h @@ -3249,7 +3249,7 @@ void stb__arr_insertn_(void **pp, int size, int i, int n STB__PARAMS) z = stb_arr_len2(p); stb__arr_addlen_(&p, size, i STB__ARGS); - memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-i)); + memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-(i+n))); } *pp = p; } @@ -3258,7 +3258,7 @@ void stb__arr_deleten_(void **pp, int size, int i, int n STB__PARAMS) { void *p = *pp; if (n) { - memmove((char *) p + i*size, (char *) p + (i+n)*size, size * (stb_arr_len2(p)-i)); + memmove((char *) p + i*size, (char *) p + (i+n)*size, size * (stb_arr_len2(p)-(i+n))); stb_arrhead2(p)->len -= n; } *pp = p; From 28f1b0f5698fe4e05410250895ee7f75d0db4559 Mon Sep 17 00:00:00 2001 From: blackpawn Date: Wed, 2 Dec 2015 22:34:04 -0600 Subject: [PATCH 5/9] Fix for stb_arr_insert --- stb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb.h b/stb.h index 2d9dab6..15ecf51 100644 --- a/stb.h +++ b/stb.h @@ -3062,7 +3062,7 @@ typedef struct #define stb_arr_insertn(a,i,n) (stb__arr_insertn((void **) &(a), sizeof(*a), i, n)) // insert an element at i -#define stb_arr_insert(a,i,v) (stb__arr_insertn((void **) &(a), sizeof(*a), i, n), ((a)[i] = v)) +#define stb_arr_insert(a,i,v) (stb__arr_insertn((void **) &(a), sizeof(*a), i, 1), ((a)[i] = v)) // delete N elements from the middle starting at index 'i' #define stb_arr_deleten(a,i,n) (stb__arr_deleten((void **) &(a), sizeof(*a), i, n)) From a4ab8c08eb4bd74dc22d98e0b1bb70372121fa36 Mon Sep 17 00:00:00 2001 From: blackpawn Date: Wed, 2 Dec 2015 23:12:12 -0600 Subject: [PATCH 6/9] Corrected fix for stb_insertn On insert the memmove length wasn't incorrect but the addlen call was. --- stb.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb.h b/stb.h index 15ecf51..eb2b7f6 100644 --- a/stb.h +++ b/stb.h @@ -3248,8 +3248,8 @@ void stb__arr_insertn_(void **pp, int size, int i, int n STB__PARAMS) } z = stb_arr_len2(p); - stb__arr_addlen_(&p, size, i STB__ARGS); - memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-(i+n))); + stb__arr_addlen_(&p, size, n STB__ARGS); + memmove((char *) p + (i+n)*size, (char *) p + i*size, size * (z-i)); } *pp = p; } From bc1b1f6cc993d87aff438cfd1fb550a36fe2780d Mon Sep 17 00:00:00 2001 From: Eugene Opalev Date: Mon, 18 Jan 2016 19:38:07 -0800 Subject: [PATCH 7/9] Warning C4703 fix --- stb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb.h b/stb.h index d8aaf37..b152c91 100644 --- a/stb.h +++ b/stb.h @@ -7786,7 +7786,7 @@ stb_ps *stb_ps_remove_any(stb_ps *ps, void **value) void ** stb_ps_getlist(stb_ps *ps, int *count) { int i,n=0; - void **p; + void **p = NULL; switch (3 & (int) ps) { case STB_ps_direct: if (ps == NULL) { *count = 0; return NULL; } From a5bbc93087575e82f927cbf9e9e3e2a5c1e2c4d9 Mon Sep 17 00:00:00 2001 From: Eugene Opalev Date: Mon, 18 Jan 2016 22:41:17 -0800 Subject: [PATCH 8/9] Warning C4005 fix --- stb.h | 6 ++++++ stb_tilemap_editor.h | 2 ++ 2 files changed, 8 insertions(+) diff --git a/stb.h b/stb.h index b152c91..818554e 100644 --- a/stb.h +++ b/stb.h @@ -207,9 +207,15 @@ CREDITS #endif #ifdef _WIN32 + #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS + #endif + #ifndef _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE + #endif + #ifndef _CRT_NON_CONFORMING_SWPRINTFS #define _CRT_NON_CONFORMING_SWPRINTFS + #endif #if !defined(_MSC_VER) || _MSC_VER > 1700 #include // _BitScanReverse #endif diff --git a/stb_tilemap_editor.h b/stb_tilemap_editor.h index 64480ba..f1f8155 100644 --- a/stb_tilemap_editor.h +++ b/stb_tilemap_editor.h @@ -328,7 +328,9 @@ #define STB_TILEMAP_INCLUDE_STB_TILEMAP_EDITOR_H #ifdef _WIN32 + #ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS + #endif #include #include #endif From 820f63be441101bbb255d5b583a1495a0884c91a Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Fri, 22 Jan 2016 05:17:12 -0800 Subject: [PATCH 9/9] stb_tilemap_editor: fix typo in docuemtnation, update version & credits --- stb.h | 6 +++++- stb_tilemap_editor.h | 6 ++++-- tests/stb.dsp | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/stb.h b/stb.h index cd86718..4b28ae1 100644 --- a/stb.h +++ b/stb.h @@ -1,4 +1,4 @@ -/* stb.h - v2.25 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h +/* stb.h - v2.26 - Sean's Tool Box -- public domain -- http://nothings.org/stb.h no warranty is offered or implied; use this code at your own risk This is a single header file with a bunch of useful utilities @@ -25,6 +25,7 @@ Version History + 2.26 various warning & buffixes 2.25 various warning & bugfixes 2.24 various warning & bugfixes 2.23 fix 2.22 @@ -186,6 +187,9 @@ CREDITS Mojofreem@github Ryan Whitworth Vincent Isambart + Mike Sartain + Eugene Opalev + Tim Sjostrand */ #ifndef STB__INCLUDE_STB_H diff --git a/stb_tilemap_editor.h b/stb_tilemap_editor.h index f1f8155..b244c9d 100644 --- a/stb_tilemap_editor.h +++ b/stb_tilemap_editor.h @@ -1,4 +1,4 @@ -// stb_tilemap_editor.h - v0.36 - Sean Barrett - http://nothings.org/stb +// stb_tilemap_editor.h - v0.37 - Sean Barrett - http://nothings.org/stb // placed in the public domain - not copyrighted - first released 2014-09 // // Embeddable tilemap editor for C/C++ @@ -259,7 +259,7 @@ // #define STBTE_MAX_CATEGORIES 100 // #define STBTE_UNDO_BUFFER_BYTES (1 << 24) // 16 MB // #define STBTE_MAX_COPY 90000 // e.g. 300x300 -// #define STBTE_MAX_PROPERTIESERTIES 10 // max properties per tile +// #define STBTE_MAX_PROPERTIES 10 // max properties per tile // // API // @@ -275,6 +275,7 @@ // either approach allows cut&pasting between levels.) // // REVISION HISTORY +// 0.37 fix warning // 0.36 minor compiler support // 0.35 layername button changes // - layername buttons grow with the layer panel @@ -311,6 +312,7 @@ // Josh Huelsman // Bugfixes: // Ryan Whitworth +// Eugene Opalev // // LICENSE // diff --git a/tests/stb.dsp b/tests/stb.dsp index 3dcb5c9..20f341b 100644 --- a/tests/stb.dsp +++ b/tests/stb.dsp @@ -66,7 +66,7 @@ LINK32=link.exe # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c -# ADD CPP /nologo /MTd /W3 /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "VORBIS_TEST" /FR /FD /GZ /c +# ADD CPP /nologo /MTd /W3 /GX /Zi /Od /I ".." /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "MAIN_TEST" /FR /FD /GZ /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG"