From owner-freebsd-hackers@FreeBSD.ORG Sun Jul 5 16:42:47 2009 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 0539D106567A for ; Sun, 5 Jul 2009 16:42:47 +0000 (UTC) (envelope-from freebsd-listen@fabiankeil.de) Received: from smtprelay11.ispgateway.de (smtprelay11.ispgateway.de [80.67.31.34]) by mx1.freebsd.org (Postfix) with ESMTP id 887D68FC18 for ; Sun, 5 Jul 2009 16:42:46 +0000 (UTC) (envelope-from freebsd-listen@fabiankeil.de) Received: from [62.143.132.243] (helo=localhost) by smtprelay11.ispgateway.de with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1MNUdN-0004x6-J6 for freebsd-hackers@freebsd.org; Sun, 05 Jul 2009 18:32:09 +0200 Date: Sun, 5 Jul 2009 18:28:56 +0200 From: Fabian Keil To: freebsd-hackers@freebsd.org Message-ID: <20090705182856.799b6b07@fabiankeil.de> X-Mailer: Claws Mail 3.7.2 (GTK+ 2.16.2; i386-portbld-freebsd8.0) X-PGP-KEY-URL: http://www.fabiankeil.de/gpg-keys/freebsd-listen-2008-08-18.asc Mime-Version: 1.0 Content-Type: multipart/signed; micalg=PGP-SHA1; boundary="Sig_/Q1rwH7nD2rW98mS+oZ97tZs"; protocol="application/pgp-signature" X-Df-Sender: 775067 Subject: Zero-length allocation with posix_memalign() X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Jul 2009 16:42:47 -0000 --Sig_/Q1rwH7nD2rW98mS+oZ97tZs Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I recently submitted a patch to the vlc developers that prevents a crash on FreeBSD 8.0 by not calling posix_memalign() with a size argument of zero. A simplified test case would be: #include int main(int argc, char **argv) { void *ptr; posix_memalign(&ptr, 16, 0); return (0); } which triggers: Assertion failed: (size !=3D 0), function arena_malloc, file /usr/src/lib/l= ibc/stdlib/malloc.c, line 3349. R=E9mi Denis-Courmont, one of the vlc developers, pointed out that passing a zero size to posix_memalign() should actually work, though: | In principle, while useless, there is no reason why allocating an empty=20 | picture should not be possible. posix_memalign() does support zero-length= =20 | allocation anyway: | http://www.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.h= tml | | If the size of the space requested is 0, the behavior is | | implementation-defined; the value returned in memptr shall be either a | | null pointer or a unique pointer. http://mailman.videolan.org/pipermail/vlc-devel/2009-July/062299.html I get the impression that this deviation from the standard could be easily fixed with something similar to the following, which is mostly copy and pasted from malloc(): index 5404798..a078d07 100644 --- a/malloc.c +++ b/malloc.c @@ -5303,6 +5303,15 @@ posix_memalign(void **memptr, size_t alignment, size= _t size) int ret; void *result; =20 + if (size =3D=3D 0) { + if (opt_sysv =3D=3D false) + size =3D 1; + else { + ret =3D 0; + *memptr =3D result =3D NULL; + goto RETURN; + } + } if (malloc_init()) result =3D NULL; else { I assume the "goto RETURN" isn't entirely compliant either as it skips the alignment check, but so does the malloc_init() failure branch. Fabian --Sig_/Q1rwH7nD2rW98mS+oZ97tZs Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.12 (FreeBSD) iEYEARECAAYFAkpQ1MwACgkQBYqIVf93VJ1G3wCgv+JfZxs0ptB4RcoqT7HT7YZ7 r7EAoJ2RofKSRVTIUYLArttbqHEI835p =djX3 -----END PGP SIGNATURE----- --Sig_/Q1rwH7nD2rW98mS+oZ97tZs--