`CON34-C`: Implementation seems to check incorrect aspects of thread storage duration objects

Open
#801 1 comment 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #778 by @lcartey — merged

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
c
Domain
tooling

Research direction

Start with the CON34-C implementation and its existing validation for tss_t values, then compare it with the compliant and non-compliant examples in this issue. The change is done when the intended thread-storage violation is reported and unrelated uninitialized-value cases are not attributed to CON34-C; validate with the rule's available tests.

Written by the indexing model from the issue text.

Description

false positive/false negative Standard-CERT-C
Affected rules
  • CON34-C
Description

The documentation seems to describe the risk of passing a tss_t into a thread. The risk here is that the new thread will have no value.

In the "compliant" case it says the appropriate fix is to use tss_get() to get the value in the current thread, then pass that value into the new thread.

In our implementation, we check that any tss_t values are definitely given a value before retrieved and passed into a thread creation statement. This is not the intention of the rule, adds additional implementation complexity, and reveals no violations in MRVA.

Example
tss_t key;
void f1() {
  // Should be marked non-compliant: new thread has no value for tss_get(key)
  // Currently not reported
  thrd_create(..., &key);
}

void f2() {
  // While the following code is suspect, it is NOT the intention of the rule is not to disallow this:
  tss_t localkey;
  tss_set(localkey, malloc(...));
  void* v = tss_get(localkey);
  thrd_create(..., v);

  // The fact that v is uninitialized has nothing to do with threads. For instance, this is also erroneous:
  printf("%d", *v);

  // Further, the above code has nothing to do with thread storage duration, and isn't necessarily invalid:
  static void *shared_buf = malloc(...);
  thrd_create(..., shared_buf); // Reasonable pattern
}
Dominant language
CodeQL
Stars
227
Forks
82
Avg merge
6d 7h
Merged PRs (30d)
9

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from github/codeql-coding-standards

All issues in github/codeql-coding-standards

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.