diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-06-22 09:48:24 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-06-22 09:49:13 -0400 |
commit | 2cf033f238c111bef62552da16117568435d3a18 (patch) | |
tree | 34deb903de7bbdf42c399dce861e63c898c77dae /src/lib/intmath/addsub.c | |
parent | 3883338c8121212b3f6f9c020d489d50bbcdd855 (diff) | |
download | tor-2cf033f238c111bef62552da16117568435d3a18.tar.gz tor-2cf033f238c111bef62552da16117568435d3a18.zip |
Extract simple integer math into its own module
Diffstat (limited to 'src/lib/intmath/addsub.c')
-rw-r--r-- | src/lib/intmath/addsub.c | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/lib/intmath/addsub.c b/src/lib/intmath/addsub.c new file mode 100644 index 0000000000..816c5a2bd7 --- /dev/null +++ b/src/lib/intmath/addsub.c @@ -0,0 +1,20 @@ +/* Copyright (c) 2003-2004, Roger Dingledine + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "lib/intmath/addsub.h" +#include "lib/cc/compat_compiler.h" + +/* Helper: safely add two uint32_t's, capping at UINT32_MAX rather + * than overflow */ +uint32_t +tor_add_u32_nowrap(uint32_t a, uint32_t b) +{ + /* a+b > UINT32_MAX check, without overflow */ + if (PREDICT_UNLIKELY(a > UINT32_MAX - b)) { + return UINT32_MAX; + } else { + return a+b; + } +} |