From a9c133f0cf6823557f2760a9ed9de8ec8e994e1f Mon Sep 17 00:00:00 2001 From: Bhagirath Mehta Date: Thu, 19 Nov 2026 13:18:05 -0600 Subject: [PATCH 1/2] Prevent undersized bundled zlib allocations Reject allocation-size overflow and preserve full-width multiplication so the bundled allocator cannot silently allocate less than requested. Exercise the boundary in native CTest and 32-bit Windows CI. Files: zlib/zutil.c, tests/zlib-allocation-test.c, tests/unittests/CMakeLists.txt, .github/workflows/test-win-latest.yml Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e5009227-6b49-4803-8153-63d8d94037e3 --- .github/workflows/test-win-latest.yml | 14 +++++ tests/unittests/CMakeLists.txt | 5 ++ tests/zlib-allocation-test.c | 84 +++++++++++++++++++++++++++ zlib/zutil.c | 15 ++++- 4 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 tests/zlib-allocation-test.c diff --git a/.github/workflows/test-win-latest.yml b/.github/workflows/test-win-latest.yml index c6f19f8aa..298bb4672 100644 --- a/.github/workflows/test-win-latest.yml +++ b/.github/workflows/test-win-latest.yml @@ -56,6 +56,20 @@ jobs: with: vs-version: '[17,)' + - name: Test bundled zlib allocation bounds (Win32) + if: matrix.arch == 'Win32' && matrix.build == 'Release' && matrix.transport == 'WinHTTP' + shell: cmd + run: | + set "VSPATH=" + for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do set "VSPATH=%%i" + if not defined VSPATH exit /b 1 + set "PATH=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer;%PATH%" + call "%VSPATH%\VC\Auxiliary\Build\vcvars32.bat" >nul + if errorlevel 1 exit /b 1 + cl /nologo /W4 /WX /TC /DZLIB_TEST_REQUIRE_32_BIT tests\zlib-allocation-test.c /Fo"%RUNNER_TEMP%\zlib-allocation-test.obj" /Fe"%RUNNER_TEMP%\zlib-allocation-test.exe" + if errorlevel 1 exit /b 1 + "%RUNNER_TEMP%\zlib-allocation-test.exe" + - name: Test ${{ matrix.transport }} ${{ matrix.arch }} ${{ matrix.build }} shell: cmd run: build-tests.cmd ${{ matrix.arch }} ${{ matrix.build }} "" ${{ matrix.transport }} diff --git a/tests/unittests/CMakeLists.txt b/tests/unittests/CMakeLists.txt index ff682427b..7b81dde24 100644 --- a/tests/unittests/CMakeLists.txt +++ b/tests/unittests/CMakeLists.txt @@ -159,3 +159,8 @@ endif() add_test(NAME UnitTests COMMAND UnitTests "--gtest_output=xml:${PROJECT_BINARY_DIR}/test-reports/UnitTests.xml") set_tests_properties(UnitTests PROPERTIES WORKING_DIRECTORY "${PROJECT_BINARY_DIR}") + +if(NOT MATSDK_PLATFORM_IOS) + add_executable(zlib-allocation-test ../zlib-allocation-test.c) + add_test(NAME zlib-allocation-test COMMAND zlib-allocation-test) +endif() diff --git a/tests/zlib-allocation-test.c b/tests/zlib-allocation-test.c new file mode 100644 index 000000000..f6772427a --- /dev/null +++ b/tests/zlib-allocation-test.c @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +static size_t allocated_size; +static unsigned allocation_calls; +static int allocation_succeeds = 1; +static char allocation; + +static void *test_malloc(size_t size) { + allocated_size = size; + allocation_calls++; + return allocation_succeeds ? &allocation : NULL; +} + +static void *test_calloc(size_t items, size_t size) { + allocation_calls++; + if (size != 0 && items > (size_t)-1 / size) + return NULL; + allocated_size = items * size; + return allocation_succeeds ? &allocation : NULL; +} + +static void test_free(void *ptr) { + (void)ptr; +} + +#define malloc test_malloc +#define calloc test_calloc +#define free test_free +#include "../zlib/zutil.c" +#undef malloc +#undef calloc +#undef free + +#define CHECK(condition) do { \ + if (!(condition)) { \ + fprintf(stderr, "Allocation check failed at line %d\n", __LINE__); \ + return 1; \ + } \ +} while (0) + +int main(void) { + voidpf result; + +#ifdef ZLIB_TEST_REQUIRE_32_BIT + CHECK(sizeof(size_t) == 4 && sizeof(uInt) == 4); +#endif + + allocation_calls = 0; + result = zcalloc(NULL, 3, 7); + CHECK(result == &allocation && allocation_calls == 1 && allocated_size == 21); + + result = zcalloc(NULL, 0, 7); + CHECK(result == &allocation && allocated_size == 0); + result = zcalloc(NULL, 7, 0); + CHECK(result == &allocation && allocated_size == 0); + + allocation_succeeds = 0; + result = zcalloc(NULL, 3, 7); + CHECK(result == NULL); + allocation_succeeds = 1; + + if (sizeof(size_t) > sizeof(unsigned)) { + result = zcalloc(NULL, UINT_MAX, 2); + CHECK(result == &allocation && allocated_size == (size_t)UINT_MAX * 2); + if (UINT_MAX <= (size_t)-1 / UINT_MAX) { + result = zcalloc(NULL, UINT_MAX, UINT_MAX); + CHECK(result == &allocation && + allocated_size == (size_t)UINT_MAX * UINT_MAX); + } + } else { + result = zcalloc(NULL, UINT_MAX, 1); + CHECK(result == &allocation && allocated_size == (size_t)UINT_MAX); + allocation_calls = 0; + result = zcalloc(NULL, UINT_MAX, 2); + CHECK(result == NULL); + if (sizeof(uInt) > 2) + CHECK(allocation_calls == 0); + } + + return 0; +} diff --git a/zlib/zutil.c b/zlib/zutil.c index 6c01021df..78513e27f 100644 --- a/zlib/zutil.c +++ b/zlib/zutil.c @@ -302,8 +302,19 @@ extern void free(voidpf ptr); voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) { (void)opaque; - return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : - (voidpf)calloc(items, size); + if (sizeof(uInt) > 2) { + /* Modified for the SDK: reject sizes that malloc cannot represent. */ +#ifdef STDC + if (size != 0 && items > (size_t)-1 / size) + return Z_NULL; + return (voidpf)malloc((size_t)items * size); +#else + if (size != 0 && items > (uInt)-1 / size) + return Z_NULL; + return (voidpf)malloc(items * size); +#endif + } + return (voidpf)calloc(items, size); } void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) { From 84141b42773199887cbc686961e456b227facee9 Mon Sep 17 00:00:00 2001 From: Bhagirath Mehta Date: Thu, 19 Nov 2026 13:51:08 -0600 Subject: [PATCH 2/2] Keep allocation regression warning-clean on older MSVC Express the same allocation checks without constant if conditions so the Windows 2022 compiler can enforce warnings as errors. Files: zlib/zutil.c, tests/zlib-allocation-test.c Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e5009227-6b49-4803-8153-63d8d94037e3 --- tests/zlib-allocation-test.c | 11 +++++++---- zlib/zutil.c | 19 ++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/tests/zlib-allocation-test.c b/tests/zlib-allocation-test.c index f6772427a..88b6b520b 100644 --- a/tests/zlib-allocation-test.c +++ b/tests/zlib-allocation-test.c @@ -43,9 +43,12 @@ static void test_free(void *ptr) { int main(void) { voidpf result; + size_t size_t_width = sizeof(size_t); + size_t uint_width = sizeof(uInt); + size_t max_size = (size_t)-1; #ifdef ZLIB_TEST_REQUIRE_32_BIT - CHECK(sizeof(size_t) == 4 && sizeof(uInt) == 4); + CHECK(size_t_width == 4 && uint_width == 4); #endif allocation_calls = 0; @@ -62,10 +65,10 @@ int main(void) { CHECK(result == NULL); allocation_succeeds = 1; - if (sizeof(size_t) > sizeof(unsigned)) { + if (size_t_width > sizeof(unsigned)) { result = zcalloc(NULL, UINT_MAX, 2); CHECK(result == &allocation && allocated_size == (size_t)UINT_MAX * 2); - if (UINT_MAX <= (size_t)-1 / UINT_MAX) { + if (UINT_MAX <= max_size / UINT_MAX) { result = zcalloc(NULL, UINT_MAX, UINT_MAX); CHECK(result == &allocation && allocated_size == (size_t)UINT_MAX * UINT_MAX); @@ -76,7 +79,7 @@ int main(void) { allocation_calls = 0; result = zcalloc(NULL, UINT_MAX, 2); CHECK(result == NULL); - if (sizeof(uInt) > 2) + if (uint_width > 2) CHECK(allocation_calls == 0); } diff --git a/zlib/zutil.c b/zlib/zutil.c index 78513e27f..6a43b038b 100644 --- a/zlib/zutil.c +++ b/zlib/zutil.c @@ -301,20 +301,17 @@ extern void free(voidpf ptr); #endif voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) { - (void)opaque; - if (sizeof(uInt) > 2) { - /* Modified for the SDK: reject sizes that malloc cannot represent. */ #ifdef STDC - if (size != 0 && items > (size_t)-1 / size) - return Z_NULL; - return (voidpf)malloc((size_t)items * size); + typedef size_t alloc_size_t; #else - if (size != 0 && items > (uInt)-1 / size) - return Z_NULL; - return (voidpf)malloc(items * size); + typedef uInt alloc_size_t; #endif - } - return (voidpf)calloc(items, size); + (void)opaque; + /* Modified for the SDK: reject sizes that malloc cannot represent. */ + if (size != 0 && sizeof(uInt) > 2 && items > (alloc_size_t)-1 / size) + return Z_NULL; + return sizeof(uInt) > 2 ? (voidpf)malloc((alloc_size_t)items * size) : + (voidpf)calloc(items, size); } void ZLIB_INTERNAL zcfree(voidpf opaque, voidpf ptr) {