Re: Bounds checking, Optimization techniques and undefined behavior

David Brown <david.brown@hesbynett.no>
Thu, 9 May 2019 09:19:23 +0200

          From comp.compilers

Related articles
[32 earlier articles]
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior derek@_NOSPAM_knosof.co.uk (Derek M. Jones) (2019-05-08)
Re: Bounds checking, Optimization techniques and undefined behavior bc@freeuk.com (Bart) (2019-05-09)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-09)
Re: Bounds checking, Optimization techniques and undefined behavior david.brown@hesbynett.no (David Brown) (2019-05-09)
Re: Bounds checking, Optimization techniques and undefined behavior 847-115-0292@kylheku.com (Kaz Kylheku) (2019-05-10)
| List of all articles for this month |

From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.compilers
Date: Thu, 9 May 2019 09:19:23 +0200
Organization: A noiseless patient Spider
References: 19-04-021 19-04-023 19-04-037 19-04-039 19-04-042 19-04-044 19-04-047 19-05-004 19-05-006 19-05-016 19-05-020 19-05-024 19-05-025 19-05-028 19-05-029 19-05-034 19-05-045 19-05-061
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="63993"; mail-complaints-to="abuse@iecc.com"
Keywords: standards, errors, comment
Posted-Date: 09 May 2019 11:28:50 EDT
Content-Language: en-GB

On 08/05/2019 10:03, David Brown wrote:


>
> [I'm looking at C11 and don't see where it says you can't cast away
> const. What am I missing? I agree any approach here is ugly and we're
> near the limits of what you can say in C. -John]
>


6.7.3p6 :


"""
If an attempt is made to modify an object defined with a const-qualified
type through use of an lvalue with non-const-qualified type, the
behavior is undefined.
"""




Consider this code:


int v = 10;
const int c = 20;


void change(const int * p, int n) {
*(int*) p = n;
}


int foo(int a) {
change(&v, a); // OK
change(&c, a); // Usually compiles, but NOT OK


return c;
}






You can cast away const (via pointers), but if the original declaration
of the object was const, it is undefined behaviour.


Compiling this with gcc x86 and optimisation (with
<https://godbolt.org>), you get this:


change:
                mov DWORD PTR [rdi], esi
                ret
foo:
                mov DWORD PTR v[rip], edi
                mov eax, 20
                mov DWORD PTR c[rip], edi
                ret
c:
                .long 20
v:
                .long 10




Although the call "change(&c, a)" attempts to modify "c" (which will
likely cause a fault on many systems), the compiler assumes the value of
"c" is still 20 for the return.
[Oh, look at that. Tried it with clang, did the same thing returning a constant 20. -John]


Post a followup to this message

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