Skip to main content
Graduate II
March 26, 2025
Solved

Conversion from 'int' to 'char' for use of return value from __io_getchar()

  • March 26, 2025
  • 2 replies
  • 688 views

The compiler is correctly warning about an int to char conversion for the following code from syscalls.c:

extern int __io_getchar(void) __attribute__((weak));

...

__attribute__((weak)) int _read(int file, char *ptr, int len)
{
 (void)file;
 int DataIdx;

 for (DataIdx = 0; DataIdx < len; DataIdx++)
 {
 *ptr++ = __io_getchar(); // Warning here
 }

 return len;
}

Whilst the behaviour here is as intended (assuming EOF is not expected), the warning is a pain for any projects using -Wconversion.

Could this please be fixed by adding a cast?

*ptr++ = ( char )__io_getchar();

This is for an STM32G484CEUx device, if that's important.

    This topic has been closed for replies.
    Best answer by Bob S

    You can make that change yourself.  The syscalls.c does not get re-generated every time you run CubeMX "generate code", so it won't overwrite your changes.

    Though, yes, it would be nice if syscalls.c had lots simple mods to pass more strict compiler checks, like adding UNUSED() for the function parameters that are not used in the stub functions.

    2 replies

    Bob SAnswer
    Super User
    March 26, 2025

    You can make that change yourself.  The syscalls.c does not get re-generated every time you run CubeMX "generate code", so it won't overwrite your changes.

    Though, yes, it would be nice if syscalls.c had lots simple mods to pass more strict compiler checks, like adding UNUSED() for the function parameters that are not used in the stub functions.

    CTapp.1Author
    Graduate II
    March 27, 2025

    Thanks, that will save me some "pain" - I hadn't spotted that those files aren't regenerated.

    Super User
    April 2, 2025

    How about flagging the post as "the answer" (please)