From: Hao Wu Date: Tue, 15 Nov 2016 01:59:37 +0000 (+0800) Subject: MdePkg/BaseLib: Refine (Ascii)StrnLenS functions logic X-Git-Tag: edk2-stable201903~4889 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=c07c517cc51a4f947022aa5eebe2aace326137e5 MdePkg/BaseLib: Refine (Ascii)StrnLenS functions logic This commit refines the logic for AsciiStrnLenS and StrnLenS. It makes the logic more straightforward to prevent possible mis-reports by static code checkers. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu Reviewed-by: Liming Gao --- diff --git a/MdePkg/Library/BaseLib/SafeString.c b/MdePkg/Library/BaseLib/SafeString.c index ede2f4c5b4..e4c0759c70 100644 --- a/MdePkg/Library/BaseLib/SafeString.c +++ b/MdePkg/Library/BaseLib/SafeString.c @@ -143,8 +143,12 @@ StrnLenS ( // String then StrnLenS returns MaxSize. At most the first MaxSize characters of String shall // be accessed by StrnLenS. // - for (Length = 0; (Length < MaxSize) && (*String != 0); String++, Length++) { - ; + Length = 0; + while (String[Length] != 0) { + if (Length >= MaxSize - 1) { + return MaxSize; + } + Length++; } return Length; } @@ -571,8 +575,12 @@ AsciiStrnLenS ( // String then AsciiStrnLenS returns MaxSize. At most the first MaxSize characters of String shall // be accessed by AsciiStrnLenS. // - for (Length = 0; (Length < MaxSize) && (*String != 0); String++, Length++) { - ; + Length = 0; + while (String[Length] != 0) { + if (Length >= MaxSize - 1) { + return MaxSize; + } + Length++; } return Length; }