Skip to content

Commit ce0773b

Browse files
committed
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.
1 parent addfe4f commit ce0773b

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/libipc/platform/posix/shm_posix.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ id_t acquire(char const * name, std::size_t size, unsigned mode) {
5151
}
5252
// For portable use, a shared memory object should be identified by name of the form /somename.
5353
// see: https://man7.org/linux/man-pages/man3/shm_open.3.html
54-
ipc::string op_name = ipc::string{"/"} + name;
54+
ipc::string op_name;
55+
if (name[0] == '/') {
56+
op_name = name;
57+
} else {
58+
op_name = ipc::string{"/"} + name;
59+
}
5560
// Open the object for read-write access.
5661
int flag = O_RDWR;
5762
switch (mode) {
@@ -206,7 +211,12 @@ void remove(char const * name) noexcept {
206211
return;
207212
}
208213
// For portable use, a shared memory object should be identified by name of the form /somename.
209-
ipc::string op_name = ipc::string{"/"} + name;
214+
ipc::string op_name;
215+
if (name[0] == '/') {
216+
op_name = name;
217+
} else {
218+
op_name = ipc::string{"/"} + name;
219+
}
210220
int unlink_ret = ::shm_unlink(op_name.c_str());
211221
if (unlink_ret == -1) {
212222
ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str());

0 commit comments

Comments
 (0)