ShellPkg/UefiShellLib: Fix Buffer underflow

Having StrLen(Buffer) == 0 results in a Buffer underflow.
Also, StrLen iterates over the Buffer elements until finding a NULL
character. This results in a quadratic search for '\r' characters
in the while loop.

Fix these issues.

Signed-off-by: Pierre Gondois <pierre.gondois@arm.com>
This commit is contained in:
Pierre Gondois
2025-06-12 13:28:13 +02:00
committed by mergify[bot]
parent f242a0e87f
commit c2eb2136b4

View File

@ -4379,6 +4379,7 @@ ShellFileHandleReadLine (
{
EFI_STATUS Status;
CHAR16 CharBuffer;
UINTN BufferLength;
UINTN CharSize;
UINTN CountSoFar;
UINT64 OriginalFilePosition;
@ -4455,8 +4456,9 @@ ShellFileHandleReadLine (
return (EFI_BUFFER_TOO_SMALL);
}
while (Buffer[StrLen (Buffer)-1] == L'\r') {
Buffer[StrLen (Buffer)-1] = CHAR_NULL;
BufferLength = StrLen (Buffer);
while ((BufferLength != 0) && (Buffer[--BufferLength] == L'\r')) {
Buffer[BufferLength] = CHAR_NULL;
}
return (Status);