diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2017-11-26 16:49:39 -0200 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2017-11-26 16:49:39 -0200 |
commit | f7eea6295742839bb819d1ee6b18ee0a4f64dac3 (patch) | |
tree | e199ca1dd289d0ca36373e885cd451cefc56c83e | |
parent | 1b5073419fdde0197c6d85c60cccc9af75340487 (diff) | |
download | luafilesystem-fix-101.tar.gz luafilesystem-fix-101.tar.bz2 luafilesystem-fix-101.zip |
Fix memory leak in case of realloc failure.fix-101
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); |