From ce0773b3e6d5abaa8d104100c5704321113853ca Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sat, 6 Dec 2025 16:06:51 +0800 Subject: [PATCH] refactor: improve name handling in shm_posix.cpp to match semaphore pattern Check if name already starts with '/' before adding prefix, consistent with the pattern used in semaphore_impl.h. This avoids duplicate prefix when users provide names in the correct format. --- src/libipc/platform/posix/shm_posix.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/libipc/platform/posix/shm_posix.cpp b/src/libipc/platform/posix/shm_posix.cpp index bcf7918..f0635e8 100644 --- a/src/libipc/platform/posix/shm_posix.cpp +++ b/src/libipc/platform/posix/shm_posix.cpp @@ -51,7 +51,12 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) { } // For portable use, a shared memory object should be identified by name of the form /somename. // see: https://man7.org/linux/man-pages/man3/shm_open.3.html - ipc::string op_name = ipc::string{"/"} + name; + ipc::string op_name; + if (name[0] == '/') { + op_name = name; + } else { + op_name = ipc::string{"/"} + name; + } // Open the object for read-write access. int flag = O_RDWR; switch (mode) { @@ -206,7 +211,12 @@ void remove(char const * name) noexcept { return; } // For portable use, a shared memory object should be identified by name of the form /somename. - ipc::string op_name = ipc::string{"/"} + name; + ipc::string op_name; + if (name[0] == '/') { + op_name = name; + } else { + op_name = ipc::string{"/"} + name; + } int unlink_ret = ::shm_unlink(op_name.c_str()); if (unlink_ret == -1) { ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str());