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..88b6b520b --- /dev/null +++ b/tests/zlib-allocation-test.c @@ -0,0 +1,87 @@ +#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; + 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(size_t_width == 4 && uint_width == 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 (size_t_width > sizeof(unsigned)) { + result = zcalloc(NULL, UINT_MAX, 2); + CHECK(result == &allocation && allocated_size == (size_t)UINT_MAX * 2); + 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); + } + } 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 (uint_width > 2) + CHECK(allocation_calls == 0); + } + + return 0; +} diff --git a/zlib/zutil.c b/zlib/zutil.c index 6c01021df..6a43b038b 100644 --- a/zlib/zutil.c +++ b/zlib/zutil.c @@ -301,8 +301,16 @@ extern void free(voidpf ptr); #endif voidpf ZLIB_INTERNAL zcalloc(voidpf opaque, unsigned items, unsigned size) { +#ifdef STDC + typedef size_t alloc_size_t; +#else + typedef uInt alloc_size_t; +#endif (void)opaque; - return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : + /* 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); }