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.
This commit is contained in:
mutouyun 2025-12-06 16:06:51 +08:00
parent addfe4f5cf
commit ce0773b3e6

View File

@ -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());