Skip to content

libc/src/stdio/printf_core/float_dec_converter.h:505: Possible poor error checking ? #95638

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

Closed
dcb314 opened this issue Jun 15, 2024 · 2 comments · Fixed by #95841
Closed

libc/src/stdio/printf_core/float_dec_converter.h:505: Possible poor error checking ? #95638

dcb314 opened this issue Jun 15, 2024 · 2 comments · Fixed by #95841
Assignees

Comments

@dcb314
Copy link

dcb314 commented Jun 15, 2024

Static analyser cppcheck says:

libc/src/stdio/printf_core/float_dec_converter.h:505:23: style: Unsigned expression 'positive_blocks' can't be negative so it is unnecessary to test it. [unsignedPositive]

Source code is

const size_t positive_blocks = float_converter.get_positive_blocks();

if (positive_blocks >= 0) {

In a strict sense, the static analyser is correct and the test looks pointless and
so could be deleted.

A deeper analysis suggests that get_positive_blocks might return 0 on error
and so the if test is doing some poor error checking and so the it should be

if (positive_block > 0) {

Someone with more knowledge of the code would be able to offer a better opinion.

git blame says Jonas Hahnfeld last changed the code in commit 99b5474,
but git blame frequently lies with shallow copies ;-<

@llvmbot
Copy link
Member

llvmbot commented Jun 15, 2024

@llvm/issue-subscribers-libc

Author: None (dcb314)

Static analyser cppcheck says:

libc/src/stdio/printf_core/float_dec_converter.h:505:23: style: Unsigned expression 'positive_blocks' can't be negative so it is unnecessary to test it. [unsignedPositive]

Source code is

const size_t positive_blocks = float_converter.get_positive_blocks();

if (positive_blocks >= 0) {

In a strict sense, the static analyser is correct and the test looks pointless and
so could be deleted.

A deeper analysis suggests that get_positive_blocks might return 0 on error
and so the if test is doing some poor error checking and so the it should be

if (positive_block > 0) {

Someone with more knowledge of the code would be able to offer a better opinion.

git blame says Jonas Hahnfeld last changed the code in commit 99b5474,
but git blame frequently lies with shallow copies ;-<

michaelrj-google added a commit to michaelrj-google/llvm-project that referenced this issue Jun 17, 2024
Fixes llvm#95638

The check was `if(unsigned_num >= 0)` which will always be true. The
intent was to check for zero, but the `for` loop inside the `if` was
already doing that.
@michaelrj-google
Copy link
Contributor

Thanks for finding this. After inspecting the code a bit it seems that the if was effectively unused, while the for loop inside it was actually performing the correct check. I've made a patch to fix the issue: #95841

As for git blame on a shallow copy, iirc a shallow copy doesn't actually have the list of commits so it probably just returned the most recent commit.

AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this issue Jul 9, 2024
Fixes llvm#95638

The check was `if(unsigned_num >= 0)` which will always be true. The
intent was to check for zero, but the `for` loop inside the `if` was
already doing that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants