Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keplerproject/luafilesystem.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2017-11-27 19:03:38 +0300
committerGitHub <noreply@github.com>2017-11-27 19:03:38 +0300
commit884b52a3b29661a10e183daf4cea7a37698f404b (patch)
treee199ca1dd289d0ca36373e885cd451cefc56c83e
parent1b5073419fdde0197c6d85c60cccc9af75340487 (diff)
Fix memory leak in case of realloc failure. (#102)
Fixes #101.
-rw-r--r--src/lfs.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/lfs.c b/src/lfs.c
index c8122c2..f510ab5 100644
--- a/src/lfs.c
+++ b/src/lfs.c
@@ -186,9 +186,12 @@ static int get_dir (lua_State *L) {
size_t size = LFS_MAXPATHLEN; /* initial buffer size */
int result;
while (1) {
- path = realloc(path, size);
- if (!path) /* failed to allocate */
- return pusherror(L, "get_dir realloc() failed");
+ char* path2 = realloc(path, size);
+ if (!path2) /* failed to allocate */ {
+ result = pusherror(L, "get_dir realloc() failed");
+ break;
+ }
+ path = path2;
if (getcwd(path, size) != NULL) {
/* success, push the path to the Lua stack */
lua_pushstring(L, path);
@@ -860,9 +863,12 @@ static int push_link_target(lua_State *L) {
char *target = NULL;
int tsize, size = 256; /* size = initial buffer capacity */
while (1) {
- target = realloc(target, size);
- if (!target) /* failed to allocate */
+ char* target2 = realloc(target, size);
+ if (!target2) { /* failed to allocate */
+ free(target);
return 0;
+ }
+ target = target2;
tsize = readlink(file, target, size);
if (tsize < 0) { /* a readlink() error occurred */
free(target);