diff mbox series

[3/3] pthread: Remove alloca usage from __sem_check_add_mapping

Message ID 20210203183414.571813-3-adhemerval.zanella@linaro.org
State New
Headers show
Series [1/3] linux: Require /dev/shm as the shared memory file system [BZ #25383] | expand

Commit Message

Adhemerval Zanella Feb. 3, 2021, 6:34 p.m. UTC
sem_open already returns EINVAL for input names larger than NAME_MAX,
so it can assume the largest name length to with tfind.

Checked on x86_64-linux-gnu.
---
 sysdeps/pthread/sem_routines.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

Comments

Florian Weimer Feb. 7, 2021, 9:29 a.m. UTC | #1
* Adhemerval Zanella via Libc-alpha:

> sem_open already returns EINVAL for input names larger than NAME_MAX,
> so it can assume the largest name length to with tfind.

Typo: “to with”

> @@ -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 NULL;
> +
>    sem_t *result = SEM_FAILED;

Should this return SEM_FAILED?

Thanks,
Florian
Adhemerval Zanella Feb. 8, 2021, 4:31 p.m. UTC | #2
On 07/02/2021 06:29, Florian Weimer wrote:
> * Adhemerval Zanella via Libc-alpha:
> 
>> sem_open already returns EINVAL for input names larger than NAME_MAX,
>> so it can assume the largest name length to with tfind.
> 
> Typo: “to with”

Ack.

> 
>> @@ -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 NULL;
>> +
>>    sem_t *result = SEM_FAILED;
> 
> Should this return SEM_FAILED?

It should, I have fixed it.

> 
> Thanks,
> Florian
>
diff mbox series

Patch

diff --git a/sysdeps/pthread/sem_routines.c b/sysdeps/pthread/sem_routines.c
index 78d9364ebd..d5e2f56547 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 NULL;
+
   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.  */