Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/test-win-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
5 changes: 5 additions & 0 deletions tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
87 changes: 87 additions & 0 deletions tests/zlib-allocation-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <limits.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

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;
}
10 changes: 9 additions & 1 deletion zlib/zutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Loading