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 D8EA681963 for ; Wed, 4 Jan 2017 18:02:21 -0800 (PST) Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP; 04 Jan 2017 18:02:21 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.33,318,1477983600"; d="scan'208";a="209784742" Received: from fmsmsx105.amr.corp.intel.com ([10.18.124.203]) by fmsmga004.fm.intel.com with ESMTP; 04 Jan 2017 18:02:21 -0800 Received: from shsmsx151.ccr.corp.intel.com (10.239.6.50) by FMSMSX105.amr.corp.intel.com (10.18.124.203) with Microsoft SMTP Server (TLS) id 14.3.248.2; Wed, 4 Jan 2017 18:02:21 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.88]) by SHSMSX151.ccr.corp.intel.com ([169.254.3.204]) with mapi id 14.03.0248.002; Thu, 5 Jan 2017 10:02:14 +0800 From: "Yao, Jiewen" To: "Wu, Hao A" , "edk2-devel@lists.01.org" CC: "Gao, Liming" , "Kinney, Michael D" Thread-Topic: [PATCH 1/4] MdePkg/BaseLib: Add safe string functions [Ascii]StrnSizeS Thread-Index: AQHSZnzyq/xqsAJObUGRIAqEBQLfeKEpIjdA Date: Thu, 5 Jan 2017 02:02:13 +0000 Message-ID: <74D8A39837DF1E4DA445A8C0B3885C503A8DB63C@shsmsx102.ccr.corp.intel.com> References: <1483528957-28340-1-git-send-email-hao.a.wu@intel.com> <1483528957-28340-2-git-send-email-hao.a.wu@intel.com> In-Reply-To: <1483528957-28340-2-git-send-email-hao.a.wu@intel.com> Accept-Language: zh-CN, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH 1/4] MdePkg/BaseLib: Add safe string functions [Ascii]StrnSizeS 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: Thu, 05 Jan 2017 02:02:22 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: jiewen.yao@intel.com > -----Original Message----- > From: Wu, Hao A > Sent: Wednesday, January 4, 2017 7:23 PM > To: edk2-devel@lists.01.org > Cc: Wu, Hao A ; Yao, Jiewen ; > Gao, Liming ; Kinney, Michael D > > Subject: [PATCH 1/4] MdePkg/BaseLib: Add safe string functions [Ascii]Str= nSizeS >=20 > Add StrnSizeS() and AsciiStrnSizeS() APIs. >=20 > These 2 safe version APIs are used to replace StrSize() and > AsciiStrSize(). Those two APIs use similar checks as [Ascii]StrnLenS(). >=20 > Cc: Jiewen Yao > Cc: Liming Gao > Cc: Michael Kinney > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Hao Wu > --- > MdePkg/Include/Library/BaseLib.h | 56 ++++++++++++++++++++++- > MdePkg/Library/BaseLib/SafeString.c | 91 > ++++++++++++++++++++++++++++++++++++- > 2 files changed, 145 insertions(+), 2 deletions(-) >=20 > diff --git a/MdePkg/Include/Library/BaseLib.h > b/MdePkg/Include/Library/BaseLib.h > index b69c703..72d1f0b 100644 > --- a/MdePkg/Include/Library/BaseLib.h > +++ b/MdePkg/Include/Library/BaseLib.h > @@ -2,7 +2,7 @@ > Provides string functions, linked list functions, math functions, > synchronization > functions, file path functions, and CPU architecture-specific function= s. >=20 > -Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
> +Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.
> Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.
> This program and the accompanying materials > are licensed and made available under the terms and conditions of the BS= D > License > @@ -208,6 +208,34 @@ StrnLenS ( > ); >=20 > /** > + Returns the size of a Null-terminated Unicode string in bytes, includi= ng the > + Null terminator. > + > + This function returns the size of the Null-terminated Unicode string > + specified by String in bytes, including the Null terminator. > + > + If String is not aligned on a 16-bit boundary, then ASSERT(). > + > + @param String A pointer to a Null-terminated Unicode string. > + @param MaxSize The maximum number of Destination Unicode > + char, including the Null terminator. > + > + @retval 0 If String is NULL. > + @retval (sizeof (CHAR16) * (MaxSize + 1)) > + If there is no Null terminator in the first MaxSize charact= ers of > + String. > + @return The size of the Null-terminated Unicode string in bytes, inclu= ding > + the Null terminator. > + > +**/ > +UINTN > +EFIAPI > +StrnSizeS ( > + IN CONST CHAR16 *String, > + IN UINTN MaxSize > + ); > + > +/** > Copies the string pointed to by Source (including the terminating null= char) > to the array pointed to by Destination. >=20 > @@ -383,6 +411,32 @@ AsciiStrnLenS ( > ); >=20 > /** > + Returns the size of a Null-terminated Ascii string in bytes, including= the > + Null terminator. > + > + This function returns the size of the Null-terminated Ascii string spe= cified > + by String in bytes, including the Null terminator. > + > + @param String A pointer to a Null-terminated Ascii string. > + @param MaxSize The maximum number of Destination Ascii > + char, including the Null terminator. > + > + @retval 0 If String is NULL. > + @retval (sizeof (CHAR8) * (MaxSize + 1)) > + If there is no Null terminator in the first MaxSize charact= ers of > + String. > + @return The size of the Null-terminated Ascii string in bytes, includi= ng the > + Null terminator. > + > +**/ > +UINTN > +EFIAPI > +AsciiStrnSizeS ( > + IN CONST CHAR8 *String, > + IN UINTN MaxSize > + ); > + > +/** > Copies the string pointed to by Source (including the terminating null= char) > to the array pointed to by Destination. >=20 > diff --git a/MdePkg/Library/BaseLib/SafeString.c > b/MdePkg/Library/BaseLib/SafeString.c > index e4c0759..f80db9f 100644 > --- a/MdePkg/Library/BaseLib/SafeString.c > +++ b/MdePkg/Library/BaseLib/SafeString.c > @@ -1,7 +1,7 @@ > /** @file > Safe String functions. >=20 > - Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.
> + Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.
> This program and the accompanying materials > are licensed and made available under the terms and conditions of the = BSD > License > which accompanies this distribution. The full text of the license may= be > found at > @@ -154,6 +154,51 @@ StrnLenS ( > } >=20 > /** > + Returns the size of a Null-terminated Unicode string in bytes, includi= ng the > + Null terminator. > + > + This function returns the size of the Null-terminated Unicode string > + specified by String in bytes, including the Null terminator. > + > + If String is not aligned on a 16-bit boundary, then ASSERT(). > + > + @param String A pointer to a Null-terminated Unicode string. > + @param MaxSize The maximum number of Destination Unicode > + char, including the Null terminator. > + > + @retval 0 If String is NULL. > + @retval (sizeof (CHAR16) * (MaxSize + 1)) > + If there is no Null terminator in the first MaxSize charact= ers of > + String. > + @return The size of the Null-terminated Unicode string in bytes, inclu= ding > + the Null terminator. > + > +**/ > +UINTN > +EFIAPI > +StrnSizeS ( > + IN CONST CHAR16 *String, > + IN UINTN MaxSize > + ) > +{ > + // > + // If String is a null pointer, then the StrnSizeS function returns ze= ro. > + // > + if (String =3D=3D NULL) { > + return 0; > + } > + > + // > + // Otherwise, the StrnSizeS function returns the size of the Null-term= inated > + // Unicode string in bytes, including the Null terminator. If there is= no > + // Null terminator in the first MaxSize characters of String, then Str= nSizeS > + // returns (sizeof (CHAR16) * (MaxSize + 1)) to keep a consistent map = with > + // the StrnLenS function. > + // > + return (StrnLenS (String, MaxSize) + 1) * sizeof (*String); > +} > + > +/** > Copies the string pointed to by Source (including the terminating null= char) > to the array pointed to by Destination. >=20 > @@ -586,6 +631,50 @@ AsciiStrnLenS ( > } >=20 > /** > + Returns the size of a Null-terminated Ascii string in bytes, including= the > + Null terminator. > + > + This function returns the size of the Null-terminated Ascii string spe= cified > + by String in bytes, including the Null terminator. > + > + @param String A pointer to a Null-terminated Ascii string. > + @param MaxSize The maximum number of Destination Ascii > + char, including the Null terminator. > + > + @retval 0 If String is NULL. > + @retval (sizeof (CHAR8) * (MaxSize + 1)) > + If there is no Null terminator in the first MaxSize charact= ers of > + String. > + @return The size of the Null-terminated Ascii string in bytes, includi= ng the > + Null terminator. > + > +**/ > +UINTN > +EFIAPI > +AsciiStrnSizeS ( > + IN CONST CHAR8 *String, > + IN UINTN MaxSize > + ) > +{ > + // > + // If String is a null pointer, then the AsciiStrnSizeS function retur= ns > + // zero. > + // > + if (String =3D=3D NULL) { > + return 0; > + } > + > + // > + // Otherwise, the AsciiStrnSizeS function returns the size of the > + // Null-terminated Ascii string in bytes, including the Null terminato= r. If > + // there is no Null terminator in the first MaxSize characters of Stri= ng, > + // then AsciiStrnSizeS returns (sizeof (CHAR8) * (MaxSize + 1)) to kee= p a > + // consistent map with the AsciiStrnLenS function. > + // > + return (AsciiStrnLenS (String, MaxSize) + 1) * sizeof (*String); > +} > + > +/** > Copies the string pointed to by Source (including the terminating null= char) > to the array pointed to by Destination. >=20 > -- > 1.9.5.msysgit.0