Learning center

Which fields in a SARIF result show that the finding is worth fixing?

The code flow decides it: each branch step in codeFlows is a condition you can check against real inputs. The ruleId and level fields only say which check fired and how the build was configured. Read what is missing too, because a result with no partialFingerprints or baselineState cannot tell you whether it is new.

Start from what each field can establish#

A SARIF 2.1.0 result is a small object, and its fields carry different kinds of evidence. ruleId names the check. level is the tool's severity for that check. locations names the line where the problem shows. codeFlows is the only field that explains why the tool believes the problem happens: the SARIF 2.1.0 specification says it "is intended for use by analysis tools that provide execution path details that illustrate a possible problem in the code."

The fixture below is constructed (includes and the struct definition trimmed). buf_load frees its buffer when the input is longer than 64 bytes and returns an error, and the caller frees the same buffer again on any error:

int buf_load(struct buf *b, const char *src)
{
  b->len = strlen(src);
  b->data = malloc(b->len + 1);
  if (!b->data)
    return -1;
  memcpy(b->data, src, b->len + 1);
  if (b->len > 64) {
    free(b->data);          /* too long: give up */   // line 14
    return -1;
  }
  return 0;
}

void buf_release(struct buf *b)
{
  free(b->data);                                      // line 22
}

int main(int argc, char **argv)
{
  struct buf b;
  if (buf_load(&b, argc > 1 ? argv[1] : "short") != 0)
    buf_release(&b);        /* caller cleans up on failure */
  else
    buf_release(&b);
  return 0;
}

GCC 13.3.0 writes SARIF with gcc -fanalyzer -fdiagnostics-format=sarif-file -c buf.c, which produces buf.c.sarif. The result object, trimmed of its location details:

{
  "ruleId": "-Wanalyzer-double-free",
  "taxa": [{ "id": "415", "toolComponent": { "name": "cwe" } }],
  "level": "warning",
  "message": { "text": "double-'free' of 'b.data'" },
  "locations": [{ "physicalLocation": { "artifactLocation": { "uri": "buf.c" },
                  "region": { "startLine": 22, "startColumn": 3, "endColumn": 16 } } }],
  "codeFlows": [{ "threadFlows": [{ "locations": [ ... 16 steps ... ] }] }]
}

The 16 thread flow steps, printed by a short script that reads each step's line, kinds and message:

step  1 line  25 enter,function   entry to 'main'
step  2 line  28 call,function    calling 'buf_load' from 'main'
step  3 line   6 enter,function   entry to 'buf_load'
step  4 line   9 acquire,memory   allocated here
step  5 line  10                  assuming 'b.data' is non-NULL
step  6 line  10 branch,false     following 'false' branch...
step  7 line  12 branch,false     ...to here
step  8 line  13 branch,true      following 'true' branch...
step  9 line  14 branch,true      ...to here
step 10 line  14 release,memory   first 'free' here
step 11 line  28 return,function  returning to 'main' from 'buf_load'
step 12 line  28 branch,true      following 'true' branch...
step 13 line  29 branch,true      ...to here
step 14 line  29 call,function    calling 'buf_release' from 'main'
step 15 line  20 enter,function   entry to 'buf_release'
step 16 line  22 danger           second 'free' here; first 'free' was at (10)

Read the branch steps as preconditions#

The primary location, line 22, is an ordinary free. The decision rests on the branch steps. Step 5 assumes malloc succeeded, step 8 takes b->len > 64 as true, and step 12 takes the error return. GCC's thread flow locations carry no state property, so the step records which way a branch went but not the value that sent it there; read the condition from the source line. Here the preconditions reduce to one question: can a caller pass a string longer than 64 bytes? main passes argv[1], so yes. Built with -fsanitize=address and run with an 80-character argument, the program stops where the flow said it would:

ERROR: AddressSanitizer: attempting double-free on 0x... in thread T0:
    #0 0x... in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:52
    #1 0x... in buf_release buf.c:22

Run with short, the same binary exits 0. The finding is real for exactly the inputs that step 8 named.

Treat ruleId and level as build facts#

