[Bug 269688] memalign() produces division by zero if size is 0

From: <bugzilla-noreply_at_freebsd.org>
Date: Sun, 19 Feb 2023 16:31:14 UTC

            Bug ID: 269688
           Summary: memalign() produces division by zero if size is 0
           Product: Base System
           Version: 13.1-RELEASE
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: pjfloyd@wanadoo.fr

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=<optimized out>) at
39              return (aligned_alloc(align, roundup(size, align)));

My source code is

91         p = memalign(0, 100);      assert(NULL == p);

You are receiving this mail because:
You are the assignee for the bug.