blob: e6508db9345f7d347368763d3854082c8154a39a [file] [log] [blame] [view]
Chris Palmereff252d62017-11-07 22:34:411# Integer Semantics, Unsafety, And You
2
3[TOC]
4
5These handy tips apply in any memory management situation and in any kind of IPC
6situation (classic Chromium IPCMojo, Windows/POSIX IPC, Mach IPC, files,
7sockets, parsing binary formats, ...).
8
9Basically, don't believe the lie that 'computers are good at arithmetic'. In
10general, unless you explicitly check an arithmetic operation, it's safest to
11assume the operation went wrong. The least painful way to systematically check
12arithmetic is Chromium's base/numerics templates and helper functions.
13
14## Be Aware Of The Subtleties Of Integer Types
15
16First [read about the scary security implications of integer arithmetic in
17C/C++](http://en.wikipedia.org/wiki/Integer_overflow). Adhere to these best
18practices:
19
20* Use the [integer templates and cast templates in
21base/numerics](../base/numerics/README.md) to avoid overflows, **especially when
22calculating the size or offset of memory allocations**.
23* Use unsigned types for values that shouldn't be negative or where defined
24overflow behavior is required.
25* Use explicitly sized integer types, such as `int32_t`, `int64_t`, or
26`uint32_t`, since caller and callee could potentially use different
27interpretations of implicitly-sized types like `int` or `long`. (For example, a
2864-bit browser process and a 32-bit plug-in process might interpret `long`
29differently.)
30
31## Be Aware Of The Subtleties Of Integer Types Across Languages
32
33### Java
34
35When writing code for Chromium on Android, you will often need to marshall
36arrays, and their sizes and indices, across the language barrier (and possibly
37also across the IPC barrier). The trouble here is that the Java integer types
38are well-defined, but the C++ integer types are whimsical. A Java `int` is a
39signed 32-bit integer with well-defined overflow semantics, and a Java `long` is
40a signed 64-bit integer with well-defined overflow semantics. in C++, only the
41explicitly-sized types (e.g. `int32_t`) have guaranteed exact sizes, and only
42unsigned integers (of any size) have defined overflow semantics.
43
44Essentially, Java integers **actually are** what people often (incorrectly)
45**assume** C++ integers are. Furthermore, Java `Array`s are indexed with Java
46`int`s, whereas C++ arrays are indexed with `size_t` (often implicitly cast, of
47course). Note that this also implies a 2^31 limit on the number of elements in
48an array that is coming from or going to Java. That Should Be Enough For
49Anybody, but it's good to keep in mind.
50
51You need to make sure that every integer value survives its journey across
52languages intact. That generally means explicit casts with range checks; the
53easiest way to do this is with the `base::checked_cast` or
54`base::saturated_cast` templates in base/numerics. Depending on how the integer
55object is going to be used, and in which direction the value is flowing, it may
56make sense to cast the value to `jint` (an ID or regular integer), `jlong` (a
57regular long integer), `size_t` (a size or index), or one of the other more
58exotic C++ integer types like `off_t`.
59
60### JavaScript And JSON
61
62[Here is some good reading on integers in
63JavaScript](http://2ality.com/2014/02/javascript-integers.html). TL;DR:
64
65* Normal JavaScript `Number`s have a 'safe' integer range of 53 bits (signed).
66See `Number.isSafeInteger`, `Number.MIN_SAFE_INTEGER`, and
67`Number.MAX_SAFE_INTEGER`.
68* Array indices are unsigned 32-bit values.
69* Character codes (`fromCharCode`, `charCodeAt`) are unsigned 16-bit values.