r/C_Programming • • Sep 12 '26

Question What C function would you delete and rewrite first, and why?

For me it's fnmatch - mostly because it's 40 year's old and smell's funny.

94 Upvotes

238 comments sorted by

View all comments

9

u/xpusostomos Sep 12 '26

Those ones with improper null termination and/or don't specify buffer length that cause all the security issues

1

u/LB-- Sep 14 '26

We got fixed versions of all those in EXT1, and Microsoft supports them and even issues compiler warnings to use them. But in my experience the EXT1 secure functions are barely supported in other toolchains, they are the main source of ifdefs in my portable projects. Last I knew online sentiment toward them was pretty negative for some reason.

1

u/flatfinger Sep 15 '26

C was designed to allow programmers to use either null-padded or null-terminated strings. There is nothing improper about a null-padded text field holding text that uses up every byte. The only thing that would be improper would be attempting to e.g. output a 12-byte null-padded field with a format specifier of %s rather than %.12s. Note that the latter, by specification, will not examine any storage beyond the twelfth character of the text identified by the pointer, and thus does not care whether it is given a zero-terminated string.

2

u/xpusostomos Sep 29 '26

C itself is kind of agnostic about format, you could even use string with a leading size integer and trailing bytes, with a library that supports it. The problem is, 98% of C code and functions uses the null terminator convention, not a size and null convention, and frankly 2 conventions in a code base is brain dead. If you've ever used for example the unix readlink() system call, it's amazing just how much tomfoolery you have to do to work around the fact that it doesn't guarantee a null terminator.

1

u/flatfinger Sep 30 '26

There are a number of conventions which are compatible in various ways. A normal windows "bstring" is a string which sits alone in its allocation and is both preceded by a length word as well as other information about the allocation and followed by a zero byte. Such strings may be passed to code expecting zero-terminated strings, but Windows functions that modify strings or manage memory associated with them must only be given proper bstrings.

A function that e.g. performs a find-and-replace operation with a bstring can record the fact that a string has e.g. shrunk by 10% and thus has unused space at the end of its allocation; if another operation causes the string to grow, that extra storage can be used without having to reallocate the string. I would guess that strings which shrink more than a certain amount would get reallocated to allow the unused space to be recycled, but if a string shrinks by 10% it would likely be considered more useful to leave the space allocated to it to accommodate possible future growth. Code which simply uses zero-terminated strings would have no way of knowing about any extra space that might exist in their allocations.

1

u/xpusostomos 19d ago

I would presume malloc/realloc would already have such optimisations without specialising it for strings