automatically ban strcpy()

There are a few standard C functions (like strcpy) which are
easy to misuse. E.g.:

  char path[PATH_MAX];
  strcpy(path, arg);

may overflow the "path" buffer. Sometimes there's an earlier
constraint on the size of "arg", but even in such a case
it's hard to verify that the code is correct. If the size
really is unbounded, you're better off using a dynamic
helper like strbuf:

  struct strbuf path = STRBUF_INIT;
  strbuf_addstr(path, arg);

or if it really is bounded, then use xsnprintf to show your
expectation (and get a run-time assertion):

  char path[PATH_MAX];
  xsnprintf(path, sizeof(path), "%s", arg);

which makes further auditing easier.

We'd usually catch undesirable code like this in a review,
but there's no automated enforcement. Adding that
enforcement can help us be more consistent and save effort
(and a round-trip) during review.

This patch teaches the compiler to report an error when it
sees strcpy (and will become a model for banning a few other
functions). This has a few advantages over a separate
linting tool:

  1. We know it's run as part of a build cycle, so it's
     hard to ignore. Whereas an external linter is an extra
     step the developer needs to remember to do.

  2. Likewise, it's basically free since the compiler is
     parsing the code anyway.

  3. We know it's robust against false positives (unlike a
     grep-based linter).

The two big disadvantages are:

  1. We'll only check code that is actually compiled, so it
     may miss code that isn't triggered on your particular
     system. But since presumably people don't add new code
     without compiling it (and if they do, the banned
     function list is the least of their worries), we really
     only care about failing to clean up old code when
     adding new functions to the list. And that's easy
     enough to address with a manual audit when adding a new
     function (which is what I did for the functions here).

  2. If this ends up generating false positives, it's going
     to be harder to disable (as opposed to a separate
     linter, which may have mechanisms for overriding a
     particular case).

     But the intent is to only ban functions which are
     obviously bad, and for which we accept using an
     alternative even when this particular use isn't buggy
     (e.g., the xsnprintf alternative above).

The implementation here is simple: we'll define a macro for
the banned function which replaces it with a reference to a
descriptively named but undeclared identifier.  Replacing it
with any invalid code would work (since we just want to
break compilation).  But ideally we'd meet these goals:

 - it should be portable; ideally this would trigger
   everywhere, and does not need to be part of a DEVELOPER=1
   setup (because unlike warnings which may depend on the
   compiler or system, this is a clear indicator of
   something wrong in the code).

 - it should generate a readable error that gives the
   developer a clue what happened

 - it should avoid generating too much other cruft that
   makes it hard to see the actual error

 - it should mention the original callsite in the error

The output with this patch looks like this (using gcc 7, on
a checkout with 022d2ac1f3 reverted, which removed the final
strcpy from blame.c):

      CC builtin/blame.o
  In file included from ./git-compat-util.h:1246,
                   from ./cache.h:4,
                   from builtin/blame.c:8:
  builtin/blame.c: In function ‘cmd_blame’:
  ./banned.h:11:22: error: ‘sorry_strcpy_is_a_banned_function’ undeclared (first use in this function)
   #define BANNED(func) sorry_##func##_is_a_banned_function
                        ^~~~~~
  ./banned.h:14:21: note: in expansion of macro ‘BANNED’
   #define strcpy(x,y) BANNED(strcpy)
                       ^~~~~~
  builtin/blame.c:1074:4: note: in expansion of macro ‘strcpy’
      strcpy(repeated_meta_color, GIT_COLOR_CYAN);
      ^~~~~~
  ./banned.h:11:22: note: each undeclared identifier is reported only once for each function it appears in
   #define BANNED(func) sorry_##func##_is_a_banned_function
                        ^~~~~~
  ./banned.h:14:21: note: in expansion of macro ‘BANNED’
   #define strcpy(x,y) BANNED(strcpy)
                       ^~~~~~
  builtin/blame.c:1074:4: note: in expansion of macro ‘strcpy’
      strcpy(repeated_meta_color, GIT_COLOR_CYAN);
      ^~~~~~

