]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseLib: Refine (Ascii)StrnLenS functions logic
authorHao Wu <hao.a.wu@intel.com>
Tue, 15 Nov 2016 01:59:37 +0000 (09:59 +0800)
committerHao Wu <hao.a.wu@intel.com>
Thu, 22 Dec 2016 08:17:05 +0000 (16:17 +0800)
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 <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/BaseLib/SafeString.c

index ede2f4c5b433f4d0057185a35afefe35f54524b3..e4c0759c704adae6d9e329a1c25f44df8ecf9cab 100644 (file)
@@ -143,8 +143,12 @@ StrnLenS (
   // String then StrnLenS returns MaxSize. At most the first MaxSize characters of String shall\r
   // be accessed by StrnLenS.\r
   //\r
-  for (Length = 0; (Length < MaxSize) && (*String != 0); String++, Length++) {\r
-    ;\r
+  Length = 0;\r
+  while (String[Length] != 0) {\r
+    if (Length >= MaxSize - 1) {\r
+      return MaxSize;\r
+    }\r
+    Length++;\r
   }\r
   return Length;\r
 }\r
@@ -571,8 +575,12 @@ AsciiStrnLenS (
   // String then AsciiStrnLenS returns MaxSize. At most the first MaxSize characters of String shall\r
   // be accessed by AsciiStrnLenS.\r
   //\r
-  for (Length = 0; (Length < MaxSize) && (*String != 0); String++, Length++) {\r
-    ;\r
+  Length = 0;\r
+  while (String[Length] != 0) {\r
+    if (Length >= MaxSize - 1) {\r
+      return MaxSize;\r
+    }\r
+    Length++;\r
   }\r
   return Length;\r
 }\r