mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-09-30 20:52:31 +07:00
fail mmap::open when ::mmap returns MAP_FAILED
This commit is contained in:
@@ -5568,6 +5568,15 @@ inline bool mmap::open(const char *path) {
|
|||||||
is_open_empty_file = true;
|
is_open_empty_file = true;
|
||||||
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) {
|
||||||
|
addr_ = nullptr;
|
||||||
|
close();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -9163,6 +9163,23 @@ TEST(MmapTest, OpenWhileFileHeldForWriting) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
// A failed ::mmap must not be reported as an open mapping. is_open() only
|
||||||
|
// compares addr_ against nullptr, so the MAP_FAILED sentinel used to pass it
|
||||||
|
// and data() handed the caller (const char *)-1. A directory opens and stats
|
||||||
|
// fine but has no mapping, so ::mmap fails for it.
|
||||||
|
TEST(MmapTest, FailedMappingIsNotOpen) {
|
||||||
|
const char *path = "./mmap_failed_mapping_test_dir";
|
||||||
|
ASSERT_EQ(0, ::mkdir(path, 0755));
|
||||||
|
auto dir_cleanup = detail::scope_exit([&] { ::rmdir(path); });
|
||||||
|
|
||||||
|
detail::mmap m(path);
|
||||||
|
EXPECT_FALSE(m.is_open());
|
||||||
|
EXPECT_NE(static_cast<const void *>(m.data()),
|
||||||
|
static_cast<const void *>(MAP_FAILED));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(KeepAliveTest, ReadTimeout) {
|
TEST(KeepAliveTest, ReadTimeout) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user