diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2017-11-27 14:03:38 -0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-27 14:03:38 -0200 |
commit | 884b52a3b29661a10e183daf4cea7a37698f404b (patch) | |
tree | e199ca1dd289d0ca36373e885cd451cefc56c83e | |
parent | 1b5073419fdde0197c6d85c60cccc9af75340487 (diff) | |
download | luafilesystem-884b52a3b29661a10e183daf4cea7a37698f404b.tar.gz luafilesystem-884b52a3b29661a10e183daf4cea7a37698f404b.tar.bz2 luafilesystem-884b52a3b29661a10e183daf4cea7a37698f404b.zip |
Fix memory leak in case of realloc failure. (#102)
Fixes #101.
-rw-r--r-- | src/lfs.c | 16 |
1 files changed, 11 insertions, 5 deletions
@@ -186,9 +186,12 @@ static int get_dir (lua_State *L) { | |||
186 | size_t size = LFS_MAXPATHLEN; /* initial buffer size */ | 186 | size_t size = LFS_MAXPATHLEN; /* initial buffer size */ |
187 | int result; | 187 | int result; |
188 | while (1) { | 188 | while (1) { |
189 | path = realloc(path, size); | 189 | char* path2 = realloc(path, size); |
190 | if (!path) /* failed to allocate */ | 190 | if (!path2) /* failed to allocate */ { |
191 | return pusherror(L, "get_dir realloc() failed"); | 191 | result = pusherror(L, "get_dir realloc() failed"); |
192 | break; | ||
193 | } | ||
194 | path = path2; | ||
192 | if (getcwd(path, size) != NULL) { | 195 | if (getcwd(path, size) != NULL) { |
193 | /* success, push the path to the Lua stack */ | 196 | /* success, push the path to the Lua stack */ |
194 | lua_pushstring(L, path); | 197 | lua_pushstring(L, path); |
@@ -860,9 +863,12 @@ static int push_link_target(lua_State *L) { | |||
860 | char *target = NULL; | 863 | char *target = NULL; |
861 | int tsize, size = 256; /* size = initial buffer capacity */ | 864 | int tsize, size = 256; /* size = initial buffer capacity */ |
862 | while (1) { | 865 | while (1) { |
863 | target = realloc(target, size); | 866 | char* target2 = realloc(target, size); |
864 | if (!target) /* failed to allocate */ | 867 | if (!target2) { /* failed to allocate */ |
868 | free(target); | ||
865 | return 0; | 869 | return 0; |
870 | } | ||
871 | target = target2; | ||
866 | tsize = readlink(file, target, size); | 872 | tsize = readlink(file, target, size); |
867 | if (tsize < 0) { /* a readlink() error occurred */ | 873 | if (tsize < 0) { /* a readlink() error occurred */ |
868 | free(target); | 874 | free(target); |