Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7ed8753

Browse filesBrowse files
tniessenRafaelGSS
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 <raisinten@gmail.com>
1 parent 2a37f74 commit 7ed8753
Copy full SHA for 7ed8753

File tree

Expand file treeCollapse file tree

1 file changed

+22
-11
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+22
-11
lines changed
Open diff view settings
Collapse file

‎test/cctest/test_crypto_clienthello.cc‎

Copy file name to clipboardExpand all lines: test/cctest/test_crypto_clienthello.cc
+22-11Lines changed: 22 additions & 11 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.