-
Notifications
You must be signed in to change notification settings - Fork 822
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
base: master
Are you sure you want to change the base?
Conversation
Tests passed ✓, Code: 17112 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
|
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); |
There was a problem hiding this comment.
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);
?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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:
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
7ccfd91
to
152d030
Compare
Tests passed ✓, Code: 17104 B (+0.0%), Stack: 1448 B (+0.0%), Structs: 812 B (+0.0%)
|
Looks good here, thanks for creating a PR! Will bring this in on the next patch release. |
Using negate on a bool causes a warning when compiling with MSVC 19.X: