mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-26 12:24:46 +00:00
Clarify the MAP_FAILED guard comment and assert the cleared size
Explain why `addr_` is reset before `close()`: `munmap()` must not be called with the sentinel. The test also checks `size()`, since the hazard is a stale size paired with a sentinel `data()`.
This commit is contained in:
1 parent
2e37c51921
commit
15a23abd7e
2 files changed
+6
-7
No files matched your search
@@ -5577,10 +5577,9 @@ inline bool mmap::open(const char *path) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A failed mapping must not be left in `addr_`: `is_open()` only compares it
|
|
||||||
// against nullptr, so the MAP_FAILED sentinel would pass and `data()` would
|
|
||||||
// hand the caller (const char *)-1.
|
|
||||||
if (addr_ == MAP_FAILED) {
|
if (addr_ == MAP_FAILED) {
|
||||||
|
// Clear the sentinel before `close()`, since `is_open()` only checks
|
||||||
|
// `addr_` against nullptr and `munmap()` must not be called with it.
|
||||||
addr_ = nullptr;
|
addr_ = nullptr;
|
||||||
close();
|
close();
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
+4
-4
@@ -9347,10 +9347,9 @@ TEST(MmapTest, OpenWhileFileHeldForWriting) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
// A failed ::mmap must not be reported as an open mapping. is_open() only
|
// A failed ::mmap() must not be reported as an open mapping, otherwise data()
|
||||||
// compares addr_ against nullptr, so the MAP_FAILED sentinel used to pass it
|
// hands the caller the MAP_FAILED sentinel. A directory is the easiest way to
|
||||||
// and data() handed the caller (const char *)-1. A directory opens and stats
|
// reach it, since ::open() and fstat() succeed for one but ::mmap() doesn't.
|
||||||
// fine but has no mapping, so ::mmap fails for it.
|
|
||||||
TEST(MmapTest, FailedMappingIsNotOpen) {
|
TEST(MmapTest, FailedMappingIsNotOpen) {
|
||||||
const char *path = "./mmap_failed_mapping_test_dir";
|
const char *path = "./mmap_failed_mapping_test_dir";
|
||||||
ASSERT_EQ(0, ::mkdir(path, 0755));
|
ASSERT_EQ(0, ::mkdir(path, 0755));
|
||||||
@@ -9358,6 +9357,7 @@ TEST(MmapTest, FailedMappingIsNotOpen) {
|
|||||||
|
|
||||||
detail::mmap m(path);
|
detail::mmap m(path);
|
||||||
EXPECT_FALSE(m.is_open());
|
EXPECT_FALSE(m.is_open());
|
||||||
|
EXPECT_EQ(0U, m.size());
|
||||||
EXPECT_NE(static_cast<const void *>(m.data()),
|
EXPECT_NE(static_cast<const void *>(m.data()),
|
||||||
static_cast<const void *>(MAP_FAILED));
|
static_cast<const void *>(MAP_FAILED));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user