fd: mmap: Don't use null span
We aren't allowed to use a null span with positive size, change the
internal interfaces to avoid it.
Change-Id: I6687d45a535c20b72f42a99e1ecdbd89f3796f99
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include-fd/stdplus/fd/gmock.hpp b/include-fd/stdplus/fd/gmock.hpp
index 08c66fb..4ec3de0 100644
--- a/include-fd/stdplus/fd/gmock.hpp
+++ b/include-fd/stdplus/fd/gmock.hpp
@@ -48,8 +48,8 @@
MOCK_METHOD(void, fcntlSetfl, (FileFlags flags), (override));
MOCK_METHOD(FileFlags, fcntlGetfl, (), (const, override));
MOCK_METHOD(std::span<std::byte>, mmap,
- (std::span<std::byte> window, ProtFlags prot, MMapFlags flags,
- off_t offset),
+ (std::byte * window, std::size_t size, ProtFlags prot,
+ MMapFlags flags, off_t offset),
(override));
MOCK_METHOD(void, munmap, (std::span<std::byte> window), (override));
};
diff --git a/include-fd/stdplus/fd/impl.hpp b/include-fd/stdplus/fd/impl.hpp
index fe3af81..e3991dc 100644
--- a/include-fd/stdplus/fd/impl.hpp
+++ b/include-fd/stdplus/fd/impl.hpp
@@ -40,8 +40,9 @@
FileFlags fcntlGetfl() const override;
protected:
- std::span<std::byte> mmap(std::span<std::byte> window, ProtFlags prot,
- MMapFlags flags, off_t offset) override;
+ std::span<std::byte> mmap(std::byte* window, std::size_t size,
+ ProtFlags prot, MMapFlags flags,
+ off_t offset) override;
void munmap(std::span<std::byte> window) override;
};
diff --git a/include-fd/stdplus/fd/intf.hpp b/include-fd/stdplus/fd/intf.hpp
index 70ff828..b15cda6 100644
--- a/include-fd/stdplus/fd/intf.hpp
+++ b/include-fd/stdplus/fd/intf.hpp
@@ -149,8 +149,8 @@
protected:
virtual std::span<std::byte>
- mmap(std::span<std::byte> window, ProtFlags prot, MMapFlags flags,
- off_t offset) = 0;
+ mmap(std::byte* window, std::size_t size, ProtFlags prot,
+ MMapFlags flags, off_t offset) = 0;
virtual void munmap(std::span<std::byte> window) = 0;
friend class MMap;
};
diff --git a/include-fd/stdplus/fd/mmap.hpp b/include-fd/stdplus/fd/mmap.hpp
index f41f562..0bc1f81 100644
--- a/include-fd/stdplus/fd/mmap.hpp
+++ b/include-fd/stdplus/fd/mmap.hpp
@@ -14,13 +14,8 @@
class MMap
{
public:
- inline MMap(Fd& fd, size_t window_size, ProtFlags prot, MMapFlags flags,
- off_t offset) :
- MMap(fd,
- std::span<std::byte>{static_cast<std::byte*>(nullptr),
- window_size},
- prot, flags, offset)
- {}
+ MMap(Fd& fd, size_t window_size, ProtFlags prot, MMapFlags flags,
+ off_t offset);
MMap(Fd& fd, std::span<std::byte> window, ProtFlags prot, MMapFlags flags,
off_t offset);
diff --git a/src/fd/impl.cpp b/src/fd/impl.cpp
index 3147e64..5f9fe7f 100644
--- a/src/fd/impl.cpp
+++ b/src/fd/impl.cpp
@@ -188,16 +188,16 @@
return FileFlags(CHECK_ERRNO(::fcntl(get(), F_GETFL), "fcntl getfl"));
}
-std::span<std::byte> FdImpl::mmap(std::span<std::byte> window, ProtFlags prot,
- MMapFlags flags, off_t offset)
+std::span<std::byte> FdImpl::mmap(std::byte* window, std::size_t size,
+ ProtFlags prot, MMapFlags flags, off_t offset)
{
- auto ret = ::mmap(window.data(), window.size(), static_cast<int>(prot),
+ auto ret = ::mmap(window, size, static_cast<int>(prot),
static_cast<int>(flags), get(), offset);
if (ret == MAP_FAILED)
{
util::doError(errno, "mmap");
}
- return {reinterpret_cast<std::byte*>(ret), window.size()};
+ return {reinterpret_cast<std::byte*>(ret), size};
}
void FdImpl::munmap(std::span<std::byte> window)
diff --git a/src/fd/mmap.cpp b/src/fd/mmap.cpp
index 7026aa6..4718121 100644
--- a/src/fd/mmap.cpp
+++ b/src/fd/mmap.cpp
@@ -6,8 +6,13 @@
namespace fd
{
+MMap::MMap(Fd& fd, size_t window_size, ProtFlags prot, MMapFlags flags,
+ off_t offset) :
+ mapping(fd.mmap(nullptr, window_size, prot, flags, offset), fd)
+{}
MMap::MMap(Fd& fd, std::span<std::byte> window, ProtFlags prot, MMapFlags flags,
- off_t offset) : mapping(fd.mmap(window, prot, flags, offset), fd)
+ off_t offset) :
+ mapping(fd.mmap(window.data(), window.size(), prot, flags, offset), fd)
{}
std::span<std::byte> MMap::get() const
diff --git a/test/pinned.cpp b/test/pinned.cpp
index 88c9bef..fc907fb 100644
--- a/test/pinned.cpp
+++ b/test/pinned.cpp
@@ -81,6 +81,7 @@
TEST(PinnedRef, Fundamental)
{
Pinned<int> pi = 4;
+ // NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult)
EXPECT_EQ(4, PinnedRef<int>(pi));
EXPECT_EQ(4, PinnedRef<const int>(pi));
EXPECT_EQ(4, PinnedRef(pi));