level reads as severity, but in this file it reports how the compiler was invoked. The specification defines "warning" as "The rule specified by ruleId was evaluated and a problem was found." GCC emits analyzer diagnostics as compiler warnings, so without -Werror its analyzer results say warning. Adding -Werror to the same command on the same file changes two fields of the result and nothing else in it (the rule's id in the tool's rule list changes with them):

-Werror=analyzer-double-free error line 22 | double-'free' of 'b.data'

The rule identifier changed from -Wanalyzer-double-free to -Werror=analyzer-double-free, and the level from warning to error. Neither change says anything about the bug. It matters for tracking, because GitHub's SARIF support reference states that "The ruleId for a result has to be the same across analysis." A build that adds -Werror turns a known finding into one that looks new. The taxa entry (CWE 415, double free) classifies the rule, not this instance.

Read the fields that are absent#

The GCC result has no kind, baselineState or partialFingerprints (nor fingerprints or rank). Each of the first three absences bears on the decision.

  • kind: the specification says "If kind is absent, it SHALL default to "fail"." The result claims a problem, not a pass or an open question.
  • baselineState: the specification defines it as "a string that specifies the state of this result with respect to some previous run". Without it, this file cannot say whether the double free arrived with your change or has been there for years. You get that answer by running the analyzer on the base commit and comparing.
  • partialFingerprints: GitHub uses it to match results across runs. Its reference says that when a file uploaded with the upload-sarif action lacks this data, GitHub "attempts to populate the partialFingerprints field from the source files". Those values come from the platform, so an alert that reappears after a file moves raises a matching question and adds no evidence about the bug.

When the flow takes a branch the program cannot take#

A code flow is evidence only if every branch it takes is possible. GCC's invoke.texi for 13.3.0 says the analyzer "is neither sound nor complete: it can have false positives and false negatives", and that by default it checks "that the conditions that hold are not mutually exclusive." In the fixture below, a contradiction gets through that check. This constructed function tests the same fact twice, spelled two ways:

int store(unsigned n, int value)
{
  int *slot = NULL;
  if (n % 2 == 0)
    slot = &slots[0];
  printf("n=%u\n", n);
  if ((n & 1) == 0)
    *slot = value;          /* same test as n % 2 == 0 */   // line 12
  return slot != NULL;
}

GCC 13.3.0 reports -Wanalyzer-null-dereference on line 12, and the flow shows the contradiction:

-Wanalyzer-null-dereference warning line 12 | dereference of NULL 'slot'
step  1 line   7 release,memory  'slot' is NULL
step  2 line   8 branch,false    following 'false' branch...
step  3 line  10 branch,false    ...to here
step  4 line  11 branch,true     following 'true' branch...
step  5 line  12 branch,true     ...to here
step  6 line  12 danger          dereference of NULL 'slot'

Step 2 needs n % 2 == 0 to be false and step 4 needs (n & 1) == 0 to be true. For an unsigned n those are the same test. A program that tries every value settles it:

$ ./check_parity
values checked: 4294967296, mismatches: 0

The fixture built with -fsanitize=address,undefined runs store for n from 0 to 3 and exits 0. The result has the same shape, level and rule family as the real one above. Only reading its branch steps against the code separates them. Dismiss it with the contradicting pair of steps as the recorded reason, and rewrite the second test as if (slot) so the next run does not raise it again.

Decide from the flow, then from the history#

What the result saysEvidence to collectDecision
codeFlows with branch stepsFor each branch step, an input or caller that makes that condition holdFix if all hold together; reproduce with a sanitizer or test when you can
Two branch steps that test the same fact with opposite outcomesA proof or exhaustive check that the conditions cannot both holdDismiss with the two step numbers as the reason; consider rewriting the condition
level of error or warningThe build flags that produced the SARIFDo not rank by it alone; compare ruleId across runs with the same flags
No codeFlowsThe rule's documentation and the primary lineTreat it as a pattern finding and check the line directly
No baselineState or fingerprintsAn analyzer run on the base commitDecide whether your change introduced it before assigning it to the change

When a tool's report reaches you as a review comment instead of SARIF, the guide to validating an AI review finding covers that evidence. The guide to pattern-match and data-flow findings covers results that arrive without a code flow at all.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading