Live data from Hacker News

Banned C standard library functions in Git source code

github.com

111–120 of 329 posts

Re: Banned C standard library functions in Git source code

#111

musl libc (an alternative libc) provides implementations of these functions that are memory-safe. (It only works on Linux though.)

Using musl as your standard library causes other problems, namely horrible Python performance and incompatibility with Valgrind.

What's slow in particular and on what arch?

Re: Banned C standard library functions in Git source code

#112
post #90

Earlier quoted context omitted.

A C programmer reading this list knows exactly why they are there. It is not at all a controversial list. Edit: I guess some down voter doesn't believe me but it continues to be true. They are all string functions. Most of them do not take an output buffer size, so a source string exceeding the destination buffer will overflow. Others, like strncpy, take an output buffer size but will not null terminate when exceeded…

If every C programmer knew, then there would be no need to ban them

Not knowing defines you as not a C programmer. That doesn't stop many people from trying to code C anyway...

I have seen strtok used, even though it takes more code to use it correctly than not use it. strlcpy is like that, too. Of course, what happens is nobody uses them correctly.

Re: Banned C standard library functions in Git source code

#115
post #6

Earlier quoted context omitted.

I'm not an expert in C, but then what's the issue with strncpy() or any "n" functions? It prevents overflow AFAIK. Also what is the alternative (memcpy?) and why?

strlcpy() is a nice alternative, it's from OpenBSD from 1996. Was later added to the other BSDs, Solaris and macOS. But, last I checked, Linux never added them. :( https://www.sudo.ws/todd/papers/strlcpy.html

strlcpy is a terrible alternative. I have never seen it used correctly. It takes more and uglier code to use it correctly than to use strlen and strcpy. People who use it don't bother.

Finding use of strlcpy in a program is a red warning of sloppy code.

There are good reasons it was kept out of glibc for so long.

Re: Banned C standard library functions in Git source code

#116
post #85

Earlier quoted context omitted.

I don't see a huge problem with strtok as long as it isn't a multi threaded program. It is not in gets territory which is literally impossible to use correctly.

Synchronous reentry is a problem. Say some loop using strtok calls a function... how do you know that that function doesn't use strtok (or call another function that does...). Any use of non-const static variables in general has this problem, and strtok is just one example.

> Say some loop using strtok calls a function... how do you know that that function doesn't use strtok (or call another function that does...).

I’m constantly surprised that C isn’t specified in such a way that lack-of-reentrancy can be determined at compile time. You’d “just” need a symbol table, sort of like the debug symbol table, in each compiled object, holding for each visible symbol the set of function calls marked `__no_reenter` that are predominated by that symbol in control flow. (Yes, some functions do computed jumps, like with longjmp. Just implicitly label those functions __no_reenter unless the programmer explicitly labels them __reenter!)

Re: Banned C standard library functions in Git source code

#117
post #99
post #93

Earlier quoted context omitted.

Here is an implementation of coroutines on top of setjmp/longjmp: https://fanf.livejournal.com/105413.html

Looks more like an implementation of undefined behaviour to me.

Don't be an ass you 'fiber' cretin.

Re: Banned C standard library functions in Git source code

#119

Earlier quoted context omitted.

Nice catch! You can send a "PR", but note that they have their own custom "PR" procedure, not the standard "PR" procedure in GitHub. > Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow https://github.com/git/git/blob/master/Documentation/Submitt... procedure for any of your improvements.

Aside: GitHub’s refusal to provide a feature to close pull requests for mirrors is utterly ridiculous since it requires that people (or bots) go around and disable pull requests and point people to the patch submission process.

What's weirdest to me is that it differs from the policy on Issues, which can be turned off in settings.

I sort of get not allowing it, except that it's inconsistent.

Re: Banned C standard library functions in Git source code

#120

For anybody else: Why is strncpy insecure? https://stackoverflow.com/questions/869883/why-is-strncpy-in... > strncpy() doesn't require NUL termination, and is therefore susceptible to a variety of exploits.

Yes. Also as mentioned in the post, strlcpy (when it's available) is safer.

But still not safe, thanks to the return value. Imagine the case where you are using strlcpy because you can't be sure if the source buffer is properly null terminated.
Post reply on HN