From 80b89cf6c8a6581245d65bc8d3fe67412c0f1bf7 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 12 Oct 2018 11:37:06 +0100 Subject: [PATCH 01/12] Fixes for stb_leakcheck.h I ran into a couple of problems while trying to use this with my program: My compiler (MinGW-w64 GCC 8.2.0) complained about not recognising %lld format specifiers. The compiler also warned about non-guarding 'if's. I ignored it at first, but then I noticed my program crashed when it ran stb_leakcheck_dumpmem. Making the 'if's guard fixed that. After that, the lib worked until I changed how it was included: instead of tacking a debug-only #include at the start of every file, I switched to using GCC's '-include' option to force-include it in every file. But doing that gave me errors about size_t not being defined. And after fixing that, I instead got errors about the #defines messing with stdlib.h. So to fix those I made the declaration-only part of the header include stdlib.h. --- stb_leakcheck.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index b8c8df1..32cf93c 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -96,7 +96,7 @@ static void stblkck_internal_print(const char *reason, const char *file, int li // and the older ones don't even have %lld either... however, the old compilers // without "long long" don't support 64-bit targets either, so here's the // compromise: - #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005 + #if (defined(_MSC_VER) && _MSC_VER < 1400) /* before VS 2005 */ || defined(__MINGW32__) printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr); #else printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr); @@ -112,16 +112,20 @@ void stb_leakcheck_dumpmem(void) stb_leakcheck_malloc_info *mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size >= 0) + { stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1); printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, mi+1); + } mi = mi->next; } #ifdef STB_LEAKCHECK_SHOWALL mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size < 0) + { stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1); printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, mi+1); + } mi = mi->next; } #endif @@ -131,6 +135,8 @@ void stb_leakcheck_dumpmem(void) #ifndef INCLUDE_STB_LEAKCHECK_H #define INCLUDE_STB_LEAKCHECK_H +#include // we want to define the macros *after* stdlib to avoid a slew of errors + #define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__) #define free(p) stb_leakcheck_free(p) #define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__) From 248604ffbcd050259024e145428d8345da1769f3 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 18 Oct 2018 13:29:55 +0100 Subject: [PATCH 02/12] Shut up GCC -pedantic warnings --- stb_leakcheck.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 32cf93c..d68e109 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -114,7 +114,7 @@ void stb_leakcheck_dumpmem(void) if ((ptrdiff_t) mi->size >= 0) { stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1); - printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, mi+1); + printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, (void*)(mi+1)); } mi = mi->next; } @@ -124,7 +124,7 @@ void stb_leakcheck_dumpmem(void) if ((ptrdiff_t) mi->size < 0) { stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1); - printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, mi+1); + printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, (void*)(mi+1)); } mi = mi->next; } From f7d9426f8e3f56a2384a4ced1c2f051cb7699383 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 1 Nov 2018 23:25:47 +0000 Subject: [PATCH 03/12] Add a way to use %zd on MinGW(-w64) This is suggested here: https://sourceforge.net/p/mingw-w64/wiki2/gnu%20printf/ Compared to using __USE_MINGW_ANSI_STDIO, this prevents those annoying warning about "unsupported" format specifiers. --- stb_leakcheck.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index d68e109..ad0938b 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -91,19 +91,23 @@ void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line) static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr) { -#if (defined(_MSC_VER) && _MSC_VER < 1900) /* 1900=VS 2015 */ || defined(__MINGW32__) +#if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015 // Compilers that use the old MS C runtime library don't have %zd // and the older ones don't even have %lld either... however, the old compilers // without "long long" don't support 64-bit targets either, so here's the // compromise: - #if (defined(_MSC_VER) && _MSC_VER < 1400) /* before VS 2005 */ || defined(__MINGW32__) + #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005 printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr); #else printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr); #endif #else // Assume we have %zd on other targets. - printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + #ifdef __MINGW32__ + __mingw_printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + #else + printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + #endif #endif } From 79dc50bb79bfa0caeeef62dfb8b2c4fe09926d29 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 1 Nov 2018 23:49:17 +0000 Subject: [PATCH 04/12] ...Actually, I don't think these are meant to be here Looking at 501812f307cd4b7609a1c6226f26bb7780cf08b8, the entire point was to *replace* these lines. That explains why they lacked braces earlier. --- stb_leakcheck.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index ad0938b..9580bfa 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -116,20 +116,14 @@ void stb_leakcheck_dumpmem(void) stb_leakcheck_malloc_info *mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size >= 0) - { stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1); - printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, (void*)(mi+1)); - } mi = mi->next; } #ifdef STB_LEAKCHECK_SHOWALL mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size < 0) - { stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1); - printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, (void*)(mi+1)); - } mi = mi->next; } #endif From 359bb10d3cfd72f9bb2b308b65b022b4a0a6c3a5 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Thu, 1 Nov 2018 23:55:07 +0000 Subject: [PATCH 05/12] Try to clean up stblkck_internal_print a little No need to pass all those parameters. Also long longs aren't 8 digits long. I don't know how to find out the length of a size_t at compile-time in a way I can use with a format specifier. --- stb_leakcheck.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 9580bfa..9fefceb 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -89,7 +89,7 @@ void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line) } } -static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr) +static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info *mi) { #if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015 // Compilers that use the old MS C runtime library don't have %zd @@ -97,16 +97,16 @@ static void stblkck_internal_print(const char *reason, const char *file, int li // without "long long" don't support 64-bit targets either, so here's the // compromise: #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005 - printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr); + printf("%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1)); #else - printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr); + printf("%s: %s (%4d): %16lld bytes at %p\n", reason, mi->file, mi->line, (long long)mi->size, (void*)(mi+1)); #endif #else // Assume we have %zd on other targets. #ifdef __MINGW32__ - __mingw_printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + __mingw_printf("%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1)); #else - printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); + printf("%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1)); #endif #endif } @@ -116,14 +116,14 @@ void stb_leakcheck_dumpmem(void) stb_leakcheck_malloc_info *mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size >= 0) - stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1); + stblkck_internal_print("LEAKED", mi); mi = mi->next; } #ifdef STB_LEAKCHECK_SHOWALL mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size < 0) - stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1); + stblkck_internal_print("FREED ", mi); mi = mi->next; } #endif From 3e6370720e4811d5fc10fb09f1f0efd67ec04a69 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 2 Nov 2018 00:13:40 +0000 Subject: [PATCH 06/12] Fixed STB_LEAKCHECK_SHOWALL 5a5cf7f9ba936bb931ecde8df9465e250132fb75 derped --- stb_leakcheck.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 9fefceb..705c6fc 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -58,8 +58,8 @@ void stb_leakcheck_free(void *ptr) mi->prev->next = mi->next; if (mi->next) mi->next->prev = mi->prev; - #endif free(mi); + #endif } } From 610a976b83d38fb17d57bc895d57b91a1c2bb422 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Fri, 2 Nov 2018 00:20:31 +0000 Subject: [PATCH 07/12] Removed redundant check --- stb_leakcheck.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 705c6fc..45894d4 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -96,7 +96,7 @@ static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info // and the older ones don't even have %lld either... however, the old compilers // without "long long" don't support 64-bit targets either, so here's the // compromise: - #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005 + #if _MSC_VER < 1400 // before VS 2005 printf("%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1)); #else printf("%s: %s (%4d): %16lld bytes at %p\n", reason, mi->file, mi->line, (long long)mi->size, (void*)(mi+1)); From d156642036d80349426b9e189a09213aa72da864 Mon Sep 17 00:00:00 2001 From: NuklearBomb <33639078+NuklearBomb@users.noreply.github.com> Date: Tue, 29 Jan 2019 01:43:31 -0500 Subject: [PATCH 08/12] Silence warning 'tc' may be used uninitialized in this function --- stb_image.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_image.h b/stb_image.h index d9c21bc..b687b06 100644 --- a/stb_image.h +++ b/stb_image.h @@ -4731,7 +4731,7 @@ static void stbi__de_iphone(stbi__png *z) static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp) { stbi_uc palette[1024], pal_img_n=0; - stbi_uc has_trans=0, tc[3]; + stbi_uc has_trans=0, tc[3]={0}; stbi__uint16 tc16[3]; stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0; int first=1,k,interlace=0, color=0, is_iphone=0; From 604b9367ee7a77aeff916a6bb5a9ee9601d72c02 Mon Sep 17 00:00:00 2001 From: Kevin Croft Date: Sat, 17 Nov 2018 21:42:30 -0800 Subject: [PATCH 09/12] Add detection for Ogg skeleton metadata --- stb_vorbis.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/stb_vorbis.c b/stb_vorbis.c index b1a8b22..da3156a 100644 --- a/stb_vorbis.c +++ b/stb_vorbis.c @@ -374,7 +374,8 @@ enum STBVorbisError VORBIS_invalid_first_page, VORBIS_bad_packet_type, VORBIS_cant_find_last_page, - VORBIS_seek_failed + VORBIS_seek_failed, + VORBIS_ogg_skeleton_not_supported }; @@ -3579,6 +3580,18 @@ static int start_decoder(vorb *f) // check for expected packet length if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page); if (f->segments[0] != 30) return error(f, VORBIS_invalid_first_page); + // check for the Ogg skeleton fishead identifying header to refine our error + if (f->segments[0] == 64 && + getn(f, header, 6) && + header[0] == 'f' && + header[1] == 'i' && + header[2] == 's' && + header[3] == 'h' && + header[4] == 'e' && + header[5] == 'a' && + get8(f) == 'd') return error(f, VORBIS_ogg_skeleton_not_supported); + else + return error(f, VORBIS_invalid_first_page); // read packet // check packet header if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page); From d1dc3fe89c35508db224ed4155ecd56a78d2e2ab Mon Sep 17 00:00:00 2001 From: Kevin Croft Date: Sun, 18 Nov 2018 19:07:55 -0800 Subject: [PATCH 10/12] Fix return typo, disambiguate else, and check for the complete fishead identifier --- stb_vorbis.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stb_vorbis.c b/stb_vorbis.c index da3156a..263cfc7 100644 --- a/stb_vorbis.c +++ b/stb_vorbis.c @@ -3579,7 +3579,7 @@ static int start_decoder(vorb *f) if (f->page_flag & PAGEFLAG_continued_packet) return error(f, VORBIS_invalid_first_page); // check for expected packet length if (f->segment_count != 1) return error(f, VORBIS_invalid_first_page); - if (f->segments[0] != 30) return error(f, VORBIS_invalid_first_page); + if (f->segments[0] != 30) { // check for the Ogg skeleton fishead identifying header to refine our error if (f->segments[0] == 64 && getn(f, header, 6) && @@ -3589,9 +3589,12 @@ static int start_decoder(vorb *f) header[3] == 'h' && header[4] == 'e' && header[5] == 'a' && - get8(f) == 'd') return error(f, VORBIS_ogg_skeleton_not_supported); + get8(f) == 'd' && + get8(f) == '\0') return error(f, VORBIS_ogg_skeleton_not_supported); else return error(f, VORBIS_invalid_first_page); + } + // read packet // check packet header if (get8(f) != VORBIS_packet_id) return error(f, VORBIS_invalid_first_page); From 82310cc5ff4317bf2bbc4eaaa30ebbe213d32f2f Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Thu, 7 Feb 2019 08:44:39 -0800 Subject: [PATCH 11/12] stb_sprintf: fix unaligned digitpair[], fix some signed-right-shifts --- stb_sprintf.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/stb_sprintf.h b/stb_sprintf.h index 841dfd6..952f7b5 100644 --- a/stb_sprintf.h +++ b/stb_sprintf.h @@ -16,6 +16,7 @@ // Marcin Wojdyr // Leonard Ritter // Stefano Zanotti +// Adam Allison // // LICENSE: // @@ -226,11 +227,18 @@ static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, static char stbsp__period = '.'; static char stbsp__comma = ','; -static char stbsp__digitpair[201] = +static struct +{ + short temp; // force next field to be 2-byte aligned + char pair[201]; +} stbsp__digitpair = +{ + 0, "00010203040506070809101112131415161718192021222324" "25262728293031323334353637383940414243444546474849" "50515253545556575859606162636465666768697071727374" - "75767778798081828384858687888990919293949596979899"; + "75767778798081828384858687888990919293949596979899" +}; STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod) { @@ -687,7 +695,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, if (dp > 0) { pr = (dp < (stbsp__int32)l) ? l - dp : 0; } else { - pr = -dp + ((pr > (stbsp__int32)l) ? l : pr); + pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32) l : pr); } goto dofloatfromg; @@ -1047,7 +1055,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, if ((fl & STBSP__TRIPLET_COMMA) == 0) { do { s -= 2; - *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair[(n % 100) * 2]; + *(stbsp__uint16 *)s = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2]; n /= 100; } while (n); } @@ -1445,7 +1453,7 @@ static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, *bits = b & ((((stbsp__uint64)1) << 52) - 1); *expo = (stbsp__int32)(((b >> 52) & 2047) - 1023); - return (stbsp__int32)(b >> 63); + return (stbsp__int32)((stbsp__uint64) b >> 63); } static double const stbsp__bot[23] = { @@ -1655,7 +1663,7 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c d = value; STBSP__COPYFP(bits, d); expo = (stbsp__int32)((bits >> 52) & 2047); - ng = (stbsp__int32)(bits >> 63); + ng = (stbsp__int32)((stbsp__int64) bits >> 63); if (ng) d = -d; @@ -1765,7 +1773,7 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c } while (n) { out -= 2; - *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair[(n % 100) * 2]; + *(stbsp__uint16 *)out = *(stbsp__uint16 *)&stbsp__digitpair.pair[(n % 100) * 2]; n /= 100; e += 2; } From 9643f6bd98be22e11cfcb82866edc60827b6c17d Mon Sep 17 00:00:00 2001 From: Sean Barrett Date: Thu, 7 Feb 2019 08:45:19 -0800 Subject: [PATCH 12/12] stb_sprintf: fix unaligned digitpair[], fix some signed-right-shifts --- stb_sprintf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stb_sprintf.h b/stb_sprintf.h index 952f7b5..d5e5d87 100644 --- a/stb_sprintf.h +++ b/stb_sprintf.h @@ -1663,7 +1663,7 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c d = value; STBSP__COPYFP(bits, d); expo = (stbsp__int32)((bits >> 52) & 2047); - ng = (stbsp__int32)((stbsp__int64) bits >> 63); + ng = (stbsp__int32)((stbsp__uint64) bits >> 63); if (ng) d = -d;