The Unix Heritage Society mailing list
 help / color / mirror / Atom feed
* [TUHS] C history question: why is signed integer overflow UB?
@ 2025-08-15 17:17 Dan Cross
  2025-08-15 17:31 ` [TUHS] " Luther Johnson
  2025-08-17  2:25 ` Clem Cole
  0 siblings, 2 replies; 12+ messages in thread
From: Dan Cross @ 2025-08-15 17:17 UTC (permalink / raw)
  To: TUHS; +Cc: Douglas McIlroy

[Note: A few folks Cc'ed directly]

This is not exactly a Unix history question, but given the close
relationship between C's development and that of Unix, perhaps it is
both topical and someone may chime in with a definitive answer.

Starting with the 1990 ANSI/ISO C standard, and continuing on to the
present day, C has specified that signed integer overflow is
"undefined behavior"; unsigned integer arithmetic is defined to be
modular, and unsigned integer operations thus cannot meaningfully
overflow, since they're always taken mod 2^b, where b is the number of
bits in the datum (assuming unsigned int or larger, since type
promotion of smaller things gets weird).

But why is signed overflow UB? My belief has always been that signed
integer overflow across various machines has non-deterministic
behavior, in part because some machines would trap on overflow (e.g.,
Unisys 1100 series mainframes) while others used non-2's-complement
representations for signed integers (again, the Unisys 1100 series,
which used 1's complement), and so the results could not be precisely
defined: even if it did not trap, overflowing a 1's complement machine
yielded a different _value_ than on 2's complement. And around the
time of initial standardization, targeting those machines was still an
important use case.  So while 2's complement with silent wrap-around
was common, it could not be assumed, and once machines that generated
traps on overflow were brought into the mix, it was safer to simply
declare behavior on overflow undefined.

But is that actually the case?

Thanks in advance.

        - Dan C.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2025-08-17  2:26 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-15 17:17 [TUHS] C history question: why is signed integer overflow UB? Dan Cross
2025-08-15 17:31 ` [TUHS] " Luther Johnson
2025-08-15 17:36   ` Luther Johnson
2025-08-15 18:03     ` Warner Losh
2025-08-16  6:01       ` Lars Brinkhoff
2025-08-15 18:02   ` Nevin Liber
2025-08-15 18:25     ` Luther Johnson
2025-08-15 18:44       ` John Levine
2025-08-15 21:04         ` Douglas McIlroy
2025-08-15 21:59           ` Dave Horsfall
2025-08-15 23:58           ` Luther Johnson
2025-08-17  2:25 ` Clem Cole

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).