MSVC 2017 下缺少 C11 strerrorlen_s 函数
Missing C11 strerrorlen_s function under MSVC 2017
我正在尝试从 MSVC 2017 下的 C11 standard 中找到要为 strerrorlen_s
函数包含哪个 header。我需要它来分配 space 以获得 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());
文档中有以下文字:
As with all bounds-checked functions, strerror_s and strerrorlen_s are only guaranteed to be available if __STDC_LIB_EXT1__
is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__
to the integer constant 1
before including string.h
.
MSVC 2017 没有定义 __STDC_LIB_EXT1__
并且在包含 string.h
之前定义 __STDC_WANT_LIB_EXT1__
似乎没有效果。虽然strerror_s
可用。
strerrorlen_s
在 Windows 和 MSVC 2017 下可用吗?
- 如果该功能不可用,是否可以通过其他方式获取错误消息长度?
strerror_s
在Windows下是线程安全的吗,因为好像在Linux下它不是,如果需要线程安全,则必须使用 strerror_r,但它在 Windows? 上不可用
微软Visual Studio,作为C编译器使用时,大多遵循1990版的C标准。最近已尝试将其更新为该语言的 1999 版本。他们仍然远远落后——编译器远不及 2011 版本。如果您需要符合标准的 C 编译器,则不能使用 VS。
此外,您似乎在 C++ 模式下使用编译器,这并不能完全帮助符合 C 标准...C11 和 C++11 并不总是兼容。
也就是说,您要求的函数是可选边界检查接口的一部分,我相信很少有编译器实现它。边界检查接口中存在的一些函数作为非标准扩展存在于 VS 之前的 C11 中。它们不一定符合标准。
不保证库函数是可重入的。它们可能是也可能不是线程安全的。
你可以找到实现 here : 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;
}
}
我正在尝试从 MSVC 2017 下的 C11 standard 中找到要为 strerrorlen_s
函数包含哪个 header。我需要它来分配 space 以获得 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());
文档中有以下文字:
As with all bounds-checked functions, strerror_s and strerrorlen_s are only guaranteed to be available if
__STDC_LIB_EXT1__
is defined by the implementation and if the user defines__STDC_WANT_LIB_EXT1__
to the integer constant1
before includingstring.h
.
MSVC 2017 没有定义 __STDC_LIB_EXT1__
并且在包含 string.h
之前定义 __STDC_WANT_LIB_EXT1__
似乎没有效果。虽然strerror_s
可用。
strerrorlen_s
在 Windows 和 MSVC 2017 下可用吗?- 如果该功能不可用,是否可以通过其他方式获取错误消息长度?
strerror_s
在Windows下是线程安全的吗,因为好像在Linux下它不是,如果需要线程安全,则必须使用 strerror_r,但它在 Windows? 上不可用
微软Visual Studio,作为C编译器使用时,大多遵循1990版的C标准。最近已尝试将其更新为该语言的 1999 版本。他们仍然远远落后——编译器远不及 2011 版本。如果您需要符合标准的 C 编译器,则不能使用 VS。
此外,您似乎在 C++ 模式下使用编译器,这并不能完全帮助符合 C 标准...C11 和 C++11 并不总是兼容。
也就是说,您要求的函数是可选边界检查接口的一部分,我相信很少有编译器实现它。边界检查接口中存在的一些函数作为非标准扩展存在于 VS 之前的 C11 中。它们不一定符合标准。
不保证库函数是可重入的。它们可能是也可能不是线程安全的。
你可以找到实现 here : 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;
}
}