Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Issue 2522073004: Add variadic helper functions for CheckedNumeric math operations (Closed)

Created:
4 years ago by jschuh
Modified:
4 years ago
Reviewers:
Tom Sepez, dcheng
CC:
chromium-reviews, vmpstr+watch_chromium.org
Target Ref:
refs/pending/heads/master
Project:
chromium
Visibility:
Public.

Description

Add variadic helper functions for CheckedNumeric math operations This introduces variadic template functions that accept two or more arithmetic or CheckedNumeric arguments and return a CheckedNumeric. The functions are: * base::CheckAdd() - addition * base::CheckSub() - subtraction * base::CheckMul() - multiplication * base::CheckDiv() - division * base::CheckMod() - modulous * base::CheckLsh() - left integer shift * base::CheckRsh() - right integer shift BUG=667097 NOTRY=true NOPRESUBMIT=true Committed: https://crrev.com/4fcd6fa432b464f64de96c9bb03593b131a07029 Cr-Commit-Position: refs/heads/master@{#434311}

Patch Set 1 #

Patch Set 2 : docs and nits #

Patch Set 3 : clang/gcc compile fixes #

Patch Set 4 : clang/gcc compile fix #

Patch Set 5 : test fixes #

Total comments: 10

Patch Set 6 : nits #

Total comments: 1

Patch Set 7 : compile fix #

Unified diffs Side-by-side diffs Delta from patch set Stats (+234 lines, -92 lines) Patch
M base/numerics/safe_math.h View 1 2 3 4 5 7 chunks +131 lines, -53 lines 0 comments Download
M base/numerics/safe_math_impl.h View 1 2 15 chunks +74 lines, -37 lines 0 comments Download
M base/numerics/safe_numerics_unittest.cc View 1 2 3 4 5 6 4 chunks +29 lines, -2 lines 0 comments Download

Messages

