Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cruntime/*printf*: free temporary string buffers also on failure #50469

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/coreclr/pal/src/cruntime/printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ static BOOL Internal_ScanfExtractFormatW(LPCWSTR *Fmt, LPSTR Out, int iOutSize,
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}

Expand Down Expand Up @@ -401,7 +401,7 @@ static BOOL Internal_ScanfExtractFormatW(LPCWSTR *Fmt, LPSTR Out, int iOutSize,
{
ERROR("strcpy_s failed\n");
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
goto EXIT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this set Result to FALSE before the goto?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result is initialized to FALSE: BOOL Result = FALSE;.

}

Out += strlen(scanf_longlongfmt);
Expand Down Expand Up @@ -532,6 +532,8 @@ static BOOL Internal_ScanfExtractFormatW(LPCWSTR *Fmt, LPSTR Out, int iOutSize,
*Out++ = 'n';

*Out = 0; /* end the string */

EXIT:
PAL_free(TempStr);
return Result;
}
Expand Down
27 changes: 17 additions & 10 deletions src/coreclr/pal/src/cruntime/printfcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ BOOL Internal_ExtractFormatA(CPalThread *pthrCurrent, LPCSTR *Fmt, LPSTR Out, LP
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
pthrCurrent->SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}
else if (**Fmt == '*')
Expand Down Expand Up @@ -258,7 +258,7 @@ BOOL Internal_ExtractFormatA(CPalThread *pthrCurrent, LPCSTR *Fmt, LPSTR Out, LP
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
pthrCurrent->SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}
else if (**Fmt == '*')
Expand Down Expand Up @@ -444,6 +444,8 @@ BOOL Internal_ExtractFormatA(CPalThread *pthrCurrent, LPCSTR *Fmt, LPSTR Out, LP
}

*Out = 0; /* end the string */

EXIT:
free(TempStr);
return Result;
}
Expand Down Expand Up @@ -525,7 +527,7 @@ BOOL Internal_ExtractFormatW(CPalThread *pthrCurrent, LPCWSTR *Fmt, LPSTR Out, L
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
pthrCurrent->SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}
else if (**Fmt == '*')
Expand Down Expand Up @@ -562,7 +564,7 @@ BOOL Internal_ExtractFormatW(CPalThread *pthrCurrent, LPCWSTR *Fmt, LPSTR Out, L
{
ERROR("atoi returned a negative value indicative of an overflow.\n");
pthrCurrent->SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}
else if (**Fmt == '*')
Expand Down Expand Up @@ -772,6 +774,8 @@ BOOL Internal_ExtractFormatW(CPalThread *pthrCurrent, LPCWSTR *Fmt, LPSTR Out, L
}

*Out = 0; /* end the string */

EXIT:
free(TempStr);
return Result;
}
Expand Down Expand Up @@ -899,7 +903,7 @@ static INT Internal_AddPaddingVfwprintf(CPalThread *pthrCurrent, PAL_FILE *strea
LPWSTR OutOriginal;
INT LengthInStr;
INT Length;
INT Written = 0;
INT Written = -1;

LengthInStr = PAL_wcslen(In);
Length = LengthInStr;
Expand All @@ -924,9 +928,8 @@ static INT Internal_AddPaddingVfwprintf(CPalThread *pthrCurrent, PAL_FILE *strea
if (wcscpy_s(Out, iLen, In) != SAFECRT_SUCCESS)
{
ERROR("wcscpy_s failed!\n");
free(OutOriginal);
pthrCurrent->SetLastError(ERROR_INSUFFICIENT_BUFFER);
return -1;
goto EXIT;
}
Out += LengthInStr;
iLen -= LengthInStr;
Expand Down Expand Up @@ -954,9 +957,8 @@ static INT Internal_AddPaddingVfwprintf(CPalThread *pthrCurrent, PAL_FILE *strea
if (wcscpy_s(Out, iLen, In) != SAFECRT_SUCCESS)
{
ERROR("wcscpy_s failed!\n");
free(OutOriginal);
pthrCurrent->SetLastError(ERROR_INSUFFICIENT_BUFFER);
return -1;
goto EXIT;
}

Out += LengthInStr;
Expand All @@ -971,9 +973,14 @@ static INT Internal_AddPaddingVfwprintf(CPalThread *pthrCurrent, PAL_FILE *strea
{
ERROR("fwrite() failed with errno == %d\n", errno);
}
free(OutOriginal);
}
else
{
Written = 0;
}

EXIT:
free(OutOriginal);
return Written;
}

Expand Down
6 changes: 4 additions & 2 deletions src/coreclr/pal/src/cruntime/silent_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ BOOL Silent_ExtractFormatA(LPCSTR *Fmt, LPSTR Out, LPINT Flags, LPINT Width, LPI
if (*Width < 0)
{
SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}
else if (**Fmt == '*')
Expand Down Expand Up @@ -439,7 +439,7 @@ BOOL Silent_ExtractFormatA(LPCSTR *Fmt, LPSTR Out, LPINT Flags, LPINT Width, LPI
if (*Precision < 0)
{
SetLastError(ERROR_INTERNAL_ERROR);
return Result;
goto EXIT;
}
}
else if (**Fmt == '*')
Expand Down Expand Up @@ -595,6 +595,8 @@ BOOL Silent_ExtractFormatA(LPCSTR *Fmt, LPSTR Out, LPINT Flags, LPINT Width, LPI
}

*Out = 0; /* end the string */

EXIT:
PAL_free(TempStr);
return Result;
}
Expand Down