This prominently shows the phrase "strcpy is a banned
function", along with the original callsite in blame.c and
the location of the ban code in banned.h. Which should be
enough to get even a developer seeing this for the first
time pointed in the right direction.

This doesn't match our ideals perfectly, but it's a pretty
good balance. A few alternatives I tried:

  1. Instead of using an undeclared variable, using an
     undeclared function. This shortens the message, because
     the "each undeclared identifier" message is not needed
     (and as you can see above, it triggers a separate
     mention of each of the expansion points).

     But it doesn't actually stop compilation unless you use
     -Werror=implicit-function-declaration in your CFLAGS.
     This is the case for DEVELOPER=1, but not for a default
     build (on the other hand, we'd eventually produce a
     link error pointing to the correct source line with the
     descriptive name).

  2. The linux kernel uses a similar mechanism in its
     BUILD_BUG_ON_MSG(), where they actually declare the
     function but do so with gcc's error attribute. But
     that's not portable to other compilers (and it also
     runs afoul of our error() macro).

     We could make a gcc-specific technique and fallback on
     other compilers, but it's probably not worth the
     complexity. It also isn't significantly shorter than
     the error message shown above.

  3. We could drop the BANNED() macro, which would shorten
     the number of lines in the error. But curiously,
     removing it (and just expanding strcpy directly to the
     bogus identifier) causes gcc _not_ to report the
     original line of code.

