This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/65273] New: Incorrect output in SIMPLE program compiled from high version gcc with O2


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65273

            Bug ID: 65273
           Summary: Incorrect output in SIMPLE program compiled from high
                    version gcc with O2
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: codeeply at foxmail dot com

#include <iostream>
#include <cstring>

using namespace std;

const int N = 2000017;

int hash(const char *s) {
    volatile int ret = 1;
    int i, len = strlen(s);
    for (i = 0; i < len; ++i) {
        ret = ret * 131 + s[i];
    }
    ret %= N;
    if (ret < 0) ret += N;
    return ret;
}

int hash1(const char *s) {
    int ret = 1;
    int i, len = strlen(s);
    for (i = 0; i < len; ++i) {
        ret = ret * 131 + s[i];
    }
    ret %= N;
    if (ret < 0) ret += N;
    return ret;
}

int main() {
    cout << hash("sfihsifef") << endl;
    cout << hash1("sfihsifef") << endl;
    return 0;
}

---------------------------------------------------------------------
:~/test>g++ -O2 g.cpp 
:~/test>./a.out 
602373
-1397644
---------------------------------------------------------------------
It's found that "if (ret < 0) ret += N;" was "optimized" without volatile.
And this bug has been seen in 4.7.2 and 4.8.2, but not in 4.1.2.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]