This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/intern/guardedalloc/tests/guardedalloc_overflow_test.cc
Sybren A. Stüvel feb71f1d71 Animation: Expand unit tests for BKE_fcurve_active_keyframe_index()
Expand unit test for `BKE_fcurve_active_keyframe_index()` to test edge
cases better.

This also introduces a new test macro `EXPECT_BLI_ASSERT()`, which can be
used to test that an assertion fails successfully.

No functional changes to actual Blender code.
2020-11-10 15:36:21 +01:00

61 lines
1.5 KiB
C++

/* Apache License, Version 2.0 */
#include "testing/testing.h"
#include "MEM_guardedalloc.h"
/* We expect to abort on integer overflow, to prevent possible exploits. */
#if defined(__GNUC__) && !defined(__clang__)
/* Disable since it's the purpose of this test. */
# pragma GCC diagnostic ignored "-Walloc-size-larger-than="
#endif
namespace {
void MallocArray(size_t len, size_t size)
{
void *mem = MEM_malloc_arrayN(len, size, "MallocArray");
if (mem) {
MEM_freeN(mem);
}
}
void CallocArray(size_t len, size_t size)
{
void *mem = MEM_calloc_arrayN(len, size, "CallocArray");
if (mem) {
MEM_freeN(mem);
}
}
} // namespace
TEST(guardedalloc, LockfreeIntegerOverflow)
{
MallocArray(1, SIZE_MAX);
CallocArray(SIZE_MAX, 1);
MallocArray(SIZE_MAX / 2, 2);
CallocArray(SIZE_MAX / 1234567, 1234567);
EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
}
TEST(guardedalloc, GuardedIntegerOverflow)
{
MEM_use_guarded_allocator();
MallocArray(1, SIZE_MAX);
CallocArray(SIZE_MAX, 1);
MallocArray(SIZE_MAX / 2, 2);
CallocArray(SIZE_MAX / 1234567, 1234567);
EXPECT_EXIT(MallocArray(SIZE_MAX, 2), ABORT_PREDICATE, "");
EXPECT_EXIT(CallocArray(7, SIZE_MAX), ABORT_PREDICATE, "");
EXPECT_EXIT(MallocArray(SIZE_MAX, 12345567), ABORT_PREDICATE, "");
EXPECT_EXIT(CallocArray(SIZE_MAX, SIZE_MAX), ABORT_PREDICATE, "");
}