Re: malloc(0) crashing with SIGABRT
On 9/13/2019 3:38 AM, Petr Skočík wrote: > On 9/12/19 6:12 PM, Ken Brown wrote: >> gcc -Wall -o malloc_zero malloc_zero.c > > My apologies. It was my own stupid mistake. > > ( > I had > > gcc -include stdlib.h -xc - <<<'int main(){ }' && ./a.out; echo $? > > where I would normally run $aout which in my shell setup is > a.exe on cygwin and a.out elsewhere, but this time I accidentally > typed a.out and I happened to have a Linux a.out there and that > was causing the crash. > ) No problem. I'm glad to hear there's no Cygwin issue. Ken
Re: malloc(0) crashing with SIGABRT
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
Re: malloc(0) crashing with SIGABRT
On 9/9/2019 1:13 PM, 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. Can you show how you produced a crash? It works fine for me with the following test program: $ cat malloc_zero.c #include #include int main () { printf ("malloc (0) = %p\n", malloc (0)); } $ gcc -Wall -o malloc_zero malloc_zero.c $ ./malloc_zero.exe malloc (0) = 0x803c0 [This is on 64-bit Cygwin. It's the same on 32-bit, but with a different address.] Ken -- 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
Re: malloc(0) crashing with SIGABRT
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
Re: malloc(0) crashing with SIGABRT
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. -- 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
malloc(0) crashing with SIGABRT
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. Best regards, Petr Skocik -- 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