Total messages: 50 (25 generated)
jschuh
tsepez@ - One last big CL full of ugly template goop. There's a little bit ...
4 years ago (2016-11-23 14:59:02 UTC) #4
Tom Sepez
On 2016/11/23 14:59:02, jschuh (very slow) wrote: > tsepez@ - One last big CL full ...
4 years ago (2016-11-23 16:51:50 UTC) #7
Tom Sepez
https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h File base/numerics/safe_math.h (right): https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h#newcode258 base/numerics/safe_math.h:258: // The typedef was required here because MSVC fails ...
4 years ago (2016-11-23 16:52:06 UTC) #8
Tom Sepez
https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h File base/numerics/safe_math.h (right): https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h#newcode38 base/numerics/safe_math.h:38: // * CheckedAdd() - addition Can I explicitly specialize ...
4 years ago (2016-11-23 16:54:58 UTC) #9
jschuh
https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h File base/numerics/safe_math.h (right): https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h#newcode38 base/numerics/safe_math.h:38: // * CheckedAdd() - addition On 2016/11/23 16:54:58, Tom ...
4 years ago (2016-11-23 19:18:00 UTC) #10
Tom Sepez
https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h File base/numerics/safe_math.h (right): https://codereview.chromium.org/2522073004/diff/80001/base/numerics/safe_math.h#newcode38 base/numerics/safe_math.h:38: // * CheckedAdd() - addition On 2016/11/23 19:18:00, jschuh ...
4 years ago (2016-11-23 19:47:08 UTC) #11
Tom Sepez
Note: CL Description needs update with short names.
4 years ago (2016-11-23 21:34:02 UTC) #13
Tom Sepez
https://codereview.chromium.org/2522073004/diff/120001/base/numerics/safe_numerics_unittest.cc File base/numerics/safe_numerics_unittest.cc (right): https://codereview.chromium.org/2522073004/diff/120001/base/numerics/safe_numerics_unittest.cc#newcode845 base/numerics/safe_numerics_unittest.cc:845: EXPECT_EQ(static_cast<decltype(h)>(1), h); can we add a test that CheckAdd<int16_t>(0x10000, ...
4 years ago (2016-11-23 21:37:25 UTC) #14
jschuh
When I'm back in the office we can talk about the problem of silent truncation ...
4 years ago (2016-11-23 21:49:22 UTC) #15
Tom Sepez
lgtm
4 years ago (2016-11-23 21:58:05 UTC) #17
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/v2/patch-status/codereview.chromium.org/2522073004/120001
4 years ago (2016-11-23 22:01:50 UTC) #19
commit-bot: I haz the power
Try jobs failed on following builders: ios-simulator on master.tryserver.chromium.mac (JOB_FAILED, http://build.chromium.org/p/tryserver.chromium.mac/builders/ios-simulator/builds/112663)
4 years ago (2016-11-23 22:26:16 UTC) #21
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/v2/patch-status/codereview.chromium.org/2522073004/140001
4 years ago (2016-11-24 02:42:10 UTC) #24
commit-bot: I haz the power
Try jobs failed on following builders: cast_shell_linux on master.tryserver.chromium.linux (JOB_TIMED_OUT, no build URL) chromeos_x86-generic_chromium_compile_only_ng on ...
4 years ago (2016-11-24 04:43:14 UTC) #26
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/v2/patch-status/codereview.chromium.org/2522073004/140001
4 years ago (2016-11-24 08:07:43 UTC) #28
commit-bot: I haz the power
Try jobs failed on following builders: chromium_presubmit on master.tryserver.chromium.linux (JOB_TIMED_OUT, no build URL) linux_chromium_compile_dbg_ng on ...
4 years ago (2016-11-24 10:08:35 UTC) #30
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/v2/patch-status/codereview.chromium.org/2522073004/140001
4 years ago (2016-11-24 11:38:30 UTC) #32
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/v2/patch-status/codereview.chromium.org/2522073004/140001
4 years ago (2016-11-24 11:55:00 UTC) #36
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/v2/patch-status/codereview.chromium.org/2522073004/140001
4 years ago (2016-11-24 11:58:11 UTC) #40
commit-bot: I haz the power
Committed patchset #7 (id:140001)
4 years ago (2016-11-24 11:59:14 UTC) #43
commit-bot: I haz the power
Patchset 7 (id:??) landed as https://crrev.com/4fcd6fa432b464f64de96c9bb03593b131a07029 Cr-Commit-Position: refs/heads/master@{#434311}
4 years ago (2016-11-24 12:01:54 UTC) #45
dcheng
Dumb question: what are the promotion rules with the new helpers? If I CheckAdd() arguments ...
4 years ago (2016-12-01 07:40:22 UTC) #47
jschuh
On 2016/12/01 07:40:22, dcheng wrote: > Dumb question: what are the promotion rules with the ...
4 years ago (2016-12-01 14:13:39 UTC) #48
chromium-reviews
Can we document this in a comment? ;-) Daniel On Thu, Dec 1, 2016, 06:13 ...
4 years ago (2016-12-01 16:08:09 UTC) #49
jschuh
4 years ago (2016-12-01 21:56:31 UTC) #50
Message was sent while issue was closed.
I will do this thing you ask of me... but I will make you review the CL.

On 2016/12/01 16:08:09, chromium-reviews wrote:
> Can we document this in a comment? ;-)
> 
> Daniel
> 
> On Thu, Dec 1, 2016, 06:13 <mailto:jschuh@chromium.org> wrote:
> 
> > On 2016/12/01 07:40:22, dcheng wrote:
> > > Dumb question: what are the promotion rules with the new helpers? If I
> > > CheckAdd() arguments with a bunch of different widths, what width should
> > I
> > > expect the result to be?
> >
> > It's identical to how the CheckedNumeric arithmetic operator overloads
> > work.
> > That is, it's standard C arithmetic promotions with two big exceptions:
> > 1. There's no default int promotion for types narrower than int.
> > 2. Bitwise logical operations will always promote to unsigned of the wider
> > type.
> >
> >
> > > In addition, what happens if I explicitly specialize the template to
> > something
> > > that's smaller than some of the input arguments? I assume that works as
> > well
> > > (and similarly for the case where I intentionally specialize to an
> > overly wide
> > > width)?
> >
> > You'll get truncations or extensions, respectively, so I wouldn't advise
> > it. I
> > suppose this is a reasonable argument for a presubmit check.
> >
> > Also, FYI, if you want to safely force an arithmetic value to a wider
> > type, you
> > should use base::strict_cast<> because it will throw a compiler error on
> > truncation.
> >
> > https://codereview.chromium.org/2522073004/
> >
> 
> -- 
> You received this message because you are subscribed to the Google Groups
> "Chromium-reviews" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email
> to mailto:chromium-reviews+unsubscribe@chromium.org.

Powered by Google App Engine
This is Rietveld 408576698