about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-02-03 14:19:27 -0300
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2021-02-08 14:10:42 -0300
commitc1f46f9db723d88082899ee703aa00779230d510 (patch)
tree934e273f6ba6db7165bad773f031741d00da5667
parentda4aea0b5e60ec2351367b0facee24e6035a7129 (diff)
downloadglibc-c1f46f9db723d88082899ee703aa00779230d510.tar.gz
glibc-c1f46f9db723d88082899ee703aa00779230d510.tar.xz
glibc-c1f46f9db723d88082899ee703aa00779230d510.zip
pthread: Remove alloca usage from __sem_check_add_mapping
sem_open already returns EINVAL for input names larger than NAME_MAX,
so it can assume the largest name length with tfind.

Checked on x86_64-linux-gnu.
-rw-r--r--sysdeps/pthread/sem_routines.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/sysdeps/pthread/sem_routines.c b/sysdeps/pthread/sem_routines.c
index 78d9364ebd..ea7a445324 100644
--- a/sysdeps/pthread/sem_routines.c
+++ b/sysdeps/pthread/sem_routines.c
@@ -31,6 +31,15 @@ struct inuse_sem
   char name[];
 };
 
+struct search_sem
+{
+  dev_t dev;
+  ino_t ino;
+  int refcnt;
+  sem_t *sem;
+  char name[NAME_MAX];
+};
+
 /* Comparison function for search of existing mapping.  */
 static int
 sem_search (const void *a, const void *b)
@@ -61,6 +70,9 @@ sem_t *
 __sem_check_add_mapping (const char *name, int fd, sem_t *existing)
 {
   size_t namelen = strlen (name);
+  if (namelen > NAME_MAX)
+    return SEM_FAILED;
+
   sem_t *result = SEM_FAILED;
 
   /* Get the information about the file.  */
@@ -71,13 +83,12 @@ __sem_check_add_mapping (const char *name, int fd, sem_t *existing)
       lll_lock (sem_mappings_lock, LLL_PRIVATE);
 
       /* Search for an existing mapping given the information we have.  */
-      struct inuse_sem *fake;
-      fake = (struct inuse_sem *) alloca (sizeof (*fake) + namelen);
-      memcpy (fake->name, name, namelen);
-      fake->dev = st.st_dev;
-      fake->ino = st.st_ino;
+      struct search_sem fake;
+      memcpy (fake.name, name, namelen);
+      fake.dev = st.st_dev;
+      fake.ino = st.st_ino;
 
-      struct inuse_sem **foundp = __tfind (fake, &sem_mappings, sem_search);
+      struct inuse_sem **foundp = __tfind (&fake, &sem_mappings, sem_search);
       if (foundp != NULL)
 	{
 	  /* There is already a mapping.  Use it.  */