Patchwork grp/grp_merge.c alignment fix

login
register
mail settings
Submitter DJ Delorie
Date July 14, 2017, 9:52 p.m.
Message ID <xnvamuvg7y.fsf@greed.delorie.com>
Download mbox | patch
Permalink /patch/21625/
State New
Headers show

Comments

DJ Delorie - July 14, 2017, 9:52 p.m.
Updated patch with the boolean coercion removed.

	[BZ #21654]
	* grp/grp_merge.c (__copy_grp): Make sure pointers-to-not-char
	are properly aligned.
	(__merge_grp): Likewise.
Carlos O'Donell - July 15, 2017, 1:30 a.m.
On 07/14/2017 05:52 PM, DJ Delorie wrote:
> 
> Updated patch with the boolean coercion removed.
 
OK with ChangeLog tweak.

> 	[BZ #21654]
> 	* grp/grp_merge.c (__copy_grp): Make sure pointers-to-not-char
> 	are properly aligned.

Suggest:

	* grp/grp_merge.c (__copy_grp): Align char** to minimum pointer
	alignment not char alignment.

Should say in first person what we are doing and succintly.

> 	(__merge_grp): Likewise.
> 
> diff --git a/grp/grp-merge.c b/grp/grp-merge.c
> index 77c494d..6590e5d 100644
> --- a/grp/grp-merge.c
> +++ b/grp/grp-merge.c
> @@ -85,6 +85,14 @@ __copy_grp (const struct group srcgrp, const size_t buflen,
>      }
>    members[i] = NULL;
>  
> +  /* Align for pointers.  We can't simply align C because we need to
> +     align destbuf[c].  */
> +  if ((((uintptr_t)destbuf + c) & (__alignof__(char **) - 1)) != 0)
> +    {
> +      uintptr_t mis_align = ((uintptr_t)destbuf + c) & (__alignof__(char **) - 1);
> +      c += __alignof__(char **) - mis_align;
> +    }
> +
>    /* Copy the pointers from the members array into the buffer and assign them
>       to the gr_mem member of destgrp.  */
>    destgrp->gr_mem = (char **) &destbuf[c];
> @@ -168,6 +176,14 @@ __merge_grp (struct group *savedgrp, char *savedbuf, char *savedend,
>    /* Add the NULL-terminator.  */
>    members[savedmemcount + memcount] = NULL;
>  
> +  /* Align for pointers.  We can't simply align C because we need to
> +     align savedbuf[c].  */
> +  if ((((uintptr_t)savedbuf + c) & (__alignof__(char **) - 1)) != 0)
> +    {
> +      uintptr_t mis_align = ((uintptr_t)savedbuf + c) & (__alignof__(char **) - 1);
> +      c += __alignof__(char **) - mis_align;
> +    }
> +
>    /* Copy the member array back into the buffer after the member list and free
>       the member array.  */
>    savedgrp->gr_mem = (char **) &savedbuf[c];
>

Patch

diff --git a/grp/grp-merge.c b/grp/grp-merge.c
index 77c494d..6590e5d 100644
--- a/grp/grp-merge.c
+++ b/grp/grp-merge.c
@@ -85,6 +85,14 @@  __copy_grp (const struct group srcgrp, const size_t buflen,
     }
   members[i] = NULL;
 
+  /* Align for pointers.  We can't simply align C because we need to
+     align destbuf[c].  */
+  if ((((uintptr_t)destbuf + c) & (__alignof__(char **) - 1)) != 0)
+    {
+      uintptr_t mis_align = ((uintptr_t)destbuf + c) & (__alignof__(char **) - 1);
+      c += __alignof__(char **) - mis_align;
+    }
+
   /* Copy the pointers from the members array into the buffer and assign them
      to the gr_mem member of destgrp.  */
   destgrp->gr_mem = (char **) &destbuf[c];
@@ -168,6 +176,14 @@  __merge_grp (struct group *savedgrp, char *savedbuf, char *savedend,
   /* Add the NULL-terminator.  */
   members[savedmemcount + memcount] = NULL;
 
+  /* Align for pointers.  We can't simply align C because we need to
+     align savedbuf[c].  */
+  if ((((uintptr_t)savedbuf + c) & (__alignof__(char **) - 1)) != 0)
+    {
+      uintptr_t mis_align = ((uintptr_t)savedbuf + c) & (__alignof__(char **) - 1);
+      c += __alignof__(char **) - mis_align;
+    }
+
   /* Copy the member array back into the buffer after the member list and free
      the member array.  */
   savedgrp->gr_mem = (char **) &savedbuf[c];