Step to UEFI (148)UnicodeValueToString() 和 AsciiValueToString() 已经退休了

最近编写一段代码,调用AsciiValueToString()一直出现错误提示。经过一番研究发现,这个函数已经“退休了”。在官方文档【参考1】有如下描述:

16. PrintLib APIs UnicodeValueToString() and AsciiValueToString() are deprecated. And their safe counterparts UnicodeValueToStringS() and AsciiValueToStringS() are added. If the macro "DISABLE_NEW_DEPRECATED_INTERFACES" is defined in platform, then
UnicodeValueToString() and AsciiValueToString() should be replaced with UnicodeValueToStringS() and AsciiValueToStringS() respectively.

简单的说,因为安全原因,所以这些函数退休了。取而代之的是后面带有S的安全函数,相比原来的函数,新的函数需要多加一个传递长度的函数,这样可以有效的对抗缓冲区溢出这样的攻击方式。

具体函数定义可以在 \MdePkg\Include\Library\BaseLib.h 看到

#ifndef DISABLE_NEW_DEPRECATED_INTERFACES

/**
  [ATTENTION] This function is deprecated for security reason.

  Convert one Null-terminated ASCII string to a Null-terminated
  Unicode string and returns the Unicode string.

  This function converts the contents of the ASCII string Source to the Unicode
  string Destination, and returns Destination.  The function terminates the
  Unicode string Destination by appending a Null-terminator character at the end.
  The caller is responsible to make sure Destination points to a buffer with size
  equal or greater than ((AsciiStrLen (Source) + 1) * sizeof (CHAR16)) in bytes.

  If Destination is NULL, then ASSERT().
  If Destination is not aligned on a 16-bit boundary, then ASSERT().
  If Source is NULL, then ASSERT().
  If Source and Destination overlap, then ASSERT().
  If PcdMaximumAsciiStringLength is not zero, and Source contains more than
  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
  then ASSERT().
  If PcdMaximumUnicodeStringLength is not zero, and Source contains more than
  PcdMaximumUnicodeStringLength ASCII characters not including the
  Null-terminator, then ASSERT().

  @param  Source        The pointer to a Null-terminated ASCII string.
  @param  Destination   The pointer to a Null-terminated Unicode string.

  @return Destination.

**/
CHAR16 *
EFIAPI
AsciiStrToUnicodeStr (
  IN      CONST CHAR8               *Source,
  OUT     CHAR16                    *Destination
  );

#endif

 

整个函数被DISABLE_NEW_DEPRECATED_INTERFACES 编译条件包裹起来。如果不想做太大的改动,直接在你项目的 DSC中查找下面这句话,注释掉即可编译通过。

[BuildOptions]
  *_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES

 

参考:
1. https://github.com/tianocore/tianocore.github.io/wiki/UDK2017-Core-Update-Notes

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注