Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-generated code with infinite loop segfaults #122401

Closed
charles-cooper opened this issue Jan 10, 2025 · 4 comments
Closed

clang-generated code with infinite loop segfaults #122401

charles-cooper opened this issue Jan 10, 2025 · 4 comments
Labels
invalid Resolved as invalid, i.e. not a bug miscompilation undefined behaviour

Comments

@charles-cooper
Copy link

the following code segfaults on any optimization level besides -O0:

#include "stdio.h"

int main() {
    printf("enter1\n");
    unsigned int i, sum = 0;
    for (i = 10; i >= 0; --i) {
        sum += i;
    }
    printf("sum = %u\n", sum);
}

repro:

~ $ clang --version
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
~ $ clang -O1 bug.c -o bug && ./bug
enter1
Segmentation fault (core dumped)

probably related: #60622

@charles-cooper
Copy link
Author

charles-cooper commented Jan 10, 2025

note that with the first print statement removed, the program simply terminates without doing anything.

#include "stdio.h"

int main() {
    unsigned int i, sum = 0;
    for (i = 10; i >= 0; --i) {
        sum += i;
    }
    printf("sum = %u\n", sum);
}

@EugeneZelenko
Copy link
Contributor

Could you please try 19 or main branch? https://godbolt.org should be helpful.

Only most recent release is maintained.

@charles-cooper
Copy link
Author

same behavior. i've included IR output to help

https://godbolt.org/z/1WjPKrf7h

Image
Image

@dtcxzyw dtcxzyw added invalid Resolved as invalid, i.e. not a bug undefined behaviour labels Jan 10, 2025
@dtcxzyw
Copy link
Member

dtcxzyw commented Jan 10, 2025

Non-trivial infinite loops are UB: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2809r3.html
If you want the infinite loop behaves as is (i.e., get stuck without segfaults), please use the trivial form: https://godbolt.org/z/K8rxrEPrM

@dtcxzyw dtcxzyw closed this as not planned Won't fix, can't repro, duplicate, stale Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid Resolved as invalid, i.e. not a bug miscompilation undefined behaviour
Projects
None yet
Development

No branches or pull requests

5 participants