source: public/doc/gnu-c/Signed-Overflow.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 5.7 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Previous: <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> &nbsp; [<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
69principle</em> undefined, meaning that anything whatsoever could happen.
70Therefore, C compilers can do optimizations that treat the overflow
71case with total unconcern. (Since the result of overflow is undefined
72in principle, one cannot claim that these optimizations are
73erroneous.)
74</p>
75<p><strong>Watch out:</strong> These optimizations can do surprising things. For
76instance,
77</p>
78<div class="example">
79<pre class="example">int i;
80<span class="roman">&hellip;</span>
81if (i &lt; 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
89overflow. These options operate per module; that is, each module
90behaves according to the options it was compiled with.
91</p>
92<p>These two options specify particular ways to handle signed integer
93overflow, 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
98operations: they produce the <var>n</var> low-order bits of the true
99result. The highest of those <var>n</var> bits is the sign bit of the
100result. With <samp>-fwrapv</samp>, these out-of-range operations are not
101considered overflow, so (strictly speaking) integer overflow never
102happens.
103</p>
104<p>The option <samp>-fwrapv</samp> enables some optimizations based on the
105defined values of out-of-range results. In GCC 8, it disables
106optimizations that are based on assuming signed integer operations
107will 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.
112This terminates the program unless the program handles the signal.
113See <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
123occurs. This checks the &lsquo;<samp>+</samp>&rsquo;, &lsquo;<samp>*</samp>&rsquo;, and &lsquo;<samp>-</samp>&rsquo; operators.
124This takes priority over <samp>-ftrapv</samp>.
125</p></dd>
126</dl>
127
128<hr>
129<div class="header">
130<p>
131Previous: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.