Bug 87454 - Maybe implement -fsanitize=implicit-integer-truncation
Summary: Maybe implement -fsanitize=implicit-integer-truncation
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: sanitizer (show other bugs)
Version: 9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-09-27 13:29 UTC by Martin Liška
Modified: 2021-12-19 16:25 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-11-07 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Liška 2018-09-27 13:29:35 UTC
It's new in LLVM 7.0.0:

  -  ``-fsanitize=implicit-integer-truncation``: Implicit conversion from
     integer of larger bit width to smaller bit width, if that results in data
     loss. That is, if the demoted value, after casting back to the original
     width, is not equal to the original value before the downcast.
     Issues caught by this sanitizer are not undefined behavior,
     but are often unintentional.

Example:

unsigned char store = 0;

bool consume(unsigned int val);

void test(unsigned long val) {
  if (consume(val)) // the value may have been silently truncated.
    store = store + 768; // before addition, 'store' was promoted to int.
  (void)consume((unsigned int)val); // OK, the truncation is explicit.
}
Comment 1 Martin Liška 2018-11-07 09:43:42 UTC
Note that truncation is only part of recently added Implicit Conversion Sanitizer:
http://releases.llvm.org/7.0.0/tools/clang/docs/ReleaseNotes.html#undefined-behavior-sanitizer-ubsan

One can find a blog post about it here (mentioned in LLVM weekly):
John Regehr has a short blog post on [recently added implicit integer cast 
sanitizers](https://blog.regehr.org/archives/1633).