Timeline for How do I detect unsigned integer overflow?
Current License: CC BY-SA 4.0
15 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Dec 16, 2019 at 4:44 | comment | added | supercat | @MikeMB: Most of the optimizations I'd regard as useful would effectively involve allowing integer operations to behave as though the upper bits were indeterminate, but gcc will sometimes generate nonsensical code when overflows occur even in cases where the upper bits would be totally ignored. | |
| Nov 9, 2019 at 13:23 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Made compliant with the Jon Skeet Decree - <https://twitter.com/PeterMortensen/status/976400000942034944>, etc.
|
| May 3, 2016 at 23:14 | comment | added | MikeMB | Let us continue this discussion in chat. | |
| May 3, 2016 at 23:13 | comment | added | Stack Exchange Broke The Law |
@MikeMB The optimization where the compiler doesn't bother to check that n is less than 32, before emitting a shift instruction that only uses the lower 5 bits of n?
|
|
| May 3, 2016 at 23:12 | comment | added | MikeMB | @immibis: I'm not really in favor of those kinds of warnings in general, because a) I fear they would become quite noisy and b) would probably only produce a meaningful message in trivial cases. But I really don't see how your particular examples would produce a warning. What optimization would rely on a leftshift not overflowing? Actually, besides the classical example posted by A Fog, I know very few optimizations that are based on the assumption that integers won't overflow. | |
| May 3, 2016 at 11:35 | comment | added | Stack Exchange Broke The Law |
@MikeMB So what you mean is: the compiler should issue a warning by default when making an optimization that relies on overflow not occurring unless it can prove that overflow won't occur. Then you end up with warnings for things like 1 << n.
|
|
| May 3, 2016 at 11:33 | comment | added | Stack Exchange Broke The Law | @MikeMB Oh, I confused that with "the compiler should issue a warning whenever overflow could occur". | |
| May 3, 2016 at 6:09 | comment | added | MikeMB | @immibis: To quote the above: "I think the compiler should issue a warning by default when making an optimization that relies on overflow not occurring." | |
| May 3, 2016 at 5:51 | comment | added | Stack Exchange Broke The Law |
@MikeMB So for(int k = 0; k < 5; k++) {...} should raise a warning when optimizations are disabled (so the compiler hasn't gone to the effort to prove that k is bounded)?
|
|
| May 3, 2016 at 5:49 | comment | added | MikeMB |
@immibis: Why should it? The values of k can easily be determined at compile time. The compiler doesn't have to make any assumptions.
|
|
| Jan 16, 2016 at 11:01 | comment | added | Stack Exchange Broke The Law |
"I think the compiler should issue a warning by default when making an optimization that relies on overflow not occurring." - so for(int k = 0; k < 5; k++) {...} should raise a warning?
|
|
| S Feb 2, 2012 at 3:45 | history | suggested | SamB | CC BY-SA 3.0 |
Code formatting
|
| Feb 2, 2012 at 3:39 | comment | added | SamB | Note that compilers may only do this with signed integer types; overflow is completely defined for the unsigned integer types. Still, yes, it's quite a dangerous trap! | |
| Feb 2, 2012 at 3:36 | review | Suggested edits | |||
| S Feb 2, 2012 at 3:45 | |||||
| Jul 25, 2011 at 21:40 | history | answered | A Fog | CC BY-SA 3.0 |