Git 源码禁止使用 C 标准库中容易被错用的函数
Git 项目的源码禁止开发者使用 C 标准库中的某些函数,原因是这些函数太容易被误用,就算使用得当也很容易出问题。因此 Git 的源码增加了一个 banned.h 的头函数,一旦你使用了这些被禁用的函数,将在编译时报错。
这些函数包括:
#ifndef BANNED_H
#define BANNED_H
/*
* This header lists functions that have been banned from our code base,
* because they're too easy to misuse (and even if used correctly,
* complicate audits). Including this header turns them into compile-time
* errors.
*/
#define BANNED(func) sorry_##func##_is_a_banned_function
#undef strcpy
#define strcpy(x,y) BANNED(strcpy)
#undef strcat
#define strcat(x,y) BANNED(strcat)
#undef strncpy
#define strncpy(x,y,n) BANNED(strncpy)
#undef strncat
#define strncat(x,y,n) BANNED(strncat)
#undef sprintf
#undef vsprintf
#ifdef HAVE_VARIADIC_MACROS
#define sprintf(...) BANNED(sprintf)
#define vsprintf(...) BANNED(vsprintf)
#else
#define sprintf(buf,fmt,arg) BANNED(sprintf)
#define vsprintf(buf,fmt,arg) BANNED(sprintf)
#endif
#endif /* BANNED_H */
banned.h 源码 https://gitee.com/mirrors/git/blob/master/banned.h
你也许感兴趣的:
- Git 3.0 即将问世:Git 用户需了解的下个重大版本信息
- 当架构师不知道如何合并分支时
- Git 采用 SHA-256、Rust、LLM 等技术动态
- 为什么在 Git Bash 中执行 git diff 命令时,Windows 系统有时会出现 10 秒的延迟
- Git Notes:Git 最酷但最不受欢迎的功能
- Git bisect:开发者工具包中被低估的调试工具
- Git 20 年,依然怪异,依然精彩
- 理解 git blame:一篇简介
- 【外评】为什么 Facebook 不使用 Git
- 【外评】Git 的故事:这次没那么有趣
你对本文的反应是: