Undefined behaviour, was: for or against equality

Martin Ward <martin@gkc.org.uk>
Fri, 7 Jan 2022 14:02:50 +0000

          From comp.compilers

Related articles
Undefined behaviour, was: for or against equality martin@gkc.org.uk (Martin Ward) (2022-01-07)
Re: Undefined behaviour, was: for or against equality david.brown@hesbynett.no (David Brown) (2022-01-07)
Re: Undefined behaviour, was: for or against equality spibou@gmail.com (Spiros Bousbouras) (2022-01-08)
Re: Undefined behaviour, was: for or against equality anton@mips.complang.tuwien.ac.at (2022-01-08)
Re: Undefined behaviour, was: for or against equality david.brown@hesbynett.no (David Brown) (2022-01-09)
Re: Undefined behaviour, was: for or against equality 480-992-1380@kylheku.com (Kaz Kylheku) (2022-01-11)
Re: Undefined behaviour, was: for or against equality gneuner2@comcast.net (George Neuner) (2022-01-11)
| List of all articles for this month |

From: Martin Ward <martin@gkc.org.uk>
Newsgroups: comp.compilers
Date: Fri, 7 Jan 2022 14:02:50 +0000
Organization: Compilers Central
References: <17d70d74-1cf1-cc41-6b38-c0b307aeb35a@gkc.org.uk> 22-01-016 22-01-018
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="31121"; mail-complaints-to="abuse@iecc.com"
Keywords: standards, semantics
Posted-Date: 07 Jan 2022 20:25:43 EST
In-Reply-To: 22-01-018
Content-Language: en-GB

On 06/01/2022 08:11, David Brown wrote:
> The trick is to memorize the/defined/ behaviours, and stick to them.


Isn't the set of defined behaviours bigger than the set
of undefined behaviours? How do you know what is defined
if you don't know what is undefined?


For example, a = b + c is precisely defined in C and C++ for
floating point variables, but the result can be "undefined behaviour"
for ordinary 32 bit signed integer values.


If you want to stick to defined behaviours then you need
to add extra code. For example, CERT recommends:


      if (((si_b > 0) && (si_a > (INT_MAX - si_b))) ||
              ((si_b < 0) && (si_a < (INT_MIN - si_b)))) {
          /* Handle error */
      } else {
          sum = si_a + si_b;
      }


--
Martin


Dr Martin Ward | Email: martin@gkc.org.uk | http://www.gkc.org.uk
G.K.Chesterton site: http://www.gkc.org.uk/gkc | Erdos number: 4


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.