[PATCH] D93822: [clang][Sema] Add diagnostics for implicit widening of multiplication result

Roman Lebedev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 26 05:49:14 PST 2020


lebedev.ri created this revision.
lebedev.ri added reviewers: rsmith, aaron.ballman, rjmccall, erichkeane, dblaikie.
lebedev.ri added a project: LLVM.
lebedev.ri requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Overflows are never fun.
In most cases (in most of the code), they are rare,
because usually you e.g. don't have as many elements.

However, it's exceptionally easy to fall into this pitfail
in code that deals with images, because, assuming 4-channel 32-bit FP data,
you need *just* ~269 megapixel image to case an overflow
when computing at least the total byte count.

In darktable <https://github.com/darktable-org/darktable>, there is a *long*, painful history of dealing with such bugs:

- https://github.com/darktable-org/darktable/pull/7419
- https://github.com/darktable-org/darktable/commit/eea1989f2c9fa76710db07baaec4c19c1e40e81c
- https://github.com/darktable-org/darktable/commit/70626dd95bf0fab36f2d011dab075e3ebbf7aa28
- https://github.com/darktable-org/darktable/pull/670
- https://github.com/darktable-org/darktable/commit/38c69fb1b2bc90057c569242cb9945a10be0b583

and yet they clearly keep resurfacing still.

It would be immensely helpful to have a diagnostic for those patterns,
which is what this change proposes.

Currently, i only diagnose the most obvious case, where multiplication
is directly widened with no other expressions inbetween,
(i.e. `long r = (int)a * (int)b` but not even e.g. `long r = ((int)a * (int)b)`)
however that might be worth relaxing later.

Right now i've added the diagnostic into `-Wall`.
I have not yet looked at clang stage-2 story.

Thoughts?


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93822

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/Sema.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/implicit-widening-of-multiplication-result.c
  clang/test/Sema/implicit-widening-of-pointer-offset-in-array-subscript-expression.c
  clang/test/Sema/implicit-widening-of-pointer-offset.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93822.313744.patch
Type: text/x-patch
Size: 27711 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201226/bed408bb/attachment-0001.bin>


More information about the cfe-commits mailing list