public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Kaz Kylheku <920-082-4242@kylheku.com>
To: cygwin@cygwin.com
Subject: Re: malloc(0) crashing with SIGABRT
Date: Thu, 12 Sep 2019 11:33:00 -0000	[thread overview]
Message-ID: <7ee0dd42337eff1b2a173d06d1cc5990@mail.kylheku.com> (raw)
In-Reply-To: <749ecff2-e384-bbfe-c961-481157c73052@SystematicSw.ab.ca>

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

  reply	other threads:[~2019-09-12  5:18 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-03  9:28 uc_sigmask set in a sigaction signal handler not honored Petr Skočík
2019-04-03 12:16 ` Corinna Vinschen
2019-04-03 12:43   ` Corinna Vinschen
2019-09-09 17:13 ` malloc(0) crashes with SIGABRT Petr Skočík
2019-09-09 18:27 ` malloc(0) crashing " Petr Skočík
2019-09-12  3:59   ` Brian Inglis
2019-09-12 11:33     ` Kaz Kylheku [this message]
2019-09-12 18:05       ` Brian Inglis
2019-09-12 17:38   ` Ken Brown
     [not found]     ` <e20e38ff-49b1-6472-b0a4-faeee0176a7c@gmail.com>
2019-09-13 13:21       ` Ken Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7ee0dd42337eff1b2a173d06d1cc5990@mail.kylheku.com \
    --to=920-082-4242@kylheku.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).