Skip to content

[Clang] Diagnose invalid pointer overflow check with intermediate variable #122457

Open
@nikic

Description

@nikic

#120222 added support to -Wtautological-compare to diagnose comparisons like ptr + unsigned_offset < ptr, which are always false due to undefined behavior on pointer overflow during addition.

However, most commonly this pattern appears when the result of the addition is stored in an intermediate variable first (because it will also be used later):

bool test(const char *ptr, size_t index) {
  const char *end_ptr = ptr + index;
  return end_ptr < ptr;
}

It would be great to diagnose these cases as well, using an analysis-based warning.

cc @AaronBallman @ldionne as we discussed this on Wednesday.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions