Skip to content

EXP39-C: False positives related to compatible types, harmless casts #1200

Description

@MichaelRFairhurst

Affected rules

  • EXP39-C

Description

First off, our implementation uses a compatibleTypes predicate that is incomplete and doesn't handle typedefs, and should be replaced with our common/types/Compatible.qll library. This results in thousands of false positives in certain codebases, usually due to casting to a project-specific typedef of a byte like GLBYTE*.

Secondly, while we use dataflow to trace realloc's that flow to a deref without a memset, we don't use dataflow to handle the more typical case of A* cast to B* followed by a deref. Instead, we have a type representing casted expressions that's both a sink and a source. (We check compatibility of the pointers in the cast in the select statement). This isn't strictly UB unless the pointer is dereferenced, and I do see examples of this in real code. As a related example, casts to void** are often flagged while void* is excluded. This isn't a rare exceptional use of void, it's really just a harmless cast.

Example

typedef char BYTE;
void example_function() {
  int x;
  (long *) &x; // harmless since it's unused
  BYTE *x_bytes = (BYTE *)&x; // Allowed since BYTE is a char.
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty-MediumA false positive or false negative report which is expected to take 1-5 days effort to addressImpact-MediumStandard-CERT-Cfalse positive/false negativeAn issue related to observed false positives or false negatives.false-positiveAn issue representing a false positive report.

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions