From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 37D6A81C5D for ; Wed, 14 Dec 2016 03:27:00 -0800 (PST) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga102.fm.intel.com with ESMTP; 14 Dec 2016 03:26:59 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,346,1477983600"; d="scan'208";a="1071935608" Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.34]) by orsmga001.jf.intel.com with ESMTP; 14 Dec 2016 03:26:58 -0800 From: Hao Wu To: edk2-devel@lists.01.org Cc: Hao Wu , Jiewen Yao , Liming Gao , Michael D Kinney Date: Wed, 14 Dec 2016 19:26:46 +0800 Message-Id: <1481714811-12568-2-git-send-email-hao.a.wu@intel.com> X-Mailer: git-send-email 1.9.5.msysgit.0 In-Reply-To: <1481714811-12568-1-git-send-email-hao.a.wu@intel.com> References: <1481714811-12568-1-git-send-email-hao.a.wu@intel.com> Subject: [PATCH 1/6] MdePkg/BaseLib: Refine (Ascii)StrnLenS functions logic X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Dec 2016 11:27:00 -0000 This commit refines the logic for AsciiStrnLenS and StrnLenS. It makes the logic more straightforward to prevent possible mis-reports by static code checkers. Cc: Jiewen Yao Cc: Liming Gao Cc: Michael D Kinney Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Hao Wu --- MdePkg/Library/BaseLib/SafeString.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/MdePkg/Library/BaseLib/SafeString.c b/MdePkg/Library/BaseLib/SafeString.c index ede2f4c..3247d28 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) { + break; + } + 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) { + break; + } + Length++; } return Length; } -- 1.9.5.msysgit.0