Bug 163188 - setting const variables inside a vararg does not change them at -O1 and -O2
setting const variables inside a vararg does not change them at -O1 and -O2
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
All Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
Depends On:
  Show dependency treegraph
Reported: 2005-07-13 17:11 EDT by Dan Williams
Modified: 2007-11-30 17:11 EST (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2005-07-14 03:52:22 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Dan Williams 2005-07-13 17:11:27 EDT
See the testcase.  bar starts as -1, but should be 42 after calling foo(), even
though its const.  You could argue that bar shouldn't be const since we are
modifying it, but even so, the behavior should not be different at -O0 than -O1/2.

# gcc quux.c -O0 -o quux
# ./quux
Success: bar is 42

# gcc quux.c -O2 -o quux
# ./quux
Error: bar is not 42!  (it is -1)


#include <stdarg.h>
#include <stdio.h>

void foo (int n_args, ...)
    va_list qux;
    int *bar;

    va_start (qux, n_args);
    bar = va_arg (qux, int *);
    *bar = 42;

int main (int argc, char **argv)
    const int bar = -1;

    foo (1, &bar);
    if (bar != 42)
        printf ("Error: bar is not 42!  (it is %d)\n", bar);
        printf ("Success: bar is 42\n");
    return 0;
Comment 1 Dan Williams 2005-07-13 17:12:37 EDT
Testcase fails at -O1 and -O2 on both:

Comment 2 Jakub Jelinek 2005-07-14 03:52:22 EDT
The testcase is simply invalid, you must not modify a const variable.
The compiler is free to assume it is not modified (that is the purpose of
the const modifier, right?), but does not have to.  You simply trigger an
undefined behaviour and anything can happen at that point.

Note You need to log in before you can comment on or make changes to this bug.