15

我正在尝试从MSVC 2017下的C11 标准中查找要包含的strerrorlen_s函数的标头。我需要它来为要使用的错误消息分配空间。代码如下:strerror_s

auto size = strerrorlen_s(errno) + 1;
char* errorReason = (char*)alloca(size);
strerror_s(errorReason, size, errno);
std::ostringstream oss;
oss << "Cannot open: " << fileName << " Reason: " << errorReason;
throw std::runtime_error(oss.str());

在文档中有以下文字:

与所有边界检查函数一样,strerror_s 和 strerrorlen_s 只有在由实现定义并且用户在 include之前__STDC_LIB_EXT1__定义__STDC_WANT_LIB_EXT1__为整数常量时才保证可用。1string.h

MSVC 2017没有定义__STDC_LIB_EXT1__,似乎__STDC_WANT_LIB_EXT1__在包含之前定义string.h没有效果。虽然strerror_s可用。

  • strerrorlen_s可以在带有MSVC 2017的Windows下使用吗?
  • 如果该功能不可用,是否有可能通过其他方式获取错误消息长度?
  • Windowsstrerror_s下的线程安全吗,因为在Linux下似乎不是,如果需要线程安全,必须使用strerror_r ,但它在Windows上不可用?
4

2 回答 2

13

Microsoft Visual Studio 在用作 C 编译器时,大多遵循 1990 版本的 C 标准。最近有人尝试将其更新为 1999 年版本的语言。他们仍然远远落后 - 编译器与 2011 版本相去甚远。如果您需要符合标准的 C 编译器,则不能使用 VS。

此外,您似乎在 C++ 模式下使用编译器,这并不完全有助于符合 C 标准...... C11 和 C++11 并不总是兼容。

话虽如此,您要求的功能是可选边界检查接口的一部分,我相信很少有编译器(如果有的话)已经实现。边界检查接口中存在的一些功能在 VS 之前的 C11 中作为非标准扩展存在。它们不一定符合标准。

不能保证库函数是可重入的。它们可能是线程安全的,也可能不是线程安全的。

于 2017-06-08T08:42:27.777 回答
2

你可以在这里找到实现:Reini Urban 的 safeclib

// Safe C Library
// 
// Copyright (C) 2012, 2013 Cisco Systems
// Copyright (C) 2017 Reini Urban
// All rights reserved.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

size_t strerrorlen_s(errno_t errnum)
{
#ifndef ESNULLP
#define ESNULLP         ( 400 )       /* null ptr                    */
#endif

#ifndef ESLEWRNG
#define ESLEWRNG        ( 410 )       /* wrong size                */
#endif

#ifndef ESLAST
#define ESLAST ESLEWRNG
#endif

  static const int len_errmsgs_s[] = {
    sizeof "null ptr",               /* ESNULLP */
    sizeof "length is zero",         /* ESZEROL */
    sizeof "length is below min",    /* ESLEMIN */
    sizeof "length exceeds RSIZE_MAX",/* ESLEMAX */
    sizeof "overlap undefined",      /* ESOVRLP */
    sizeof "empty string",           /* ESEMPTY */
    sizeof "not enough space",       /* ESNOSPC */
    sizeof "unterminated string",    /* ESUNTERM */
    sizeof "no difference",          /* ESNODIFF */
    sizeof "not found",              /* ESNOTFND */
    sizeof "wrong size",             /* ESLEWRNG */
  };

  if (errnum >= ESNULLP && errnum <= ESLAST) 
  {
    return len_errmsgs_s[errnum - ESNULLP] - 1;
  }
  else 
  {
    const char *buf = strerror(errnum);
    return buf ? strlen(buf) : 0;
  }
}
于 2019-02-27T02:32:06.083 回答