30 January 2023, 00:09 | #1 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Different Results for malloc(0) Without/With Optimisations with VBCC
Hi there!
I stumble upon a problem with malloc() when the size is 0. I have this piece of code that can receive different sizes (depending on the HTTP answer from a Web server). Essentially: Code:
hresp->body = malloc(number_of_bytes); if(hresp->body == NULL) { temp_string = NULL; log_print_error("_http_request(), could not allocate response body\n"); goto _RETURN_ERROR; } Without optimisation: Code:
all : COFLAGS += -O=1 -g -hunkdebug -DFORTIFY -DUNRECOGNISED all : ASFLAGS += all : LDFLAGS += -S -s -sc -sd Code:
dist : COFLAGS += -O=3 -size -final dist : ASFLAGS += -opt-size dist : LDFLAGS += Cheers! Last edited by tygre; 30 January 2023 at 00:39. |
30 January 2023, 02:28 | #2 |
WinUAE 4000/40, V4SA
Join Date: Apr 2020
Location: East of Oshawa
Posts: 538
|
Why are you allocating 0 bytes, though? Would it be possible to just skip malloc() when you aren't allocating anything?
|
30 January 2023, 05:32 | #3 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Hi ColdAcid!
Yes, I know have a if that checks for zero value but I preferred my code before, it was simpler, more "linear" . The reason for have a zero value is that the body of a HTTP response may be of 0-length, for example in the case of a redirect (status code 302). Still, I wonder if the difference of behaviours is expected wrt. the difference compiler optimisations? Take care! |
30 January 2023, 08:39 | #4 |
Registered User
Join Date: Jan 2005
Location: Umeå
Age: 43
Posts: 922
|
Content-Length is not required in the answer and might be missing for unknown reasons. An example where it is always missing is for streaming sources.
|
30 January 2023, 08:41 | #5 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,121
|
Have you checked what the C library says about it? It sounds like something that might be undefined behaviour.
|
30 January 2023, 09:27 | #6 | |
Registered User
Join Date: Jan 2019
Location: Germany
Posts: 3,215
|
Both is actually allowed.
Quote:
|
|
30 January 2023, 10:22 | #7 |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,121
|
Personally I'd never try to allocate zero bytes. Sounds like a logical error to me.
|
30 January 2023, 10:34 | #8 |
ex. demoscener "Bigmama"
Join Date: Jun 2012
Location: Fyn / Denmark
Posts: 1,624
|
|
30 January 2023, 15:08 | #9 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Hi all!
Exactly my point, HooverPhonique: I liked the case "a unique pointer value that can later be successfully passed to free()" because it made my code more straightforward I'm just wondering if it's expected from VBCC (or something else?) to have two different behaviours depending on the compilation options? Cheers! |
30 January 2023, 15:44 | #10 | |
Alien Bleed
Join Date: Aug 2022
Location: UK
Posts: 4,121
|
Quote:
|
|
30 January 2023, 16:04 | #11 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
PS. Here is the problem is with malloc(0)
|
30 January 2023, 17:41 | #12 | ||
Registered User
Join Date: Jan 2021
Location: Germany
Posts: 18
|
Quote:
Quote:
|
||
30 January 2023, 17:56 | #13 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Hi VBC!
I'm targetting m86k-amigos, compiling against NDK v3.2R4. Thanks for the tip about the optimisation! Update: I tried O=-1 and the resulting object files are broken: they are between 28 and 40 bytes instead of "normal" sizes Cheers! Last edited by tygre; 30 January 2023 at 18:12. |
30 January 2023, 18:42 | #14 | |
Registered User
Join Date: Jan 2021
Location: Germany
Posts: 18
|
Strange. I double-checked the vc.lib from the target-archive and it does contain a check for 0 in malloc(). Are you sure about the malloc(0)?
Quote:
|
|
30 January 2023, 20:17 | #15 | ||
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Quote:
It bit my behind when I released the "release" version of my code (with the optimisations), in which malloc(0) was now returning NULL instead of a "valid" pointer. Quote:
|
||
31 January 2023, 17:33 | #16 |
Registered User
Join Date: Jan 2021
Location: Germany
Posts: 18
|
Yes, what seems strange to me is that the non-optimized version would have returned non-zero for malloc(0). Are you sure that there are no other differences between those builds?
|
31 January 2023, 20:03 | #17 | |
Natteravn
Join Date: Nov 2009
Location: Herford / Germany
Posts: 2,496
|
Quote:
Maybe some of your defines in the no-optmisation build (like -DFORTIFY) overload vclib's malloc() and implement your own functions for debugging purposes? Otherwise, prepare a small example for reproduction and I will happily analyse it. |
|
01 February 2023, 01:26 | #18 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Hi there
Indeed it could be something like Fortify... let me prepare this MWE and see if I can reproduce this difference! Will keep you posted! |
04 February 2023, 17:53 | #19 |
Returning fan!
Join Date: Jan 2011
Location: Montréal, QC, Canada
Posts: 1,434
|
Hi there!
So I created a MWE and, indeed, it's Fortify that changes the behaviour of malloc(0)! With Fortify enabled, malloc(0) returns a non-NULL pointer ("0 bytes in 1 blocks with 104 bytes overheard") while, with Fortify disabled, malloc(0) returns NULL. Mystery solved! (I joined this MWE, just (un)comment -DFORTIFY in the makefile to see the difference.) Cheers! |
15 February 2023, 13:15 | #20 | |
Registered User
Join Date: Aug 2010
Location: Germany
Posts: 532
|
Quote:
The second behaviour is very common on POSIX systems and the malloc/calloc/realloc implementations in my clib2 code feature this behaviour if you use libunix.a instead of the libc.a variant (which uses the first behaviour). Small computer systems such as the Amiga tend to have a different view here with regard to what the standard library functions might do. malloc(0) might not yield option #1 (a NULL pointer), option #2 (a pointer to a small memory allocation), but option #3477: memory corruption with or without a crash later or maybe sooner. All this goes to say that you should never ever put your trust into the 'C' standard library to do right by you under circumstances which challenge the specification of the respective library function. If you read the historic documentation for it, e.g. Manx Aztec 'C' of yore, you will find that it mostly describes the "happy case" and leaves the likely undefined behaviour outside the specified use to the imagination of the reader. Bottom line is that you need to be most careful on the Amiga, reviewing code you are porting for gotchas such as the malloc(0) jack-in-the-box. Defensive programming is called for, not just for your own code but also for somebody else's... |
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
68000 code optimisations | pmc | Coders. Asm / Hardware | 248 | 17 September 2023 13:20 |
optimisations for 68000 | Galahad/FLT | Coders. Asm / Hardware | 16 | 13 January 2023 11:45 |
AllocSysObject vs malloc? | Sim085 | Coders. C/C++ | 12 | 17 December 2022 17:47 |
16x16 CPU tile flip optimisations | mcgeezer | Coders. Asm / Hardware | 51 | 20 February 2021 11:54 |
Is there some compiler providing resource tracking for malloc/free/FILE* handles? | jotd | Coders. C/C++ | 3 | 29 December 2018 11:58 |
|
|