Skip to content

Conversation

@keith-packard
Copy link
Contributor

@keith-packard keith-packard commented Apr 27, 2022

Here's a collection of fixes to drivers, subsys, tests and samples to resolve compiler warnings and errors found when -ffreestanding is taken out of the compiler command line, leaving it able to detect problems when using standard C library functions.

I think there are a couple of real bugs found here, although most of the changes relate to buffers sized for numbers not expected to span the full range of their declared type.

This series is no longer stacked on top of the picolibc-module series so printk/cbprintf errors will not be checked until those patches are also merged.

Closes: #45157

Depends on #44096 (includes commits from #44096)

andyross
andyross previously approved these changes May 12, 2022
Copy link
Contributor

@andyross andyross left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scanned all the code I recognize well enough to review and all looks great. This +1 will be wiped out with the next rev of course, but putting it here anyway so there's a historical record.

@keith-packard keith-packard force-pushed the compiler-hosted-mode branch 3 times, most recently from b12c0c9 to ff1b506 Compare May 13, 2022 05:05
@keith-packard keith-packard force-pushed the compiler-hosted-mode branch from ff1b506 to 0183709 Compare May 29, 2022 00:21
@github-actions github-actions bot added the area: Tests Issues related to a particular existing or missing test label May 29, 2022
gcc in 'hosted' mode checks the implementation of 'exit' to make sure it
doesn't return.

Signed-off-by: Keith Packard <[email protected]>
This driver carefully saved the errno value from the failing call and
then didn't use it in the ERROR report, using the potentially invalid
current errno value (which may have been set by the close call).

Signed-off-by: Keith Packard <[email protected]>
Sensor value computation was creating a 64-bit integer value, passing
that to 'abs' and assigning that to an int32_t result and then sanity
checking the result. If the computation goes badly wrong, then the range
reduction of 64-bit to 32-bit values could generate a falsely in-range
value.

Instead, perform the computation in 64-bits, range check that value and
then assign to a 32-bit variable.

Signed-off-by: Keith Packard <[email protected]>
Not sure why this is needed for this branch, but it pretty clearly is --
there are hundreds of set-but-unused variables in the codebase.

Signed-off-by: Keith Packard <[email protected]>
idle_rate is uint8_t, sof_cnt is uint32_t. The result is uint32_t, which
is the wrong type for 'abs'. Explicitly cast idle_rate to uint32_t,
subtract sof_cnt and then explicitly cast to int32_t and then use abs,
storing the result in another int32_t which matches the return type for
abs.

This quiets clang warnings about passing unsigned values to abs.

Signed-off-by: Keith Packard <[email protected]>
POSIX main is supposed to return int, and there's no reason not to have
this example do that.

Signed-off-by: Keith Packard <[email protected]>
Subtracting with a uint64_t operand yields a uint64_t result, for which
the absolute value is not terribly interesting. Cast the operand to
int64_t.

Use llabs instead of abs as abs takes an int parameter and not an
int64_t. This appears to work even with the minimal C library.

Signed-off-by: Keith Packard <[email protected]>
When the compiler is allowed to know the semantics of malloc and free,
this test needs to be careful to not have both calls elided by not using
the result at all. Simply storing the malloc pointer in a global
variable is sufficient here.

Signed-off-by: Keith Packard <[email protected]>
When defined as 'char', the compiler notices that the memcpy targeting that
address will write more than one byte which generates a warning. Use an
array instead so that the compiler doesn't assume a specific size.

Signed-off-by: Keith Packard <[email protected]>
@keith-packard keith-packard force-pushed the compiler-hosted-mode branch from 1345886 to cd5338f Compare June 10, 2022 22:28
@keith-packard
Copy link
Contributor Author

I rebased this back to main so it can be reviewed independently of the picolibc merge.

Copy link
Contributor

@nvlsianpu nvlsianpu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to the flash driver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: API Changes to public APIs area: ARM ARM (32-bit) Architecture area: Boards area: Build System area: C Library C Standard Library area: Kconfig area: Kernel area: Modem area: Modules area: native port Host native arch port (native_sim) area: Networking area: RISCV RISCV Architecture (32-bit & 64-bit) area: Samples Samples area: Tests Issues related to a particular existing or missing test area: Xtensa Xtensa Architecture manifest manifest-hal_st manifest-hal_ti manifest-picolibc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmake: Use of -ffreestanding disables many useful optimizations and compiler warnings

7 participants