Skip to content

Commit be2463a

Browse files
committed
FIX: memory leak in list-env function
1 parent ac45c09 commit be2463a

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

src/core/n-io.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ static REBSER *Read_All_File(char *fname)
886886
blk = Make_Block(len*2);
887887

888888
str = start;
889-
while (NZ(eq = FIND_CHR(str+1, '=')) && NZ(n = (REBCNT)LEN_STR(str))) {
889+
while (NZ(n = (REBCNT)LEN_STR(str)) && NZ(eq = FIND_CHR(str+1, '='))) {
890890
Set_Series(REB_STRING, Append_Value(blk), Copy_OS_Str(str, eq-str));
891891
Set_Series(REB_STRING, Append_Value(blk), Copy_OS_Str(eq+1, n-(eq-str)-1));
892892
str += n + 1; // next
@@ -1169,8 +1169,10 @@ static REBSER *Read_All_File(char *fname)
11691169
{
11701170
REBCHR *result = OS_LIST_ENV();
11711171

1172-
Set_Series(REB_MAP, D_RET, String_List_To_Block(result));
1172+
if(result == NULL) Trap0(RE_NO_MEMORY);
11731173

1174+
Set_Series(REB_MAP, D_RET, String_List_To_Block(result));
1175+
FREE_MEM(result);
11741176
return R_RET;
11751177
}
11761178

src/os/posix/host-lib.c

+3
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"
555555
**
556556
*/ REBCHR *OS_List_Env(void)
557557
/*
558+
** Returns NULL on error.
559+
**
558560
***********************************************************************/
559561
{
560562
extern char **environ;
@@ -565,6 +567,7 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"
565567
for (n = 0; environ[n]; n++) len += 1 + LEN_STR(environ[n]);
566568

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

570573
// combine all strings into one:

src/os/win32/host-lib.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ static void *Task_Ready;
546546
**
547547
*/ REBCHR *OS_List_Env(void)
548548
/*
549+
** Returns NULL on error.
550+
**
549551
***********************************************************************/
550552
{
551553
REBCHR *env = GetEnvironmentStrings();
@@ -559,7 +561,9 @@ static void *Task_Ready;
559561
}
560562
len++;
561563

562-
str = OS_Make(len * sizeof(REBCHR));
564+
str = OS_Make(len * sizeof(REBCHR)); // Must be released by caller!
565+
if(!str) return NULL;
566+
563567
MOVE_MEM(str, env, len * sizeof(REBCHR));
564568

565569
FreeEnvironmentStrings(env);
@@ -1226,7 +1230,7 @@ static void *Task_Ready;
12261230
}
12271231

12281232
output_error:
1229-
if (input_type == FILE_TYPE) {
1233+
if (input_type == FILE_TYPE && si.hStdInput != NULL) {
12301234
CloseHandle(si.hStdInput);
12311235
}
12321236

0 commit comments

Comments
 (0)