X-Recipient: archive-cygwin@delorie.com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:reply-to:subject:to:references:from:message-id
	:date:mime-version:in-reply-to:content-type
	:content-transfer-encoding; q=dns; s=default; b=JZWfO8PHQf3zu26i
	4oBKOPw0O4gk8xgiiLxrtpibNUc0sydM2Su/a+jpTfQt9lm/sUJ636BvvfbSyVRU
	3ykmacfIMyhg/b48cdyGp3yw6Wpghc+aMWx3+xiN4UvanbPgSftEEc41M5jbJGqf
	hFOA94h+0S4vnb95JgBMVvO+e6g=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
	:list-unsubscribe:list-subscribe:list-archive:list-post
	:list-help:sender:reply-to:subject:to:references:from:message-id
	:date:mime-version:in-reply-to:content-type
	:content-transfer-encoding; s=default; bh=9TfaqLhhrRtFrYPmnGXbhl
	Rlrh0=; b=KqdzAnp/RssK987gQRJQ25+kX+Df7DuCcMv6+tkkpKhzMCsrmAbZ0i
	weG80wCHjoXuk9lrwanFJ2SuJREcJHkxzt95vOgsjVRGPyyi93Fhy7LMlMlLsGto
	ZbpwOEqpDg1foy3lE37KKFEIABDNZFTEbtvu2ZPagmzSBcH3+Y89o=
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Authentication-Results: sourceware.org; auth=none
X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 spammy=kylheku, kaz, Kylheku, Kaz
X-HELO: smtp-out-so.shaw.ca
Reply-To: Brian.Inglis@SystematicSw.ab.ca
Subject: Re: malloc(0) crashing with SIGABRT
To: cygwin@cygwin.com
References: <366918d8-b505-45be-dc28-303579f17341@gmail.com> <78e19eb7-956e-cd05-a076-e56ce347bbbe@gmail.com> <749ecff2-e384-bbfe-c961-481157c73052@SystematicSw.ab.ca> <7ee0dd42337eff1b2a173d06d1cc5990@mail.kylheku.com>
From: Brian Inglis <Brian.Inglis@SystematicSw.ab.ca>
Openpgp: preference=signencrypt
Message-ID: <50691acc-9ae3-14e8-1080-bab73239650e@SystematicSw.ab.ca>
Date: Thu, 12 Sep 2019 11:38:42 -0600
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0
MIME-Version: 1.0
In-Reply-To: <7ee0dd42337eff1b2a173d06d1cc5990@mail.kylheku.com>
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-IsSubscribed: yes
Note-from-DJ: This may be spam

On 2019-09-11 23:18, Kaz Kylheku wrote:
> 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.

It's more a question of what the NULL pointer maps to: I liked systems mapping
NULL pointers to inaccessible pages; and compilers that allow bss to be filled
with bits: carelessness got caught fast!

> (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.)

[Keeping tag bits or a byte elsewhere is less overhead than the instructions
required to sanitize tainted pointers before use, assuming all the code
remembers to do so, and those programs deserve what they get! Blargh!]

-- 
Take care. Thanks, Brian Inglis, Calgary, Alberta, Canada

This email may be disturbing to some readers as it contains
too much technical detail. Reader discretion is advised.

--
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

