Missing C11 strerrorlen_s function under MSVC 2017

房东的猫 提交于 2019-12-04 23:55:21

Microsoft Visual Studio, when used as C compiler, mostly follows the 1990 version of the C standard. Some attempts have been made recently to update it to the 1999 version of the language. They are still far behind with that - the compiler is nowhere near the 2011 version. If you need a standard compliant C compiler you cannot use VS.

In addition, you seem to use the compiler in C++ mode which isn't exactly helping C standard compliance... C11 and C++11 are not always compatible.

That being said, the function you ask for is part of the optional bounds-checking interface, which I believe very few, if any, compilers have yet implemented. Some functions present in the bounds-checking interface existed in VS prior C11 as non-standard extensions. They are not necessarily standard compliant.

There are no guarantees that library functions are re-entrant. They may or may not be thread-safe.

you can find the implementation here : Reini Urban's 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;
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!