result = New datatype[ count ]
I think that when count<0 (constant value assigned or variable), the compiler must not hang and a null pointer should be always returned.
Comparison with Allocate, Callocate, Reallocate
result = [C]Allocate( count )
- if count<0, a null pointer is returned
- if count>=0, a non nul pointer is returned (even for count=0)
result = Reallocate( pointer, count )
- if count<0, a null pointer is returned
- if count=0 and pointer=0, a non null pointer is returned (as similar to [C]Allocate behavior)
- if count=0 and pointer>0, a null pointer is returned (syntax equivalent to Deallocate)
- if count>0, a non null pointer is returned
Referring to forum at post:
http://www.freebasic.net/forum/viewtopic.php?p=190454#p190454
Update of the "New[count]" behavior analysis, because for count=0, it depends of the data-type:
result = New datatype[ count ]
"terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc"
"This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information."
I think that when count<=0 (constant value assigned or variable), the compiler must not hang and a null pointer should be always returned.
For constant negative values, fbc is getting stuck in emit_x86.bas, emitting code like the following:
From this I'm guessing:
1. There will be 4294967295 lines like this, wrapping around nearly four times in 32-bit mode.
2. There is no apparent check to make sure the allocation succeeded.
3. There is no overflow check (2^32-1 4-byte Integers obviously can't be addressed in a 32-bit memory space), and the -1*sizeof(integer) size calculation has silently overflowed to 4294967292.
astNewMEM() and the emit_x86.bas:hMemClear*() functions have checks to emit a rep stosd to clear huge amounts of memory, instead of emitting tons of mov's to initialize every dword to zero manually.
The negative size value (signed Integers used to hold it) breaks those "is it too much to initialize manually?" checks in astNewMEM() and hMemClear(). Then hMemClearBlkIMM() gets called which does a cunsg() when emitting the mov's.
I think that part definitely needs fixing, because the compiler shouldn't try to emit gigabytes of ASM code either way (seemingly getting stuck). Besides that though, it should be noted that New and Allocate functions have unsigned parameters, so negative values that are passed in will wrap around and be seen as very huge values. For -1 becoming 4 GiB, the system will probably refuse to allocate that much and return NULL.
The report about std::bad_alloc must be coming from GCC's libraries or the system; as another example, on Linux I get a simple "Aborted" message. I think that's because FB calls the exception-throwing versions of the new/delete operators from GCC's libsupc++. But since we do no exception handling in FB I'm guessing some of the CRT/GCC code ends up calling abort().
I'm thinking we could either ensure to call the non-exception-throwing versions of new/delete operators (which I believe exists, with different name mangling of course), or provide our own new/delete operators in the rtlib, or simply call malloc/free as I don't think the new/delete operators from libsupc++ do anything else anyways. Interestingly, that would remove FB's dependency on libsupc++, I think.
Last edit: dkl 2013-09-11
The "compiler hang" should now be fixed by [1c24eb].
Not sure what to do about the exception throwing, except not using C++'s new/delete operators in the first place. I'd actually like that (as mentioned, dropping the dependency on libsupc++, that would be great), but I'll have to do some research first to figure out the possible consequences...
Related
Commit: [1c24eb]
This should be solved now: [8a9501], [8bfe9a]
Related
Commit: [8a9501]
Commit: [8bfe9a]
One case of program aborting, the one for 'result = New UDT[ 0 ]' and UDT having for example a constructor, is not solved.
Example:
Must I create a new bug report?
Last edit: fxm (freebasic.net) 2018-08-16
fxm, OK, will just re-open this ticket and refer to the forum post.
Started pull request https://github.com/freebasic/fbc/pull/100
The pull request fixes some, but not all, of the remaining issues with this bug.
The last issue, not solved is the limits on NEW T[N]:
Merged pull request 100 in to fbc/master.
I added a note about overflowing the allocation at https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpNew
I think the reported bugs are solved, so am closing this report.
fixed in [f081eea86d6ae21aa2abf7cdeaa54cf56cb8b6e0]
Related
Commit: [f081ee]