Skip to content

Commit

Permalink
FIX: memory leak in list-env function
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed Feb 9, 2021
1 parent ac45c09 commit be2463a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/core/n-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ static REBSER *Read_All_File(char *fname)
blk = Make_Block(len*2);

str = start;
while (NZ(eq = FIND_CHR(str+1, '=')) && NZ(n = (REBCNT)LEN_STR(str))) {
while (NZ(n = (REBCNT)LEN_STR(str)) && NZ(eq = FIND_CHR(str+1, '='))) {
Set_Series(REB_STRING, Append_Value(blk), Copy_OS_Str(str, eq-str));
Set_Series(REB_STRING, Append_Value(blk), Copy_OS_Str(eq+1, n-(eq-str)-1));
str += n + 1; // next
Expand Down Expand Up @@ -1169,8 +1169,10 @@ static REBSER *Read_All_File(char *fname)
{
REBCHR *result = OS_LIST_ENV();

Set_Series(REB_MAP, D_RET, String_List_To_Block(result));
if(result == NULL) Trap0(RE_NO_MEMORY);

Set_Series(REB_MAP, D_RET, String_List_To_Block(result));
FREE_MEM(result);
return R_RET;
}

Expand Down
3 changes: 3 additions & 0 deletions src/os/posix/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"
**
*/ REBCHR *OS_List_Env(void)
/*
** Returns NULL on error.
**
***********************************************************************/
{
extern char **environ;
Expand All @@ -565,6 +567,7 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"
for (n = 0; environ[n]; n++) len += 1 + LEN_STR(environ[n]);

cp = str = OS_Make(len + 1); // +terminator
if(!cp) return NULL;
*cp = 0;

// combine all strings into one:
Expand Down
8 changes: 6 additions & 2 deletions src/os/win32/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ static void *Task_Ready;
**
*/ REBCHR *OS_List_Env(void)
/*
** Returns NULL on error.
**
***********************************************************************/
{
REBCHR *env = GetEnvironmentStrings();
Expand All @@ -559,7 +561,9 @@ static void *Task_Ready;
}
len++;

str = OS_Make(len * sizeof(REBCHR));
str = OS_Make(len * sizeof(REBCHR)); // Must be released by caller!
if(!str) return NULL;

MOVE_MEM(str, env, len * sizeof(REBCHR));

FreeEnvironmentStrings(env);
Expand Down Expand Up @@ -1226,7 +1230,7 @@ static void *Task_Ready;
}

output_error:
if (input_type == FILE_TYPE) {
if (input_type == FILE_TYPE && si.hStdInput != NULL) {
CloseHandle(si.hStdInput);
}

Expand Down

0 comments on commit be2463a

Please sign in to comment.