Skip to content

-Wtautological-compare should flag tautological pointer bounds checks #120214

Closed
@nikic

Description

@nikic

-Wtautological-compare should report pointer "bounds checks" patterns that are always true/false because unsigned pointer arithmetic cannot overflow.

bool test1(const char *ptr, size_t index) {
  return ptr + index < ptr; // always false
}

bool test2(const char *ptr, size_t index) {
  return ptr + index >= ptr; // always true
}

These two are fine though:

bool test3(const char *ptr, ssize_t index) {
  return ptr + index < ptr; // unknown (same as index < 0)
}

bool test4(const char *ptr, ssize_t index) {
  return ptr + index >= ptr; // unknown (same as index >= 0)
}

Idea from #118472 (comment).

Metadata

Metadata

Assignees

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