From 449b210d4e27f689ef49b83463d73c614de82f57 Mon Sep 17 00:00:00 2001 From: "andrey.bychkov" Date: Wed, 5 Aug 2026 22:20:26 +0300 Subject: [PATCH] add test for pisharedmemory --- tests/io/testpisharedmemory.cpp | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/io/testpisharedmemory.cpp diff --git a/tests/io/testpisharedmemory.cpp b/tests/io/testpisharedmemory.cpp new file mode 100644 index 00000000..b898ff7f --- /dev/null +++ b/tests/io/testpisharedmemory.cpp @@ -0,0 +1,35 @@ +#include "pisharedmemory.h" + +#include "gtest/gtest.h" + +#ifdef WINDOWS +# include +#else +# include +#endif + +TEST(PISharedMemory_Test, ReadWriteBounds) { + PISharedMemory shm("pip_bughunt_shm_bounds_" + PIString::fromNumber(int(getpid())), 64); + ASSERT_TRUE(shm.open()); + + const char payload[32] = "0123456789abcdef"; + ASSERT_EQ(32, shm.write(payload, 32)); + ASSERT_EQ(16, shm.write(payload, 16, 10)); + + // Out-of-bounds writes must be rejected, not silently overflow the mapping. + EXPECT_EQ(-1, shm.write(payload, 100)); // exceeds size + EXPECT_EQ(-1, shm.write(payload, 32, 97)); // offset + size exceeds size + EXPECT_EQ(-1, shm.write(payload, 16, -5)); // negative offset + + // Out-of-bounds reads must be rejected. + char dst[128]; + EXPECT_EQ(-1, shm.read(dst, 200)); // exceeds size + EXPECT_EQ(-1, shm.read(dst, 32, 97)); // offset + size exceeds size + EXPECT_EQ(-1, shm.read(dst, 32, -1)); + + // In-bounds reads must still succeed. + EXPECT_EQ(16, shm.read(dst, 16, 10)); + ASSERT_EQ(0, memcmp(dst, "0123456789abcdef", 16)); + + ASSERT_TRUE(shm.close()); +} \ No newline at end of file