This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] [bugprone-implicit-widening-of-multiplication-result] Improved check to ignore false positives with integer literals.
AbandonedPublic

Authored by felix642 on Aug 19 2023, 10:10 AM.

Details

Summary

The following code is safe and should not trigger the warning
constexpr std::size_t k1Mb = 1024 * 1024;

Fixes #64732

Diff Detail

Event Timeline

felix642 created this revision.Aug 19 2023, 10:10 AM
felix642 requested review of this revision.Aug 19 2023, 10:10 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2023, 10:10 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
felix642 updated this revision to Diff 551771.Aug 19 2023, 10:12 AM

Fixed format

I'm not sure if this is right fix, example:

const std::size_t k1Tb = 1024 * 1024 * 1024 * 1024;

This is detected by -Winteger-overflow, but this:

const  std::size_t k1Pb =  1024U * 1024U * 1024U * 1024U * 1024U;

is not detect by anything except this check, even that it overflow to 0.
Funny thing that if we change those unsigned to signed, then its detected by -Winteger-overflow

For me we shoudn't silent those issues, if someone want they they can always put nolint or explicit cast, we could consider adding some basic calculations just to verify if there will be no overflow, but still proper way would be to provide warning that would say, hey, write this as: "1024LU * 1024LU"

In short I'm against simply ignoring literal calculations because this check is only thing that currently detect this issue: https://github.com/llvm/llvm-project/issues/64828

Hi @PiotrZSL thank you for taking the time to look at this revision.

I agree with you we should not silence a warning if no other tool can diagnose the issue. I'm guessing that -Winteger-overflow does no trigger any warning on unsigned "overflow" since behavior is well defined in the standard :

  1. This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

But on the other hand I have to agree with DenisYaroshevskiy that it is tedious to add a cast to every multiplication of integer literals on a large codebase. Especially when we know that those values do not overflow.

Maybe we should add some basic calculations when the operation is composed of integer literals, like you previously mentioned, to check if the operation actually overflows and print this warning if it does?
In that case we could also improve the fix-it and suggest to add LU or U instead of static_cast.

Yes doing some basic calculations would be probably the best, but for that we would need to implement some "calculator" or find some exist implementation some're in clang.
Even if it would support only basic operations like *

felix642 abandoned this revision.Sep 18 2023, 3:20 PM