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.

95 Upvotes

238 comments sorted by

View all comments

Show parent comment

2

u/flatfinger Sep 14 '26

Worse, the Committee seems to have been and continue to be oblivious to the fact that much of C's usefulness revolved around corner cases where whose behavior would be impossible to predict without information the language had no way of conveying, but which the execution environment might convey to the programmer via means outside the language. When the Standard noted that implementations may process cases where it waives jurisdiction "in a documented manner characteristic of the environment", it failed to recognize a useful class of implementations that will process those corner cases "in a manner characteristic of the environment, which will be documented to the extent that the environment happens to document it."

Worse, the way the Standard accommodates optimizing transforms that would be useful for some kinds of program but inappropriate for others is to characterize as "undefined behavior" any corner case whose behavior might be affected thereby, rather than recognizing a category of implementations that perform such transforms, and allowing them to affect program behavior in certain ways. For example, it may be useful to specify that if a piece of code with a single statically reachable exit could not have any side effect other than preventing the execution of code following the exit, a compiler may cleanly omit the code entirely, but behavior would need to be consistent with either executing the code as written, or cleanly omitting it, but would be considered "defined" in either case.

1

u/Dangerous_Region1682 Sep 14 '26

Yep. What the compiler writer might consider cleanly omitted might be very different from that of a kernel engineer’s. Once again, having a compiler generate code that executes what you write rather than what it thinks it can optimize away is often two different kettles of fish.

1

u/flatfinger Sep 14 '26

I'm not sure how "cleanly omitted" is in any way ambiguous. Consider the following function:

unsigned test(unsigned x)
{
  unsigned i = 1;
  while((i & 0x7FFF) != x) i*=3;
  if (x < 32768) arr[x] = 0;
  return i;
}

A compiler that processes the while as written could rely upon the fact that the if will only be reached if x is less than 32768. Cleanly omitting the while would make it possible for the if to be reached with a larger value of x, thus making it necessary for the compiler to generate code for the if that accommodates that possibility. The clang compiler (and gcc when in C++ mode) will process calls to test() that ignore the return value as unconditional stores to arr[x]*.*

1

u/Dangerous_Region1682 Sep 15 '26

But in kernel space you don’t write things like that. You tend to write code that is much more allied to what you want the assembler to look like. If you are setting breakpoints and you set them in the kernel debugger you don’t want to be searching around for code to only find it’s been optimized away. It’s hard enough to trace what’s happening with multi threaded kernel code without having clever optimizations of cleanly omitted code. It’s confusing and takes time to figure out what the difference between what I wrote and what the compiler generated when I’m looking at the assembler with the kernel debugger. What if the argument to test() was a pointer to something in the data segment that was being modified by another thread which might not be the case in this artificial example but nevertheless?

1

u/flatfinger Sep 15 '26

My point is that the way clang and gcc process things creates "artificial" side effects whose consequences are totally unpredictable and which directly contradict the behavior of the code as written. Treating a program as a sequence of steps, the above function would never perform any step that would be capable of writing past element 32768 of arr, but the machine code clang and gcc would generate if a function caller ignores the return value could perform an out-of-bounds store.

1

u/Dangerous_Region1682 Sep 16 '26

Oh, I see what you are saying now…

1

u/flatfinger Sep 16 '26

A very important principle in security is "command/data separation". One of the reasons that memory safety is considered fundamental is that out-of-bounds stores that involve "data" can act as arbitrary "commands". IMHO, there should be a dialect of C that respects command/data separation and memory safety invariants. If code does something like:

extern unsigned short ushort1;
int int1 = ushort1*50000/25000;
if (int1 < 50000) actOnLowishNumber(int1);
if (int1 >= 0) actOnNonnegativeNumber(int1);

and ushort1 is 50000, it may be acceptable to either have code call actOnLowishNumber(-71798) or actOnNonnegativeNumber(100000) but the compiler should treat the first line as setting int1 to some particular value, which should then both be both used in the comparisons and passed to the functions.

It's generally not possible for kernel code to guarantee the absence of data races involving storage which is accessible to non-privileged processes. If code does something like:

unsigned test(unsigned short *p)
{
    unsigned short temp = *p;
    temp -= (temp >> 15);
    return temp;
}

it should not be possible for a data race on *p to cause the function to return 65535, but as processed by GCC-ARM when targeting the ARM Cortex-M0, the generated machine code will be equivalent to:

unsigned test(unsigned short *p)
{
    unsigned short temp1 = *p;
    short temp2 = (short)*p;
    temp += (temp2 >> 15);
    return temp;
}

which will yield 65535 if the first read yields 0 and the second yields 65535, or vice versa.