Fix the overrun issue reported by static application security testing #177
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The current issue is reported by the SAST(Static Application Security Testing) as below:
Error: OVERRUN:
snappy_unittest.cc:95: return_constant: Function call "sysconf(_SC_PAGESIZE)" may return -1.
snappy_unittest.cc:95: assignment: Assigning: "page_size" = "sysconf(SC_PAGESIZE)". The value of "page_size" is now 18446744073709551615.
snappy_unittest.cc:97: overrun-buffer-arg: Calling "mprotect" with "this->protected_page" and "page_size" is suspicious because of the very large index, 18446744073709551615. The index may be due to a negative parameter being interpreted as unsigned.
95| const size_t page_size = sysconf(_SC_PAGESIZE);
96| // Undo the mprotect.
97|-> CHECK_EQ(0, mprotect(protected_page_, page_size, PROT_READ|PROT_WRITE));
98| CHECK_EQ(0, munmap(mem_, alloc_size_));
99| }
Let's set the page size to 4096, if the invoking sysconf(_SC_PAGESIZE) failed, otherwise still use the actual value of sysconf(_SC_PAGESIZE). In addition, also save its value in the constructor function in order to use it again in the deconstructor function, that can avoid calling the sysconf(_SC_PAGESIZE) twice.
(Did the same changes in snappy_test_tool.cc).