Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: REM Cost Reform

...

However, the C Standard, 6.2.4 paragraphs 4 and 5 [ISO/IEC 9899:20112024], states:

The result of attempting to indirectly access an object with thread storage duration from a thread other than the one with which the object is associated is implementation-defined. . . .

The result of attempting to indirectly access an object with automatic storage duration from a thread other than the one with which the object is associated is implementation-defined.

...

Code Block
bgColor#ccccff
langc
#include <threads.h>
#include <stdio.h>
#include <stdlib.h>

static tss_t key;

int child_thread(void *v) {
  int *result = v;
  printf("Result: %d\n", *result); /* Correctly prints 1 */
  return 0;
}

int create_thread(void *thrd) {
  int *val = (int *)malloc(sizeof(int));
  if (val == NULL) {
    /* Handle error */
  }
  *val = 1;
  if (thrd_success != tss_set(key, val)) {
    /* Handle error */
  }
  /* ... */
  void *v = tss_get(key);
  if (thrd_success != thrd_create((thrd_t *)thrd,
                                   child_thread, v)) {
    /* Handle error */
  }
  return 0;
}

int main(void) {
  thrd_t parent_tid, child_tid;

  if (thrd_success != tss_create(&key, free)) {
  /* Handle error */
  }
  if (thrd_success != thrd_create(&parent_tid, create_thread,
                                  &child_tid)) {
    /* Handle error */
  }
  if (thrd_success != thrd_join(parent_tid, NULL)) {
    /* Handle error */
  }
  if (thrd_success != thrd_join(child_tid, NULL)) {
    /* Handle error */
  }
  tss_delete(key);
return 0;
} 

This compliant solution uses pointer-to-integer and integer-to-pointer conversions, which have implementation-defined behavior. (See INT36-C. Converting a pointer to integer or integer to pointer.)

Compliant Solution (Thread-Local Storage, Windows, Visual Studio)

...

Recommendation

Severity

Likelihood

Detectable

Remediation CostRepairable

Priority

Level

CON34-C

Medium

Probable

No

HighNo

P4

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

Supported, resulting undefined behavior is reported by the runtime error analysis
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

CONCURRENCY.LOCALARG
CONCURRENCY.C_THREAD.ISD

Local Variable Passed to Thread
Inappropriate Storage Duration
Cppcheck Premium

Include Page
Cppcheck Premium_V
Cppcheck Premium_V

premium-cert-con34-c
Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

DF4926, DF4927, DF4928
Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-CON34-a

Declare objects shared between POSIX threads with appropriate storage durations
Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rule CON34-CChecks for automatic or thread local variable escaping from a C11 thread (rule fully covered)

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...

Bibliography

...