During a review of production firmware I noticed an oddity that should cause a compiler error but it somehow does not.
In C, a function may not be overloaded. There are exceptions to this (with _Generic), but we are not engaging with this functionality. We have accidentally included a function overload in a C source file, which the Intellisense correctly marks as an error. However, the compiler does not think it is an error. I am using NCS 2.5.1 with the ARM GNU toolchain 9-2019-q4-major. Zephyr version 3.4.99. I modified the blinky sample application to replicate the issue.
Consider this code:
void test();
void function()
{
test();
}
void test(void * arg)
{
arbitrary_code();
}
void function2()
{
test(NULL);
}
In the code, test()
is initially declared to be parameter-less and then invoked. However, further down test()
is defined as a parametrized function and invoked as such. This should cause an error in C. I tried this with a different set of build tools (IAR) and they correctly identified this as an error.
Interestingly, if you change the order of the statements, the error is caught. It seems that this compiler glitch only happens if you have a declaration with its invocation written before the function is "redefined" and used with the other signature.