16

I'm trying to find which header to include for strerrorlen_s function from C11 standard under MSVC 2017. I need it for allocating space for error message which to get with strerror_s. The code is the following:

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());

In the documentation are the following words:

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 does not define __STDC_LIB_EXT1__ and it seems that defining __STDC_WANT_LIB_EXT1__ before including string.h doesn't have effect. Although strerror_s is available.

  • Is strerrorlen_s available under Windows with MSVC 2017?
  • Is it possible some other way to get error message length if the function is not available?
  • Is strerror_s thread safe under Windows, because it seems that under Linux it's not and strerror_r must be used if there is need for thread safety, but it is not available on Windows?
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
bobeff
  • 3,543
  • 3
  • 34
  • 62
  • Is `getline()` missing for you too ? – Badda Jun 08 '17 at 08:24
  • @Badda No `getline` is available. – bobeff Jun 08 '17 at 08:28
  • 1
    @Aconcagua The value of `RSIZE_MAX` macro is `2147483647`. Allocating such memory for error message is highly unpractical. :) – bobeff Jun 08 '17 at 12:21
  • @bobeff Truth - MinGW does not seem to provide it (at least I couldn't find it at anywhere...) and I assumed some more meaningful value. Coming from [here](https://stackoverflow.com/a/33604977/1312382), I discovered its true meaning. As mainly C++ developer (rsize_t actually not being part of the standard there), I was not quite aware of this new C11 feature. Sorry for giving bad advice... – Aconcagua Jun 08 '17 at 14:12
  • Why can't you simply do `oss << "Cannot open: " << fileName << " Reason: " << strerror(errno);`? –  Jun 09 '17 at 09:20
  • @manni66 I decided to use `strerror_s` after the warning: `warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.` I still can use `strerror` and disable the warning or use `strerror_s` and use static array with some arbitrary size and everything will still work, but I was curious whether it is possible to do this in the _most right_ way. :) – bobeff Jun 09 '17 at 12:00

2 Answers2

13

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    GCC under windows (MinGW) does not seem to support these functions either. – Aconcagua Jun 08 '17 at 08:46
  • 1
    @Lundin Do you know whether it is possible somehow to estimate the max size of the error message in `C++` after `strerrorlen_s` function is not available? – bobeff Jun 08 '17 at 12:23
  • 1
    @bobeff If you are using GCC, the runtime probably supports `%m` printf flag. You can use `snprintf(buf, size, "%m")`. –  Apr 18 '18 at 12:49
  • @bobeff The longest possible message is 94 characters, so you can use an array with a fixed length of 95 (to account for null termination) – Ryan Jan 09 '19 at 16:38
  • @Ryan Do you have any references that would backup your statement? – Alfred Myers Apr 02 '21 at 13:05
  • strerrorlen_s is still not implemented in Visual Studio 2022, see https://github.com/MicrosoftDocs/cpp-docs/blob/main/docs/overview/visual-cpp-language-conformance.md#note_O – Christian Troester Mar 10 '23 at 12:43
2

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;
  }
}
sailfish009
  • 2,561
  • 1
  • 24
  • 31