Skip to content

Miscompiling error resulting to go into a forloop when the condition is false #145919

Closed as not planned
@alliaces

Description

@alliaces

Hi !
I'm encoutering a problem from clang19.1.0 (clang20.1.0 also has it) with this kind of code :
https://godbolt.org/z/WWdqcvj6h

double recursiveEval(int n, const double a) {
    return n <= 0 ? 0 : a + recursiveEval(n - 1, a);
}

double eval(const std::vector<double>& emptyVector) {
    std::vector<double> vec;
    for (const double& it : emptyVector) {
        std::cerr << "BIG ERROR !! I should not be here" << std::endl;
        vec.push_back(recursiveEval(it, it));
    }
    return recursiveEval(vec.size(), vec[0]);
}


int main(int argc, char **argv) {
    std::vector<double> coefs;

    eval(coefs);
    return 0;
}

It segfaults but that is not the problem. The problem is we go into the forloop whereas the vector emptyVector is empty.

The problem disappears when (any of these):

  • vec or emptyVector are not empty
  • vec[0] is replaced by a prvalue
  • the line return recursiveEval(vec.size(), vec[0]); is deleted (replaced by any return).

Thanks for your help

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions