9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Charles Forsyth <charles.forsyth@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] Undefined Behaviour in C
Date: Mon, 23 Nov 2015 14:30:29 +0000	[thread overview]
Message-ID: <CAOw7k5joEYjF=rtNkongkj15ZcuEYvRfKpp1cy6h8c1McaYPMQ@mail.gmail.com> (raw)
In-Reply-To: <1448274004.1751482.447419065.2BE466C4@webmail.messagingengine.com>

[-- Attachment #1: Type: text/plain, Size: 1513 bytes --]

On 23 November 2015 at 10:20, Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
wrote:

> Had been reading the SOSP paper:
> <https://pdos.csail.mit.edu/papers/stack:sosp13.pdf>
>

As an example of how tricky it can be, one of their examples is

const uint8_t *data = /* buffer head */;
const uint8_t *data_end = /* buffer tail */;
int size = bytestream_get_be16(&data);
if (data + size >= data_end || data + size < data)
   return -1;

They say "A correct fix is to replace data + x >= data_end || data + x <
data with x >= data_end − data, which is simpler and also avoids invoking
undefined behavior; one should also add the check x < 0 if x can be
negative."

Unfortunately, that replacement is itself well-defined only if data and
data_end "point to elements of the same array object, or one past the last
element of the array object" (and there's an implementation-dependent
option for the interpretation of "one past" when ensuring the address can
be represented). It looks from the comments as though that might be true in
this particular case (or it's intended to be understood), but if not,
avoiding the compiler's "optimisation" that messes up one form of undefined
behaviour will lead you to write code that has different undefined states.

Generally, an optimising compiler for a systems language, especially one
 in which pointer values can be manipulated explicitly,
needs to be sure of its ground when second-guessing the effect of a given
statement or expression.

[-- Attachment #2: Type: text/html, Size: 2518 bytes --]

      parent reply	other threads:[~2015-11-23 14:30 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-23 10:20 Ramakrishnan Muthukrishnan
2015-11-23 11:20 ` Vasudev Kamath
2015-11-25 10:27   ` Alexandru Gheorghe
2015-11-25 10:43     ` Brantley Coile
2015-11-25 10:53       ` Brantley Coile
2015-11-25 12:59       ` Charles Forsyth
2015-11-25 13:48         ` erik quanstrom
2015-11-25 14:25           ` Brantley Coile
2015-11-25 14:31             ` Brantley Coile
2015-11-25 16:03             ` plannine
2015-11-25 17:13               ` Ryan Gonzalez
2015-11-25 18:41                 ` Brantley Coile
2015-11-26  2:04                   ` Prof Brucee
2015-11-26  2:43                     ` Brantley Coile
2015-11-26  2:57                       ` Prof Brucee
2015-11-26  3:48                         ` Ryan Gonzalez
2015-11-26  7:27                     ` Bakul Shah
2015-11-26 11:22                       ` Brantley Coile
2015-11-26 11:37                         ` tlaronde
2015-11-26 11:55                           ` Charles Forsyth
2015-11-26 11:38                         ` Bruce Ellis
2015-11-26 16:31                         ` erik quanstrom
2015-11-26 16:42                           ` Brantley Coile
2015-11-26 16:50                             ` Charles Forsyth
2015-11-26 17:12                               ` erik quanstrom
2015-11-26 16:46                           ` Alexandru Gheorghe
2015-11-26 17:48                         ` Bakul Shah
2015-11-26 18:04                           ` Brantley Coile
2015-11-26 23:14                           ` Steve Simon
2015-11-26 23:24                             ` Charles Forsyth
2015-11-26 23:55                             ` Brantley Coile
2015-11-25 19:19               ` Steffen Nurpmeso
2015-11-23 11:32 ` Charles Forsyth
2015-11-23 11:37   ` Charles Forsyth
2015-11-23 11:50 ` Brantley Coile
2015-11-23 12:05   ` Charles Forsyth
2015-11-23 12:17     ` Brantley Coile
2015-11-23 12:40       ` Charles Forsyth
2015-11-23 12:09   ` Charles Forsyth
2015-11-23 14:30 ` Charles Forsyth [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOw7k5joEYjF=rtNkongkj15ZcuEYvRfKpp1cy6h8c1McaYPMQ@mail.gmail.com' \
    --to=charles.forsyth@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).