[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: realloc is allowed to fail to shrink a block
- From: Roberto Ierusalimschy <roberto@...>
- Date: Mon, 27 Nov 2017 10:59:21 -0200
> >From Lua 5.3.4 lauxlib.c, the default lua_Alloc routine used by
> luaL_newstate is:
>
> static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
> (void)ud; (void)osize; /* not used */
> if (nsize == 0) {
> free(ptr);
> return NULL;
> }
> else
> return realloc(ptr, nsize);
> }
>
> The documentation (and the assert in luaM_realloc_) specify that the
> alloc routine must not return NULL when shrinking a block. However, the
> standards for realloc() provide no such guarantee, and it can be easily
> demonstrated that common implementations (I've tested with jemalloc, but
> I'm told by others that it's also true of tcmalloc) will return NULL
> when trying to shrink a block under certain conditions.
>
> I haven't been able to make Lua actually fail (yet), but there is
> clearly a false assumption here.
I would like to see that demonstration.
About the standard, that depends on your interpretation. It clearly
does not require that explicitly. However, it says this:
ISO/IEC 9899:1999 (E) 7.20.3.4
The realloc function returns a pointer to the new object (which may
have the same value as a pointer to the old object), or a null pointer
if the new object could not be allocated.
It is hard to argue that a "new object could not be allocated" when
it is already allocated. (The function does nothing and returns the
same pointer.) Failing in those cases might not be a bug, but it sounds
a little unintelligent.
> (The fact that the assertion exists suggests that someone thought it
> was a problem - whether because of the possible recursive call into
> the garbage collector, or throwing an error somewhere not expected...)
The assertion just documents the expectations of Lua.
-- Roberto