如何在 Mac OS 上安装包含可选字符串函数的 C11 编译器?

How to install C11 compiler on Mac OS with optional string functions included?

我正在尝试以下代码以查看是否支持 C 中的可选字符串函数(我已经安装了 Mac OS X El Capitan 和 XCode)。 ..

#include <stdio.h>

int main(void)
{
  #if defined __STDC_LIB_EXT1__
    printf("Optional functions are defined.\n");
  #else
    printf("Optional functions are not defined.\n");
  #endif

  return 0;
}

...但这表明它们不是。

我已经尝试了所有来自 XCode 的不同编译器(cc、gcc、llvm-gcc、clang)。

我也试过 brew install gcc 假设 GNU C 编译器会给我这些额外的功能,但它没有。

有没有一种方法可以在 Mac OS 上简单地安装一个 C11 兼容的编译器,这将给我这些额外的(即安全的)字符串函数。

总结: 你不会让它工作的。有更好的方法来确保您的代码正确。现在,改用地址清理器。

也称为 C11 标准或 TR 24731 的“附件 K”,这些功能广泛实施。唯一常用的实现是 Microsoft Visual Studio 的一部分,其他常见的 C 实现拒绝(明确地,甚至)附件 K 中的功能。因此,虽然附件 K 在技术上是标准的一部分,但出于实际目的,它应该是被视为 Microsoft 特定的扩展。

有关详细信息,请参阅 Field Experience With Annex K — Bounds Checking Interfaces(文档 N1967)。根据这份报告,附件 K 只有四个实现,两个用于 Windows,一个被认为“非常不完整”,其余一个“不适合生产使用,没有很大的变化”。

但是,这些字符串函数“安全”的说法有点误导。这些函数只是添加了边界检查,只有在函数被正确调用时才有效——但话又说回来,“非安全”函数只有在它们也被正确调用时才有效。从上面引用的报告中,

Despite more than a decade since the original proposal and nearly ten years since the ratification of ISO/IEC TR 24731-1:2007, and almost five years since the introduction of the Bounds checking interfaces into the C standard, no viable conforming implementations has emerged. The APIs continue to be controversial and requests for implementation continue to be rejected by implementers.

The design of the Bounds checking interfaces, though well-intentioned, suffers from far too many problems to correct. Using the APIs has been seen to lead to worse quality, less secure software than relying on established approaches or modern technologies. More effective and less intrusive approaches have become commonplace and are often preferred by users and security experts alike.

Therefore, we propose that Annex K be either removed from the next revision of the C standard, or deprecated and then removed.

我建议使用地址清理器作为替代。

不要将 strncpystrncat 等用作“安全”函数,它们并非设计用于此目的,也不是 strcpy 的直接替代品、strcat 等,与 strcpy_sstrcat_s 不同,后者 的替代品。

如果您不使用 Windows 或 Embarcadero,则需要使用外部 safeclib:https://github.com/rurban/safeclib/releases

没有其他 libc 带有安全的 C11 Annex K 扩展。 有关与此相关的各种 libc 怪癖的概述,请参阅 https://rurban.github.io/safeclib/doc/safec-3.3/d1/dae/md_doc_libc-overview.html