编程与软件工程github.com
Git 的禁用 C 函数清单
Git's list of banned C functions
2019 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 Git 源码中的 banned.h 文件,列出了 Git 项目禁止使用的 C 标准库函数(如 strcpy、sprintf、gmtime 等),并通过宏在编译期直接报错拦截。这些函数并非本身有错,而是极易被误用,属于常见的漏洞来源。
为什么经久不衰:它展示了大型 C 项目如何用工程手段(编译期禁用危险 API)系统性防范安全漏洞,这一思路至今仍是 C 代码安全实践的典范。
HN 评论精选
外来者惊讶于 C 中连基础函数都是安全陷阱
作为一个只写过十行 C 代码、来自其他语言的人,让我震惊的是,C 里那些巨大的坑居然是像“格式化字符串”或“获取 GMT 时间”这样看似人畜无害的函数。这……真的很吓人。
paultopia查看英文原文
Its really wild, as a person coming from other languages who has written maybe ten lines of C in his life that the functions that seem to be massive footguns in C are, like, "format a string" or "get time in GMT." That's... really scary.
禁用函数是权衡后的工程决策,类似规则在嵌入式领域更严格
这些函数本身并没有内在问题,技术上都可以安全使用,我也安全地用了几十年。问题是它们是巨大的陷阱,在大型软件中可以说“不值得冒险”。你还可以走得更远:在我参与的几个嵌入式项目中,规则包括:应用启动后禁止动态内存分配(之后任何堆分配都是编译错误)、禁止任何妨碍静态计算栈用量的结构(如递归)、禁止任何锁、每个数据结构的大小都必须以简单无疑的方式得到保证,等等。
lmilcin查看英文原文
To respond to some of the comments. It is not that there is anything intrinsically wrong with these functions. You can technically use all of them and I have been using all of them, safely, for decades. The issue is they are huge traps to the point that in a larger piece of software one can say "well, it's just not worth it". You can go much, much, much further than that. In couple embedded projects I worked some of the rules were: * dynamic allocation after application has started is banned -- any heap buffers and data structures must be allocated at the start of the application and after that any allocation is a compile time error, * any constructs that would prevent statically calculating stack usage were banned (for example any form of recursion except when exact recursion depth is ensured statically), * any locks were banned, * absolutely every data structure must have size ensured, in a simple way, beyond any reasonable doubt, etc.
希望禁用列表同时给出推荐替代方案
如果报错信息能建议合适的替代函数就更好了。我看到不该用 gmtime、localtime、ctime、ctime_r、asctime、asctime_r,但他们觉得我该用什么?
drfuchs查看英文原文
It would be nice if the error messages generated would suggest replacement functions that they deem appropriate. I see that I'm not supposed to use gmtime, localtime, ctime, ctime_r, asctime, and asctime_r; but what do they think I should use?
Git 维护者解释:列表按实际误用情况增量更新,不求穷尽
很多评论在问“为什么不禁用 XYZ”。答案是:我们并不追求穷尽所有危险函数。这个列表的目的是在代码评审之前就拦住常见错误,所以基本是当有人试图误用某个函数时才把它加进来。我不记得有人在 Git 代码库里滥用过 longjmp()(当然,这不是挑战)。
peff查看英文原文
I see a lot of comments to the effect of "shouldn't XYZ also be banned". The answer is that we're not necessarily trying to be exhaustive. The point is to flag common errors before we even hit review, so we add new functions mostly when somebody tries to misuse them. I don't recall anybody trying to abuse longjmp() in Git's codebase yet (and no, that's not a challenge).
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选