Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for "unsafe use of type bool" warning when compiling with MSVC. #1065

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amubiera
Copy link

Using negate on a bool causes a warning when compiling with MSVC 19.X:

littlefs\lfs.c(2561): error C2220: the following warning is treated as an error
littlefs\lfs.c(2561): warning C4804: '-': unsafe use of type 'bool' in operation

@geky-bot
Copy link
Collaborator

Tests passed ✓, Code: 17112 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
Code Stack Structs Coverage
Default 17112 B (+0.0%) 1448 B (+0.0%) 812 B (+0.0%) Lines 2429/2591 lines (-0.0%)
Readonly 6222 B (+0.0%) 448 B (+0.0%) 812 B (+0.0%) Branches 1280/1610 branches (+0.0%)
Threadsafe 17964 B (+0.0%) 1448 B (+0.0%) 820 B (+0.0%) Benchmarks
Multiversion 17184 B (+0.0%) 1448 B (+0.0%) 816 B (+0.0%) Readed 29369693876 B (+0.0%)
Migrate 18776 B (+0.0%) 1752 B (+0.0%) 816 B (+0.0%) Proged 1482874766 B (+0.0%)
Error-asserts 17884 B (+0.0%) 1440 B (+0.0%) 812 B (+0.0%) Erased 1568888832 B (+0.0%)

lfs.c Show resolved Hide resolved
lfs.c Outdated
@@ -2558,7 +2558,7 @@ static int lfs_dir_orphaningcommit(lfs_t *lfs, lfs_mdir_t *dir,
if (err != LFS_ERR_NOENT) {
if (lfs_gstate_hasorphans(&lfs->gstate)) {
// next step, clean up orphans
err = lfs_fs_preporphans(lfs, -hasparent);
err = lfs_fs_preporphans(lfs, hasparent ? -1 : 0);
Copy link
Member

Choose a reason for hiding this comment

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

Would it make more sense to change this to:

err = lfs_fs_preporphans(lfs, -(int8_t)hasparent);

?

Copy link
Author

@amubiera amubiera Feb 4, 2025

Choose a reason for hiding this comment

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

That works as well. Was just worried that on some compiler, a true bool could be all 0xFF and -0xFF would not turn out to be -1.

However, the C standard states that a true bool converts to 1:

4 Standard conversions
An rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one.

I also checked Godbolt compiler explorer and both -(int8_t)hasparent and hasparent ? -1 : 0 produce the same assembly in an optimized build. But slower in debug builds.

I pushed a new version with -(int8_t)hasparent)

Cheers!

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for this!

I was curious, for some reason the ternary operator cost a bit more. You would think GCC would be smart enough to emit the same code for these:

-hasparent              17104 B
hasparent ? -1 : 0      17112 B (+0.0%)
-(int8_t)hasparent      17104 B (+0.0%)

Not a deal-breaker, but very curious...

That works as well. Was just worried that on some compiler, a true bool could be all 0xFF and -0xFF would not turn out to be -1.

This was a real concern in the wild-west before stdbool.h. Fortunately, littlefs only supports up to C99, which standardized stdbool.h and the _Bool type.

This has been enough of an issue though that we ended up adding an explicit assert:

littlefs/lfs.c

Lines 4209 to 4213 in 0494ce7

// check that bool is a truthy-preserving type
//
// note the most common reason for this failure is a before-c99 compiler,
// which littlefs currently does not support
LFS_ASSERT((bool)0x80000000);

Related discussion: #772

@amubiera amubiera force-pushed the fix-unsafe-use-of-bool branch from 7ccfd91 to 152d030 Compare February 3, 2025 23:59
@geky-bot
Copy link
Collaborator

geky-bot commented Feb 4, 2025

Tests passed ✓, Code: 17104 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
Code Stack Structs Coverage
Default 17104 B (+0.0%) 1448 B (+0.0%) 812 B (+0.0%) Lines 2429/2591 lines (-0.0%)
Readonly 6222 B (+0.0%) 448 B (+0.0%) 812 B (+0.0%) Branches 1278/1608 branches (+0.0%)
Threadsafe 17956 B (+0.0%) 1448 B (+0.0%) 820 B (+0.0%) Benchmarks
Multiversion 17176 B (+0.0%) 1448 B (+0.0%) 816 B (+0.0%) Readed 29369693876 B (+0.0%)
Migrate 18768 B (+0.0%) 1752 B (+0.0%) 816 B (+0.0%) Proged 1482874766 B (+0.0%)
Error-asserts 17884 B (+0.0%) 1440 B (+0.0%) 812 B (+0.0%) Erased 1568888832 B (+0.0%)

@geky
Copy link
Member

geky commented Feb 4, 2025

Looks good here, thanks for creating a PR!

Will bring this in on the next patch release.

@geky geky added the next patch label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants