<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.freebsd.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.freebsd.org/bugzilla/"
          
          maintainer="bugmeister@FreeBSD.org"
>

    <bug>
          <bug_id>269688</bug_id>
          
          <creation_ts>2023-02-19 16:31:14 +0000</creation_ts>
          <short_desc>memalign() produces division by zero if alignment is 0</short_desc>
          <delta_ts>2023-09-25 04:34:59 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>Base System</product>
          <component>bin</component>
          <version>13.1-RELEASE</version>
          <rep_platform>Any</rep_platform>
          <op_sys>Any</op_sys>
          <bug_status>Closed</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>---</priority>
          <bug_severity>Affects Some People</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Paul Floyd">pjfloyd</reporter>
          <assigned_to name="freebsd-bugs (Nobody)">bugs</assigned_to>
          <cc>emaste</cc>
    
    <cc>freebsd</cc>
    
    <cc>kib</cc>
    
    <cc>pjfloyd</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1247677</commentid>
    <comment_count>0</comment_count>
    <who name="Paul Floyd">pjfloyd</who>
    <bug_when>2023-02-19 16:31:14 +0000</bug_when>
    <thetext>The implementation is to just call aligned_alloc():

	return (aligned_alloc(align, roundup(size, align)));

where roundup is this macro

:#define        roundup(x, y)   ((((x)+((y)-1))/(y))*(y))  /* to any y */

If size is 0 that will be

   (0 + align - 1) / 0 * 0


That gives me

(gdb) r
Starting program: /usr/home/paulf/scratch/valgrind/memcheck/tests/memalign2 

Program received signal SIGFPE, Arithmetic exception.
Integer divide by zero.
0x00000008002f3415 in memalign (align=0, size=&lt;optimized out&gt;) at /usr/src/lib/libc/gen/memalign.c:39
39              return (aligned_alloc(align, roundup(size, align)));

My source code is

91         p = memalign(0, 100);      assert(NULL == p);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1247678</commentid>
    <comment_count>1</comment_count>
    <who name="Mina Galić">freebsd</who>
    <bug_when>2023-02-19 16:34:17 +0000</bug_when>
    <thetext>that function is also undocumented, so it&apos;s hard to know what the expected behaviour should be.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1247686</commentid>
    <comment_count>2</comment_count>
    <who name="Paul Floyd">pjfloyd</who>
    <bug_when>2023-02-19 17:14:00 +0000</bug_when>
    <thetext>On other platforms the behaviour is:

macOS doesn&apos;t exist
Linux glibc

just calls malloc

https://elixir.bootlin.com/glibc/glibc-2.37.9000/source/malloc/malloc.c#L3510
  /* If we need less alignment than we give anyway, just relay to malloc.  */
  if (alignment &lt;= MALLOC_ALIGNMENT)
    return __libc_malloc (bytes);

musl

just calls malloc

https://github.com/esmil/musl/blob/master/src/malloc/memalign.c

if (align &lt;= 4*sizeof(size_t)) {
		if (!(mem = malloc(len)))
			return NULL;
		return mem;
	}

illimos

sets EINVAL and returns NULL

https://github.com/illumos/illumos-gate/blob/master/usr/src/lib/libc/port/gen/memalign.c

	/*
	 * check for valid size and alignment parameters
	 * MAX_ALIGN check prevents overflow in later calculation.
	 */
	if (nbytes == 0 || _misaligned(align) || align == 0 ||
	    align &gt; MAX_ALIGN) {
		errno = EINVAL;
		return (NULL);
	}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1247688</commentid>
    <comment_count>3</comment_count>
    <who name="Paul Floyd">pjfloyd</who>
    <bug_when>2023-02-19 17:17:18 +0000</bug_when>
    <thetext>And I would just pass it on to aligned_alloc:

void *
memalign(size_t align, size_t size)
{
	return (aligned_alloc(align, size ? roundup(size, align) : size));
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1248229</commentid>
    <comment_count>4</comment_count>
    <who name="Ed Maste">emaste</who>
    <bug_when>2023-02-23 00:16:08 +0000</bug_when>
    <thetext>In `p = memalign(0, 100);` align is 0 and size is 100.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1248249</commentid>
    <comment_count>5</comment_count>
    <who name="Paul Floyd">pjfloyd</who>
    <bug_when>2023-02-23 06:32:20 +0000</bug_when>
    <thetext>(In reply to Ed Maste from comment #4)

Yes you&apos;re right. Sorry for the brain fart. I&apos;ll continue on github (with the advantage that I can edit my mistakes more easily).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1248516</commentid>
    <comment_count>6</comment_count>
    <who name="">commit-hook</who>
    <bug_when>2023-02-24 18:21:58 +0000</bug_when>
    <thetext>A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=2c709ee70ade9fd8f77b37917a4169d667dda41d

commit 2c709ee70ade9fd8f77b37917a4169d667dda41d
Author:     Paul Floyd &lt;pjfloyd@wanadoo.fr&gt;
AuthorDate: 2023-02-24 16:29:01 +0000
Commit:     Ed Maste &lt;emaste@FreeBSD.org&gt;
CommitDate: 2023-02-24 18:19:06 +0000

    libc: handle zero alignment in memalign()

    For compatibility with glibc. The previous code would trigger a division
    by zero in roundup() and terminate.  Instead, just pass through to
    malloc() for align == 0.

    PR:             269688
    Reviewed by:    imp, mjg
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/655

 lib/libc/gen/memalign.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1249281</commentid>
    <comment_count>7</comment_count>
    <who name="">commit-hook</who>
    <bug_when>2023-03-03 00:24:10 +0000</bug_when>
    <thetext>A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=1b1d2aea020e88b3d2076207b13fec3adde42aa9

commit 1b1d2aea020e88b3d2076207b13fec3adde42aa9
Author:     Paul Floyd &lt;pjfloyd@wanadoo.fr&gt;
AuthorDate: 2023-02-24 16:29:01 +0000
Commit:     Ed Maste &lt;emaste@FreeBSD.org&gt;
CommitDate: 2023-03-03 00:23:24 +0000

    libc: handle zero alignment in memalign()

    For compatibility with glibc. The previous code would trigger a division
    by zero in roundup() and terminate.  Instead, just pass through to
    malloc() for align == 0.

    PR:             269688
    Reviewed by:    imp, mjg
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/655

    (cherry picked from commit 2c709ee70ade9fd8f77b37917a4169d667dda41d)

 lib/libc/gen/memalign.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1276847</commentid>
    <comment_count>8</comment_count>
    <who name="Paul Floyd">pjfloyd</who>
    <bug_when>2023-09-25 04:34:59 +0000</bug_when>
    <thetext> libc: handle zero alignment in memalign()

For compatibility with glibc. The previous code would trigger a division
by zero in roundup() and terminate.  Instead, just pass through to
malloc() for align == 0.

PR:		269688
Reviewed by:	imp, mjg
MFC after:	1 week
Pull Request:	#655</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>