ISO C Safe Array Functions

In 2011 ISO substantially rewrote the C language. I didn't hear about this so I found out about it the hard way - my program crashed when I called a C function that copied from a location, to a location in the same buffer. It turns out that the ISO C adopted in 2012 was designed to be safer and the run time is supposed to crash when you do this. This is to make it harder to create an array overflow. According to the ISO C documentation, in the new language spec, there are safe versions of dangerous functions like strcpy(). These have the same function name with a "_s"suffix. For example you can use strcpy_s() instead of the old strcpy(). According to the ISO standard you can use these if you #define __STDC_WANT_LIB_EXT1__ as something other than 0 (the examples define it as 1) before you #include the <library>. In my projects this does nothing - the new safe "_s" functions aren't found. I can't find __STDC_WANT_LIB_EXT1__ any where in my projects, for example in the headers. Is there some framework I need to add?


I can't find any documentation for this new ISO standard C here at developer support - not in developer documentation or in these forums.


How can I implement the new ISO C?

Accepted Reply

I upgraded to XCode 9. When you change the C Language Dialect the Quick Help pane lists:


C11: Accept ISO C11 (2011), but not GNU extensions. [-std=c11]


GNU11: Accept ISO C11 and GNU extensions. [-std=gnu11]


Even if explicitly #define __STDC_WANT_LIB_EXT1__ 201112L These settings won't define the ISO C11 safe copying functions. It also tells me to "Please see the full GCC manual for the full definition of all these settings on the C dialect: <http://developer.apple.com/documentation/DeveloperTools/gcc-4.2.1/gcc/C-Dialect-Options.html>". Going to the listed documentation results in "Sorry, that page cannot be found.


Spotlight only finds one occurrance of "strcpy_s" on my machine (the one in my test source code), so I guess the runtime is ISO compliant but XCode is not.

Replies

If I #define __STDC_WANT_LIB_EXT1__ without a number, my test project won't compile. There are two errors:


in stdint.h:


#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1

#define RSIZE_MAX (SIZE_MAX >> 1)

#endif


in string.h:


#if defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1

#include <sys/_types/_rsize_t.h>

#include <sys/_types/_errno_t.h>


So it appears that the safe ISO C methods are supported but #defining __STDC_WANT_LIB_EXT1__ 1 won't make the definitions of these functions available to the compiler.

Have you tried changing the C Language Dialect in build settings to c11? Doing so might take care of the #defines internally ...

Maybe this is the correct answer to your question:


stackoverflow.com/questions/16700541/is-support-of-annex-k-in-c11-required-for-a-conforming-implementation


Note in particular that this doesn't envisage that you will #define __STDC_WANT_LIB_EXT1__ in your code, but that the compiler will define it to indicate that it provides the safe functions.


Note also that clang provides a "safe" version of strcpy called strlcpy.

It doesn't help.

This is interesting but not helpful. Defining

__STDC_LIB_EXT1__
as
201112L or 1
doesn't make the safe C functions in Annex K available to the Compiler. I know these functions are in Annex K but I need to write some code to use these. One would expect to find some information about this in the developer documentation but I can't find any. It appears that the OS X runtime correctly defines a copy to/from the same buffer as undefined but XCode doesn't provide access to the safe functions. A spotlight search doesn't come up with any hits for strcpy_s except in my test environment . You would expect to find it if it's in any header file. It could be in a library but there's no way to figure out where.

I had to read (the Stack Overflow post) a few times, but this is how I understood it:


"Annex K of C11 describes new "safe" library functions. It also requires a conforming implementation to provide these new functions if it also pre-defines the __STDC_LIB_EXT1__ macro as … whatever. If it does not define the macro, then it's still conforming, without providing the functions."


This was said in no single response, but I synthesized it from various rational-sounding responses. If I am correct, the important issue in the spec is what it says about the compiler's definition of __STDC_LIB_EXT1__ (if any), not what it says about the functions. Also, by the same interpretation, the Apple implementation is conforming, since it does not provide that macro definition (as you've discovered).


I also found a different Stack Overflow post that stated that the GNU C compiler implementors refused to adopt the safe functions (in the "strcpy_s" form — it already had the strlcpy form), and it's certainly possible that Apple felt obliged to follow the GNU lead, in order to preserve source code compatibility with GNU C, which was likely still in common use by Apple developers around the time C11 was finalized.

I upgraded to XCode 9. When you change the C Language Dialect the Quick Help pane lists:


C11: Accept ISO C11 (2011), but not GNU extensions. [-std=c11]


GNU11: Accept ISO C11 and GNU extensions. [-std=gnu11]


Even if explicitly #define __STDC_WANT_LIB_EXT1__ 201112L These settings won't define the ISO C11 safe copying functions. It also tells me to "Please see the full GCC manual for the full definition of all these settings on the C dialect: <http://developer.apple.com/documentation/DeveloperTools/gcc-4.2.1/gcc/C-Dialect-Options.html>". Going to the listed documentation results in "Sorry, that page cannot be found.


Spotlight only finds one occurrance of "strcpy_s" on my machine (the one in my test source code), so I guess the runtime is ISO compliant but XCode is not.