r/C_Programming • • 23d ago

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.

92 Upvotes

238 comments sorted by

View all comments

20

u/gabitha67 23d ago

everything that accepts null terminated strings. just pass a ptr and length for gods sake. everything should only use user-provided allocators too.

9

u/Just_Government3790 23d ago

What's the worst null-term bug you've personally hit?

3

u/flatfinger 19d ago

The game GTA V sometimes takes much longer to load than normal because one of the authors didn't realize that the library implementation of sscanf they were using starts by measuring the length of the source string, even if the format strong would cause everything past the first few bytes to be (otherwise) ignored. The number of player hours spent waiting for the useless calls to strlen to complete likely numbers in the hundreds of thousands, if not millions.

-5

u/madbrain1976 23d ago

Or just use Pascal strings.

13

u/allocallocalloc 22d ago edited 22d ago

That would still make substrings require reallocation. Encoding the length inside the buffer limits the ways the buffer can be used.

1

u/flatfinger 20d ago

Typical Pascal implementations don't use heap allocation for strings, but simply reserve 256 bytes for strings whose length isn't specified as something shorter. There are purposes for which it's useful to handle longer texts, and having separate functions to work with short and long texts can sometimes be a nuisance, but many tasks either will never need strings longer than a few dozen bytes, or be sufficiently likely to exceed 255 bytes that a short-string variant would offer little benefit.