r/cprogramming • u/RoomNo7891 • Jan 22 '26
Why some famous open source projects rewrite some C standard function from zero?
Hello,
I was watching NGINX and libuv source code and noticed that both the projects (at different ratios) rewrite standard functions (such as string manipulation functions) or rewrite existing macro including their prefix (es.
UV__INET6_ADDRSTRLEN in inet.c).
Is it due to performance or maybe to create a common wrapper between OS?
Thanks!
54
Upvotes
13
u/[deleted] Jan 22 '26
C standard library string functions are better avoided, C strings in general are a major source of memory and code execution bugs.
It's not uncommon for applications to implement their own safer strings.
15
u/Brilliant-Orange9117 Jan 22 '26
There a multiple valid reasons. Sometimes functions are implemented on all platforms, sometimes the specification that they (hopefully) implement isn't precise enough or changes in incompatible ways e.g. the PostgreSQL project had to reimplement string comparison because operating system updates can change the Unicode collation which breaks databases. Some older projects once upon had to have write complicated compatibility code and never bothered to remove it because it still works e.g. libdjb using getpeername() to leak the error code of a non-blocking connect() because ancient HP-UX versions kernel panicked if tried to do it the obvious way by retrying the connect().
Also don make the mistake of assuming all source code you run into is a good teaching example or avoids unnecessary complexity and duplications. Some code is just crap that barely works.
3
u/spl1n3s Jan 22 '26
The standard library function provides additional checks and special cases you don't always need and can omit.
However, a lot of compilers perform compile time checks and pick a optimized version for that specific case anyways. For example if you try to outperform memcpy or memcmp you will have some really hard time because at compile time the compiler won't use a general purpose memcpy but a specific memcpy for your use case (e.g. is the memory aligned, is the data size a multiple of for example 8 byte, it the data size very small, etc.)
Some standard library functions are not thread safe. Sometimes you want to avoid the standard library setup, which "always" happens hidden when you enter into main(). (Note: main is not the true entry point of a c program).
2
u/scallywag_software Jan 22 '26
Game engines tend to do this, mostly for performance
4
u/tseli0s Jan 22 '26
Sometimes those functions were written ages ago and they kinda stuck. Sometimes it's done for portability.
2
u/Traveling-Techie Jan 23 '26
To avoid copyright issues?
1
u/EatingSolidBricks Jan 22 '26
You never know where you wanna run your database and that toaster might not have C runtime
1
u/tcmart14 Jan 23 '26
For lots of reasons. Sometimes because on one platform, a function was available, but they implemented it themselves for portability. You still see some C code that implement their own bcopy because the BSDs had it and the codebase started there (lot of MUD codebase), but Linux didn’t have it. Now you replace it with memmove or memcopy if you want or something similar, but 20 years ago there wasn’t something like bcopy that was universal. Keep in mind, the standard functions used to be way less.
43
u/[deleted] Jan 22 '26
[removed] — view removed comment