So this strategy seems to be an acceptable mix of
information, portability, simplicity, and robustness,
without _too_ much extra clutter. I also tested it with
clang, and it looks as good (actually, slightly less
cluttered than with gcc).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2 files changed
tree: 39d67aa65cd39c4cc3ddea2fb6c9cb89328a438d
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. perl/
  14. po/
  15. ppc/
  16. refs/
  17. sha1dc/
  18. t/
  19. templates/
  20. vcs-svn/
  21. xdiff/
  22. .clang-format
  23. .gitattributes
  24. .gitignore
  25. .gitmodules
  26. .mailmap
  27. .travis.yml
  28. .tsan-suppressions
  29. abspath.c
  30. aclocal.m4
  31. advice.c
  32. advice.h
  33. alias.c
  34. alias.h
  35. alloc.c
  36. alloc.h
  37. apply.c
  38. apply.h
  39. archive-tar.c
  40. archive-zip.c
  41. archive.c
  42. archive.h
  43. argv-array.c
  44. argv-array.h
  45. attr.c
  46. attr.h
  47. banned.h
  48. base85.c
  49. bisect.c
  50. bisect.h
  51. blame.c
  52. blame.h
  53. blob.c
  54. blob.h
  55. branch.c
  56. branch.h
  57. builtin.h
  58. bulk-checkin.c
  59. bulk-checkin.h
  60. bundle.c
  61. bundle.h
  62. cache-tree.c
  63. cache-tree.h
  64. cache.h
  65. chdir-notify.c
  66. chdir-notify.h
  67. check-builtins.sh
  68. check-racy.c
  69. check_bindir
  70. checkout.c
  71. checkout.h
  72. color.c
  73. color.h
  74. column.c
  75. column.h
  76. combine-diff.c
  77. command-list.txt
  78. commit-graph.c
  79. commit-graph.h
  80. commit-slab-decl.h
  81. commit-slab-impl.h
  82. commit-slab.h
  83. commit.c
  84. commit.h
  85. common-main.c
  86. config.c
  87. config.h
  88. config.mak.dev
  89. config.mak.in
  90. config.mak.uname
  91. configure.ac
  92. connect.c
  93. connect.h
  94. connected.c
  95. connected.h
  96. convert.c
  97. convert.h
  98. copy.c
  99. COPYING
  100. credential-cache--daemon.c
  101. credential-cache.c
  102. credential-store.c
  103. credential.c
  104. credential.h
  105. csum-file.c
  106. csum-file.h
  107. ctype.c
  108. daemon.c
  109. date.c
  110. decorate.c
  111. decorate.h
  112. delta.h
  113. detect-compiler
  114. diff-delta.c
  115. diff-lib.c
  116. diff-no-index.c
  117. diff.c
  118. diff.h
  119. diffcore-break.c
  120. diffcore-delta.c
  121. diffcore-order.c
  122. diffcore-pickaxe.c
  123. diffcore-rename.c
  124. diffcore.h
  125. dir-iterator.c
  126. dir-iterator.h
  127. dir.c
  128. dir.h
  129. editor.c
  130. entry.c
  131. environment.c
  132. exec-cmd.c
  133. exec-cmd.h
  134. fast-import.c
  135. fetch-object.c
  136. fetch-object.h
  137. fetch-pack.c
  138. fetch-pack.h
  139. fmt-merge-msg.h
  140. fsck.c
  141. fsck.h
  142. fsmonitor.c
  143. fsmonitor.h
  144. generate-cmdlist.sh
  145. gettext.c
  146. gettext.h
  147. git-add--interactive.perl
  148. git-archimport.perl
  149. git-bisect.sh
  150. git-compat-util.h
  151. git-cvsexportcommit.perl
  152. git-cvsimport.perl
  153. git-cvsserver.perl
  154. git-difftool--helper.sh
  155. git-filter-branch.sh
  156. git-instaweb.sh
  157. git-merge-octopus.sh
  158. git-merge-one-file.sh
  159. git-merge-resolve.sh
  160. git-mergetool--lib.sh
  161. git-mergetool.sh
  162. git-p4.py
  163. git-parse-remote.sh
  164. git-quiltimport.sh
  165. git-rebase--am.sh
  166. git-rebase--interactive.sh
  167. git-rebase--merge.sh
  168. git-rebase--preserve-merges.sh
  169. git-rebase.sh
  170. git-remote-testgit.sh
  171. git-request-pull.sh
  172. git-send-email.perl
  173. git-sh-i18n.sh
  174. git-sh-setup.sh
  175. git-stash.sh
  176. git-submodule.sh
  177. git-svn.perl
  178. GIT-VERSION-GEN
  179. git-web--browse.sh
  180. git.c
  181. git.rc
  182. gpg-interface.c
  183. gpg-interface.h
  184. graph.c
  185. graph.h
  186. grep.c
  187. grep.h
  188. hash.h
  189. hashmap.c
  190. hashmap.h
  191. help.c
  192. help.h
  193. hex.c
  194. http-backend.c
  195. http-fetch.c
  196. http-push.c
  197. http-walker.c
  198. http.c
  199. http.h
  200. ident.c
  201. imap-send.c
  202. INSTALL
  203. iterator.h
  204. khash.h
  205. kwset.c
  206. kwset.h
  207. levenshtein.c
  208. levenshtein.h
  209. LGPL-2.1
  210. line-log.c
  211. line-log.h
  212. line-range.c
  213. line-range.h
  214. list-objects-filter-options.c
  215. list-objects-filter-options.h
  216. list-objects-filter.c
  217. list-objects-filter.h
  218. list-objects.c
  219. list-objects.h
  220. list.h
  221. ll-merge.c
  222. ll-merge.h
  223. lockfile.c
  224. lockfile.h
  225. log-tree.c
  226. log-tree.h
  227. ls-refs.c
  228. ls-refs.h
  229. mailinfo.c
  230. mailinfo.h
  231. mailmap.c
  232. mailmap.h
  233. Makefile
  234. match-trees.c
  235. mem-pool.c
  236. mem-pool.h
  237. merge-blobs.c
  238. merge-blobs.h
  239. merge-recursive.c
  240. merge-recursive.h
  241. merge.c
  242. mergesort.c
  243. mergesort.h
  244. name-hash.c
  245. notes-cache.c
  246. notes-cache.h
  247. notes-merge.c
  248. notes-merge.h
  249. notes-utils.c
  250. notes-utils.h
  251. notes.c
  252. notes.h
  253. object-store.h
  254. object.c
  255. object.h
  256. oidmap.c
  257. oidmap.h
  258. oidset.c
  259. oidset.h
  260. pack-bitmap-write.c
  261. pack-bitmap.c
  262. pack-bitmap.h
  263. pack-check.c
  264. pack-objects.c
  265. pack-objects.h
  266. pack-revindex.c
  267. pack-revindex.h
  268. pack-write.c
  269. pack.h
  270. packfile.c
  271. packfile.h
  272. pager.c
  273. parse-options-cb.c
  274. parse-options.c
  275. parse-options.h
  276. patch-delta.c
  277. patch-ids.c
  278. patch-ids.h
  279. path.c
  280. path.h
  281. pathspec.c
  282. pathspec.h
  283. pkt-line.c
  284. pkt-line.h
  285. preload-index.c
  286. pretty.c
  287. pretty.h
  288. prio-queue.c
  289. prio-queue.h
  290. progress.c
  291. progress.h
  292. prompt.c
  293. prompt.h
  294. protocol.c
  295. protocol.h
  296. quote.c
  297. quote.h
  298. reachable.c
  299. reachable.h
  300. read-cache.c
  301. README.md
  302. ref-filter.c
  303. ref-filter.h
  304. reflog-walk.c
  305. reflog-walk.h
  306. refs.c
  307. refs.h
  308. refspec.c
  309. refspec.h
  310. remote-curl.c
  311. remote-testsvn.c
  312. remote.c
  313. remote.h
  314. replace-object.c
  315. replace-object.h
  316. repository.c
  317. repository.h
  318. rerere.c
  319. rerere.h
  320. resolve-undo.c
  321. resolve-undo.h
  322. revision.c
  323. revision.h
  324. run-command.c
  325. run-command.h
  326. send-pack.c
  327. send-pack.h
  328. sequencer.c
  329. sequencer.h
  330. serve.c
  331. serve.h
  332. server-info.c
  333. setup.c
  334. sh-i18n--envsubst.c
  335. sha1-array.c
  336. sha1-array.h
  337. sha1-file.c
  338. sha1-lookup.c
  339. sha1-lookup.h
  340. sha1-name.c
  341. sha1dc_git.c
  342. sha1dc_git.h
  343. shallow.c
  344. shell.c
  345. shortlog.h
  346. sideband.c
  347. sideband.h
  348. sigchain.c
  349. sigchain.h
  350. split-index.c
  351. split-index.h
  352. strbuf.c
  353. strbuf.h
  354. streaming.c
  355. streaming.h
  356. string-list.c
  357. string-list.h
  358. sub-process.c
  359. sub-process.h
  360. submodule-config.c
  361. submodule-config.h
  362. submodule.c
  363. submodule.h
  364. symlinks.c
  365. tag.c
  366. tag.h
  367. tar.h
  368. tempfile.c
  369. tempfile.h
  370. thread-utils.c
  371. thread-utils.h
  372. tmp-objdir.c
  373. tmp-objdir.h
  374. trace.c
  375. trace.h
  376. trailer.c
  377. trailer.h
  378. transport-helper.c
  379. transport-internal.h
  380. transport.c
  381. transport.h
  382. tree-diff.c
  383. tree-walk.c
  384. tree-walk.h
  385. tree.c
  386. tree.h
  387. unicode-width.h
  388. unimplemented.sh
  389. unix-socket.c
  390. unix-socket.h
  391. unpack-trees.c
  392. unpack-trees.h
  393. upload-pack.c
  394. upload-pack.h
  395. url.c
  396. url.h
  397. urlmatch.c
  398. urlmatch.h
  399. usage.c
  400. userdiff.c
  401. userdiff.h
  402. utf8.c
  403. utf8.h
  404. varint.c
  405. varint.h
  406. version.c
  407. version.h
  408. versioncmp.c
  409. walker.c
  410. walker.h
  411. wildmatch.c
  412. wildmatch.h
  413. worktree.c
  414. worktree.h
  415. wrap-for-bin.sh
  416. wrapper.c
  417. write-or-die.c
  418. ws.c
  419. wt-status.c
  420. wt-status.h
  421. xdiff-interface.c
  422. xdiff-interface.h
  423. zlib.c
README.md

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission). To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org. The mailing list archives are available at https://public-inbox.org/git/, http://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks