From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 2261 invoked by alias); 12 Sep 2019 05:18:25 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 2248 invoked by uid 89); 12 Sep 2019 05:18:25 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_00,FROM_STARTS_WITH_NUMS,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=no version=3.3.1 spammy=HX-Languages-Length:1379, H*u:0.9.2, H*UA:0.9.2, sus X-HELO: smtp-out-no.shaw.ca Received: from smtp-out-no.shaw.ca (HELO smtp-out-no.shaw.ca) (64.59.134.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 12 Sep 2019 05:18:23 +0000 Received: from kylheku.com ([70.79.163.252]) by shaw.ca with ESMTPA id 8HUpiPWCmsAGk8HUqiboXs; Wed, 11 Sep 2019 23:18:21 -0600 Received: from www-data by kylheku.com with local (Exim 4.72) (envelope-from <920-082-4242@kylheku.com>) id 1i8HUp-0003vm-DX for cygwin@cygwin.com; Wed, 11 Sep 2019 22:18:19 -0700 To: cygwin@cygwin.com Subject: Re: malloc(0) crashing with SIGABRT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Date: Thu, 12 Sep 2019 11:33:00 -0000 From: Kaz Kylheku <920-082-4242@kylheku.com> In-Reply-To: <749ecff2-e384-bbfe-c961-481157c73052@SystematicSw.ab.ca> References: <366918d8-b505-45be-dc28-303579f17341@gmail.com> <78e19eb7-956e-cd05-a076-e56ce347bbbe@gmail.com> <749ecff2-e384-bbfe-c961-481157c73052@SystematicSw.ab.ca> Message-ID: <7ee0dd42337eff1b2a173d06d1cc5990@mail.kylheku.com> X-Sender: 920-082-4242@kylheku.com User-Agent: Roundcube Webmail/0.9.2 X-IsSubscribed: yes X-SW-Source: 2019-09/txt/msg00135.txt.bz2 On 2019-09-11 20:59, Brian Inglis wrote: > On 2019-09-09 11:13, Petr Skočík wrote: >> There's been a twitter discussion on how different POSIX platforms >> handle malloc(0): >> https://twitter.com/sortiecat/status/1170697927804817412 . >> >> As for Cygwin, the answer appears to be "not well", but this should be >> easy to fix. > > POSIX SUS V4 2018 says: > > "RETURN VALUE > > Upon successful completion with size not equal to 0, malloc() shall > return a > pointer to the allocated space. If size is 0, either: > > A null pointer shall be returned [CX] [Option Start] and errno may be > set to > an implementation-defined value, [Option End] or > > A pointer to the allocated space shall be returned. The application > shall > ensure that the pointer is not used to access an object. > > Otherwise, it shall return a null pointer [CX] [Option Start] and set > errno to > indicate the error. [Option End]" > > The second option could be implemented by a pointer to an unmapped > page, or a > reference to an inaccessible mmap-ed area length zero. That's easy: the null pointer, plus some small offset that observes alignment, like 16. (Alignment is important even if the memory isn't accessed, because nonportable programs depend on it for other reasons, like being able to use the least significant few bits of a pointer for tagging.) -- Problem reports: http://cygwin.com/problems.html FAQ: http://cygwin.com/faq/ Documentation: http://cygwin.com/docs.html Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple