Skip to content

Commit 8c4daf9

Browse files
tniessenjuanarbol
authored andcommitted
test: check that sysconf returns a positive value
Static analysis insists that sysconf(_SC_PAGE_SIZE) might return a negative integer (even though it never will). This was supposed to be handled by the existing check EXPECT_GE(page, static_cast<int>(N)). I assume that static analysis does not consider this sufficient because static_cast<int>(N) could be negative or zero if N exceeds INT_MAX (even though it never will). To resolve this (theoretical) problem, explicitly check that the return value is positive and then cast it to a size_t. PR-URL: #44666 Reviewed-By: Darshan Sen <[email protected]>
1 parent 6deca66 commit 8c4daf9

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

test/cctest/test_crypto_clienthello.cc

+22-11
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,38 @@
2929
#endif
3030
#endif
3131

32+
#if defined(USE_MPROTECT)
33+
size_t GetPageSize() {
34+
int page_size = sysconf(_SC_PAGE_SIZE);
35+
EXPECT_GE(page_size, 1);
36+
return page_size;
37+
}
38+
#elif defined(USE_VIRTUALPROTECT)
39+
size_t GetPageSize() {
40+
SYSTEM_INFO system_info;
41+
GetSystemInfo(&system_info);
42+
return system_info.dwPageSize;
43+
}
44+
#endif
45+
3246
template <size_t N>
3347
class OverrunGuardedBuffer {
3448
public:
3549
OverrunGuardedBuffer() {
50+
#if defined(USE_MPROTECT) || defined(USE_VIRTUALPROTECT)
51+
size_t page = GetPageSize();
52+
EXPECT_GE(page, N);
53+
#endif
3654
#ifdef USE_MPROTECT
3755
// Place the packet right before a guard page, which, when accessed, causes
3856
// a segmentation fault.
39-
int page = sysconf(_SC_PAGE_SIZE);
40-
EXPECT_GE(page, static_cast<int>(N));
4157
alloc_base = static_cast<uint8_t*>(aligned_alloc(page, 2 * page));
4258
EXPECT_NE(alloc_base, nullptr);
4359
uint8_t* second_page = alloc_base + page;
4460
EXPECT_EQ(mprotect(second_page, page, PROT_NONE), 0);
4561
data_base = second_page - N;
4662
#elif defined(USE_VIRTUALPROTECT)
4763
// On Windows, it works almost the same way.
48-
SYSTEM_INFO system_info;
49-
GetSystemInfo(&system_info);
50-
DWORD page = system_info.dwPageSize;
5164
alloc_base = static_cast<uint8_t*>(
5265
VirtualAlloc(nullptr, 2 * page, MEM_COMMIT, PAGE_READWRITE));
5366
EXPECT_NE(alloc_base, nullptr);
@@ -70,16 +83,14 @@ class OverrunGuardedBuffer {
7083
OverrunGuardedBuffer& operator=(const OverrunGuardedBuffer& other) = delete;
7184

7285
~OverrunGuardedBuffer() {
86+
#if defined(USE_MPROTECT) || defined(USE_VIRTUALPROTECT)
87+
size_t page = GetPageSize();
88+
#endif
7389
#ifdef USE_VIRTUALPROTECT
74-
SYSTEM_INFO system_info;
75-
GetSystemInfo(&system_info);
76-
DWORD page = system_info.dwPageSize;
77-
VirtualFree(alloc_base, 2 * system_info.dwPageSize, MEM_RELEASE);
90+
VirtualFree(alloc_base, 2 * page, MEM_RELEASE);
7891
#else
7992
#ifdef USE_MPROTECT
8093
// Revert page protection such that the memory can be free()'d.
81-
int page = sysconf(_SC_PAGE_SIZE);
82-
EXPECT_GE(page, static_cast<int>(N));
8394
uint8_t* second_page = alloc_base + page;
8495
EXPECT_EQ(mprotect(second_page, page, PROT_READ | PROT_WRITE), 0);
8596
#endif

0 commit comments

Comments
 (0)