public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Martin Sebor <msebor@gmail.com>
Cc: John Scott <jscott@posteo.net>, libc-alpha@sourceware.org
Subject: Re: [PATCH] manual: remove an obsolete requirement on aligned_alloc() usage
Date: Mon, 25 Oct 2021 18:50:44 -0700	[thread overview]
Message-ID: <ba8a6ea6-61b2-6bce-d5c3-1a8117c4c755@cs.ucla.edu> (raw)
In-Reply-To: <11bc87c2-d59c-0e32-79a4-68bf7d01a2b8@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 962 bytes --]

On 10/25/21 08:45, Martin Sebor via Libc-alpha wrote:
> Based on my code inspection of _mid_memalign in malloc.c I think
> a change that would more accurately reflect the implementation
> is one that described that when alignment is not a power of two
> it's bumped up to next larger power of two, and adjusted
> the EINVAL condition appropriately.

Let's not document this implementation detail, as it might tie us down 
unnecessarily in the future.

> I believe the DR460 change went into C17.  Here's an early C2x
> draft from 2018 publicly available on the WG14 site that should
> still be very close to C17:
> 
>   http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf

This says that aligned_alloc "shall fail" if the alignment is not a 
valid alignment supported by the implementation. This means the current 
glibc alloc_aligned behavior does not conform to C17 and should be fixed 
so that it does. Something like the attached (untested) patch, say.

[-- Attachment #2: align.patch --]
[-- Type: text/x-patch, Size: 1155 bytes --]

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 2ba1fee144..386fb5a4cb 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3416,6 +3416,12 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
   mstate ar_ptr;
   void *p;
 
+  if (!powerof2 (alignment))
+    {
+      __set_errno (EINVAL);
+      return 0;
+    }
+
   /* If we need less alignment than we give anyway, just relay to malloc.  */
   if (alignment <= MALLOC_ALIGNMENT)
     return __libc_malloc (bytes);
@@ -3424,24 +3430,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
   if (alignment < MINSIZE)
     alignment = MINSIZE;
 
-  /* If the alignment is greater than SIZE_MAX / 2 + 1 it cannot be a
-     power of 2 and will cause overflow in the check below.  */
-  if (alignment > SIZE_MAX / 2 + 1)
-    {
-      __set_errno (EINVAL);
-      return 0;
-    }
-
-
-  /* Make sure alignment is power of 2.  */
-  if (!powerof2 (alignment))
-    {
-      size_t a = MALLOC_ALIGNMENT * 2;
-      while (a < alignment)
-        a <<= 1;
-      alignment = a;
-    }
-
   if (SINGLE_THREAD_P)
     {
       p = _int_memalign (&main_arena, alignment, bytes);

  parent reply	other threads:[~2021-10-26  1:50 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-21  9:28 John Scott
2021-10-21 15:41 ` Alejandro Colomar (man-pages)
2021-10-25 15:45 ` Martin Sebor
2021-10-25 16:03   ` Martin Sebor
2021-10-26  1:50   ` Paul Eggert [this message]
2021-10-26  2:34     ` Martin Sebor

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ba8a6ea6-61b2-6bce-d5c3-1a8117c4c755@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=jscott@posteo.net \
    --cc=libc-alpha@sourceware.org \
    --cc=msebor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).