tor  0.4.0.1-alpha
addsub.c
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
14 #include "lib/intmath/addsub.h"
15 #include "lib/cc/compat_compiler.h"
16 
17 /* Helper: safely add two uint32_t's, capping at UINT32_MAX rather
18  * than overflow */
19 uint32_t
20 tor_add_u32_nowrap(uint32_t a, uint32_t b)
21 {
22  /* a+b > UINT32_MAX check, without overflow */
23  if (PREDICT_UNLIKELY(a > UINT32_MAX - b)) {
24  return UINT32_MAX;
25  } else {
26  return a+b;
27  }
28 }
Utility macros to handle different features and behavior in different compilers.
Header for addsub.c.