diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-08-12 11:13:47 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2020-08-12 11:13:47 -0300 |
commit | 6d763a250090e94b7cca0b971aac54b5c468cc18 (patch) | |
tree | a63e2ed0b4430c24be68156b5472d24fa9319b2c | |
parent | b5bc89846721375fe30772eb8c5ab2786f362bf9 (diff) | |
download | lua-6d763a250090e94b7cca0b971aac54b5c468cc18.tar.gz lua-6d763a250090e94b7cca0b971aac54b5c468cc18.tar.bz2 lua-6d763a250090e94b7cca0b971aac54b5c468cc18.zip |
'realloc' can fail when shrinking a block
According to ISO C, 'realloc' can fail when shrinking a block. If that
happens, 'l_alloc' simply ignores the fail and returns the original
block.
-rw-r--r-- | lauxlib.c | 9 |
1 files changed, 7 insertions, 2 deletions
@@ -1011,8 +1011,13 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | |||
1011 | free(ptr); | 1011 | free(ptr); |
1012 | return NULL; | 1012 | return NULL; |
1013 | } | 1013 | } |
1014 | else | 1014 | else { /* cannot fail when shrinking a block */ |
1015 | return realloc(ptr, nsize); | 1015 | void *newptr = realloc(ptr, nsize); |
1016 | if (newptr == NULL && ptr != NULL && nsize <= osize) | ||
1017 | return ptr; /* keep the original block */ | ||
1018 | else /* no fail or not shrinking */ | ||
1019 | return newptr; /* use the new block */ | ||
1020 | } | ||
1016 | } | 1021 | } |
1017 | 1022 | ||
1018 | 1023 | ||