Skip to content

Inverse expression IR for casts is sometime incorrect #1211

Open
@dtarditi

Description

@dtarditi

While doing the upgrade to clang 17 sources, I found that a compiler assert was failing in a new test that was added to clang-17: test/Sema/pointer-subtraction.c.

 #3 0x00007ff67d2383dd llvm::llvm_unreachable_internal(char const *, char const *, unsigned int) ... llvm\lib\Support\ErrorHandling.cpp:212:0
 #4 0x00007ff6861abd97 `anonymous namespace'::IntExprEvaluator::VisitCastExpr ... clang\lib\AST\ExprConstant.cpp:13583:0

A minimal cut-down case is:

void a(void) {
  char *f = (char *)0;
  
    f = (char *)((char *)0 - f);         // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}};
    f = (char *)((char *)0 - f);         // expected-warning {{performing pointer subtraction with a null pointer has undefined behavior}}
}

I tracked the assert failure to incorrect IR being created for inverse expressions for casts in lib/AST/ExprUtils.cpp in the InverseUtil class. The problem is that some cast operators need to be inverted too. For example CK_IntegralToPointer needs to be inverted to CK_PointerToIntegral.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions