Live data from Hacker News

Git's list of banned C functions

github.com

21–30 of 639 posts

Re: Git's list of banned C functions

#21

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.

Yeah, there is a culture of complacency in C probably owing to the enormous historical baggage of legacy code that has to be supported and the blurred line between stdlib and system call.

Re: Git's list of banned C functions

#23

It would be interesting to see the rationale behind these bans, and what the suggested alternatives are. Some are obvious, like `strcpy`, but I can't remember what the problem with `sprintf` or the time functions are. If you are doing something like `sprintf(buffer, "%f, %f", a, b)`, yes it is tricky to choose the size of buffer frugally, but if you replace that by `ftoa` and constructing the string by hand, you are…

This was my reaction as well. Banning strncpy just encourages haphazard manual copying.

Re: Git's list of banned C functions

#24

Ah this is a very good idea. I guess you still have to make sure that all your translation units include this header, which isn't completely foolproof. Static analysis would probably be more robust, but way more involved.

gcc has a -include option, so this can be done once in the Makefile and get the benefit everywhere (unless you’re being clever).

Re: Git's list of banned C functions

#25

Ah this is a very good idea. I guess you still have to make sure that all your translation units include this header, which isn't completely foolproof. Static analysis would probably be more robust, but way more involved.

I remember visual studio having an option to force include a file, surely something like that would exist for other toolchains

Re: Git's list of banned C functions

#26
post #19
post #13

Earlier quoted context omitted.

From the commit messages > The ctime_r() and asctime_r() functions are reentrant, but have no check that the buffer we pass in is long enough (the manpage says it "should have room for at least 26 bytes"). Since this is such an easy-to-get-wrong interface, and since we have the much safer strftime() as well as its more convenient strbuf_addftime() wrapper, let's ban both of those. ( https://github.com/git/git/commit/…

Yes, but every hapless user shouldn't have to go searching through a bunch of commit messages to find the suggested replacement. Bad UX.

The UX of using this list is not by manually searching through the list and seeing the reason behind them. You include the file together with the rest of your sources and now you get compilation errors if you try to use them. Can't think of a better UX for banned functions.

Discovering why the thing is banned you only have to do once, if you care. If you're just modifying something quickly and minor in Git, you might not even care why.

Re: Git's list of banned C functions

#27
post #19
post #13

Earlier quoted context omitted.

From the commit messages > The ctime_r() and asctime_r() functions are reentrant, but have no check that the buffer we pass in is long enough (the manpage says it "should have room for at least 26 bytes"). Since this is such an easy-to-get-wrong interface, and since we have the much safer strftime() as well as its more convenient strbuf_addftime() wrapper, let's ban both of those. ( https://github.com/git/git/commit/…

Yes, but every hapless user shouldn't have to go searching through a bunch of commit messages to find the suggested replacement. Bad UX.

It seems pretty safe to assume a developer contributing C code to git itself would know how to use git blame (or the GitHub interface for it).

Re: Git's list of banned C functions

#28
post #23

It would be interesting to see the rationale behind these bans, and what the suggested alternatives are. Some are obvious, like `strcpy`, but I can't remember what the problem with `sprintf` or the time functions are. If you are doing something like `sprintf(buffer, "%f, %f", a, b)`, yes it is tricky to choose the size of buffer frugally, but if you replace that by `ftoa` and constructing the string by hand, you are…

This was my reaction as well. Banning strncpy just encourages haphazard manual copying.

I think you're meant to use snprintf instead. It would be great to see documentation on the alternatives!

Re: Git's list of banned C functions

#29

It would be interesting to see the rationale behind these bans, and what the suggested alternatives are. Some are obvious, like `strcpy`, but I can't remember what the problem with `sprintf` or the time functions are. If you are doing something like `sprintf(buffer, "%f, %f", a, b)`, yes it is tricky to choose the size of buffer frugally, but if you replace that by `ftoa` and constructing the string by hand, you are…

snprintf will always terminate the string, and won't overflow the buffer.

Re: Git's list of banned C functions

#30
post #3

Are there some details on whats wrong with these?

All the string functions have buffer overrun vulnerabilities if not used carefully. I'm not sure about the time functions though.

Very much this. I frequently write small games in C, and the number of times I have been bitten by baffling behaviour because a string somewhere was copied into an array that was too short, are many! Apart from that, I love the simplicity of the language and the stdlib, and it's definitely my preferred hobby programming environment.

It would be good to know what the commonly-accepted alternatives are.

Post reply on HN