public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc] rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX}
@ 2023-03-08 13:39 Adhemerval Zanella
  0 siblings, 0 replies; only message in thread
From: Adhemerval Zanella @ 2023-03-08 13:39 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=0b7bf0e0a486da6be7c5dde742a80c1138f9cc89

commit 0b7bf0e0a486da6be7c5dde742a80c1138f9cc89
Author: abushwang <abushwangs@gmail.com>
Date:   Tue Mar 7 20:16:20 2023 +0800

    rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX}
    
    according to man-pages-posix-2017, shm_open() function may fail if the length
    of the name argument exceeds {_POSIX_PATH_MAX} and set ENAMETOOLONG
    
    Signed-off-by: abushwang <abushwangs@gmail.com>
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

Diff:
---
 posix/shm-directory.c      | 12 +++++++++---
 rt/shm_open.c              |  5 +++--
 sysdeps/pthread/sem_open.c |  5 +++--
 3 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/posix/shm-directory.c b/posix/shm-directory.c
index 86d9fd8e4f..f130bab3c2 100644
--- a/posix/shm-directory.c
+++ b/posix/shm-directory.c
@@ -25,6 +25,7 @@
 #include <string.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include <errno.h>
 
 int
 __shm_get_name (struct shmdir_name *result, const char *name, bool sem_prefix)
@@ -54,9 +55,14 @@ __shm_get_name (struct shmdir_name *result, const char *name, bool sem_prefix)
   if (sem_prefix)
     alloc_buffer_copy_bytes (&buffer, "sem.", strlen ("sem."));
   alloc_buffer_copy_bytes (&buffer, name, namelen + 1);
-  if (namelen == 0 || memchr (name, '/', namelen) != NULL
-      || alloc_buffer_has_failed (&buffer))
-    return -1;
+  if (namelen == 0 || memchr (name, '/', namelen) != NULL)
+    return EINVAL;
+  if (alloc_buffer_has_failed (&buffer))
+    {
+      if (namelen > NAME_MAX)
+        return ENAMETOOLONG;
+      return EINVAL;
+    }
   return 0;
 }
 libc_hidden_def (__shm_get_name)
diff --git a/rt/shm_open.c b/rt/shm_open.c
index 6c1f4d604f..fc1dc96bb4 100644
--- a/rt/shm_open.c
+++ b/rt/shm_open.c
@@ -30,9 +30,10 @@ int
 __shm_open (const char *name, int oflag, mode_t mode)
 {
   struct shmdir_name dirname;
-  if (__shm_get_name (&dirname, name, false) != 0)
+  int ret =__shm_get_name (&dirname, name, false);
+  if (ret != 0)
     {
-      __set_errno (EINVAL);
+      __set_errno (ret);
       return -1;
     }
 
diff --git a/sysdeps/pthread/sem_open.c b/sysdeps/pthread/sem_open.c
index 2551b035e1..2d32a13557 100644
--- a/sysdeps/pthread/sem_open.c
+++ b/sysdeps/pthread/sem_open.c
@@ -47,9 +47,10 @@ __sem_open (const char *name, int oflag, ...)
     }
 
   struct shmdir_name dirname;
-  if (__shm_get_name (&dirname, name, true) != 0)
+  int ret = __shm_get_name (&dirname, name, true);
+  if (ret != 0)
     {
-      __set_errno (EINVAL);
+      __set_errno (ret);
       return SEM_FAILED;
     }

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-08 13:39 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 13:39 [glibc] rt: fix shm_open not set ENAMETOOLONG when name exceeds {_POSIX_PATH_MAX} Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).