getcharNoWait() has inconsistent return values
The function RODOS::getcharNoWait()
from bare-metal/stm32f4/hw_getchar.cpp
has return type char
with 0
indicating that no char was available. However, HAL_UART::getcharNoWait()
from bare-metal/stm32f4/hal/hal_uart.cpp
has return type int16_t
with -1
indicating that no char was available.
This is RODOS::getcharNoWait()
:
// Attention:
// does return 0 if no character is available,
// because char is unsigned on ARM
char getcharNoWait() {
int c = uart_stdout.getcharNoWait();
if(c<0) {
return 0;
} else {
return static_cast<char>(c);
}
}
I have three concerns:
- The comment "Does return 0 if no character is available" makes little sense, because contrary to it, a character is returned (specifically the
NUL
-character), even if no character is available. - The
NUL
-character typically terminates strings, however, this function just tosses them in randomly which may disrupt functions likestrlen
,strcmp
, etc. - Both functions have the same name, but vastly different return types that need to be handled completely differently. I was used to using:
namespace RODOS {
extern HAL_UART uart_stdout;
}
int16_t ret = uart_stdout.getcharNoWait();
if (ret != -1) {
char received = static_cast<char>(ret);
} else {
// something that does not rely on char
}
Now I just used the exact same code but used RODOS::getcharNoWait() instead, thinking it would work the same and I would save the namespace RODOS{}
part. Then I wondered why it behaved differently...
Additional Remark: The other ports may need review aswell. The Posix implementation behaves differently aswell.
Edited by Niels Koch