1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Signed Overflow (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Signed Overflow (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Signed Overflow (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Integer-Overflow.html" rel="up" title="Integer Overflow">
|
---|
33 | <link href="Mixed-Mode.html" rel="next" title="Mixed Mode">
|
---|
34 | <link href="Unsigned-Overflow.html" rel="prev" title="Unsigned Overflow">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Signed-Overflow"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Previous: <a href="Unsigned-Overflow.html" accesskey="p" rel="prev">Unsigned Overflow</a>, Up: <a href="Integer-Overflow.html" accesskey="u" rel="up">Integer Overflow</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Overflow-with-Signed-Integers"></span><h4 class="subsection">6.3.2 Overflow with Signed Integers</h4>
|
---|
64 | <span id="index-compiler-options-for-integer-overflow"></span>
|
---|
65 | <span id="index-integer-overflow_002c-compiler-options"></span>
|
---|
66 | <span id="index-overflow_002c-compiler-options"></span>
|
---|
67 |
|
---|
68 | <p>For signed integers, the result of overflow in C is <em>in
|
---|
69 | principle</em> undefined, meaning that anything whatsoever could happen.
|
---|
70 | Therefore, C compilers can do optimizations that treat the overflow
|
---|
71 | case with total unconcern. (Since the result of overflow is undefined
|
---|
72 | in principle, one cannot claim that these optimizations are
|
---|
73 | erroneous.)
|
---|
74 | </p>
|
---|
75 | <p><strong>Watch out:</strong> These optimizations can do surprising things. For
|
---|
76 | instance,
|
---|
77 | </p>
|
---|
78 | <div class="example">
|
---|
79 | <pre class="example">int i;
|
---|
80 | <span class="roman">…</span>
|
---|
81 | if (i < i + 1)
|
---|
82 | x = 5;
|
---|
83 | </pre></div>
|
---|
84 |
|
---|
85 | <p>could be optimized to do the assignment unconditionally, because the
|
---|
86 | <code>if</code>-condition is always true if <code>i + 1</code> does not overflow.
|
---|
87 | </p>
|
---|
88 | <p>GCC offers compiler options to control handling signed integer
|
---|
89 | overflow. These options operate per module; that is, each module
|
---|
90 | behaves according to the options it was compiled with.
|
---|
91 | </p>
|
---|
92 | <p>These two options specify particular ways to handle signed integer
|
---|
93 | overflow, other than the default way:
|
---|
94 | </p>
|
---|
95 | <dl compact="compact">
|
---|
96 | <dt><samp>-fwrapv</samp></dt>
|
---|
97 | <dd><p>Make signed integer operations well-defined, like unsigned integer
|
---|
98 | operations: they produce the <var>n</var> low-order bits of the true
|
---|
99 | result. The highest of those <var>n</var> bits is the sign bit of the
|
---|
100 | result. With <samp>-fwrapv</samp>, these out-of-range operations are not
|
---|
101 | considered overflow, so (strictly speaking) integer overflow never
|
---|
102 | happens.
|
---|
103 | </p>
|
---|
104 | <p>The option <samp>-fwrapv</samp> enables some optimizations based on the
|
---|
105 | defined values of out-of-range results. In GCC 8, it disables
|
---|
106 | optimizations that are based on assuming signed integer operations
|
---|
107 | will not overflow.
|
---|
108 | </p>
|
---|
109 | </dd>
|
---|
110 | <dt><samp>-ftrapv</samp></dt>
|
---|
111 | <dd><p>Generate a signal <code>SIGFPE</code> when signed integer overflow occurs.
|
---|
112 | This terminates the program unless the program handles the signal.
|
---|
113 | See <a href="Signals.html">Signals</a>.
|
---|
114 | </p></dd>
|
---|
115 | </dl>
|
---|
116 |
|
---|
117 | <p>One other option is useful for finding where overflow occurs:
|
---|
118 | </p>
|
---|
119 |
|
---|
120 | <dl compact="compact">
|
---|
121 | <dt><samp>-fsanitize=signed-integer-overflow</samp></dt>
|
---|
122 | <dd><p>Output a warning message at run time when signed integer overflow
|
---|
123 | occurs. This checks the ‘<samp>+</samp>’, ‘<samp>*</samp>’, and ‘<samp>-</samp>’ operators.
|
---|
124 | This takes priority over <samp>-ftrapv</samp>.
|
---|
125 | </p></dd>
|
---|
126 | </dl>
|
---|
127 |
|
---|
128 | <hr>
|
---|
129 | <div class="header">
|
---|
130 | <p>
|
---|
131 | Previous: <a href="Unsigned-Overflow.html" accesskey="p" rel="prev">Unsigned Overflow</a>, Up: <a href="Integer-Overflow.html" accesskey="u" rel="up">Integer Overflow</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
132 | </div>
|
---|
133 |
|
---|
134 |
|
---|
135 |
|
---|
136 | </body>
|
---|
137 | </html>
|
---|