我试图在 MSVC 2017 下找到strerrorlen_s
来自C11 standard的功能的标头。我需要它来为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());
在文档中有以下字样:
与所有边界检查函数一样,只有在实现定义
__STDC_LIB_EXT1__
并且用户将__STDC_WANT_LIB_EXT1__
定义为整数常量1
时,才能保证strerror_s和strerrorlen_s可用。在包括string.h
之前。
MSVC 2017 未定义__STDC_LIB_EXT1__
,似乎在包含__STDC_WANT_LIB_EXT1__
之前定义string.h
并不起作用。虽然strerror_s
可用。
strerrorlen_s
线程安全,因为似乎在 Linux 下它不是,并且必须使用strerror_r需要线程安全,但 Windows ?答案 0 :(得分:13)
Microsoft Visual Studio在用作C编译器时,大多遵循1990版C标准。最近已经进行了一些尝试以将其更新为该语言的1999版本。他们仍远远落后于此 - 编译器远不及2011版本。如果您需要符合标准的C编译器,则无法使用VS。
此外,您似乎在C ++模式下使用编译器,这并不完全有助于C标准符合性... C11和C ++ 11并不总是兼容。
话虽如此,你要求的功能是可选的边界检查界面的一部分,我相信很少(如果有的话)编译器已经实现了。边界检查接口中存在的一些函数在VS之前的C11中作为非标准扩展存在。它们不一定符合标准。
无法保证库函数可重入。它们可能是也可能不是线程安全的。
答案 1 :(得分:1)
您可以找到实现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;
}
}