JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries


CONCURRENCY.LOCALARG : Local Variable Passed to Thread

Summary

A local variable is used as an argument for a thread.

Properties

Class Name Local Variable Passed to Thread
Significance reliability
Mnemonic CONCURRENCY.LOCALARG
Categories
MisraC++2023 MisraC++2023:0.3.2 A function call shall not violate the function's preconditions
CERT-C CERT-C:CON31-C Do not destroy a mutex while it is locked
  CERT-C:CON34-C Declare objects shared between threads with appropriate storage durations
CERT-CPP CERT-CPP:CON50-CPP Do not destroy a mutex while it is locked
Availability Available for C and C++.
Enabling Checks for this warning class are enabled by default. To disable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += discard class="Local Variable Passed to Thread"

Example

#include <pthread.h>

#define NUM_THREADS 5

void *perform_work(void *arguments){
  /* do something with arguments */
  return NULL;
}

int main(void) {
    pthread_t threads[2*NUM_THREADS];
    int local_thread_args[NUM_THREADS];              /* automatic storage duration */
    static int static_thread_args[NUM_THREADS];      /* static storage duration */
    int i;
    int result_code;

    /* pass local data to some threads */
    for (i = 0; i < NUM_THREADS; i++) {
        local_thread_args[i] = i;
        result_code = pthread_create(&threads[2*i], NULL, perform_work, &local_thread_args[i]); /* 'Local Variable Passed to Thread' warning issued here */
        if (result_code != 0) {return result_code;}
    }

    /* pass local-static data to some threads */
    for (i = 0; i < NUM_THREADS; i++) {
        static_thread_args[i] = i;
        result_code = pthread_create(&threads[2*i+1], NULL, perform_work, &static_thread_args[i]);          /* ok: static_thread_args has static storage duration */
        if (result_code != 0) {return result_code;}
    }

    /* wait for threads to complete */
    for (i = 0; i < 2*NUM_THREADS; i++) {
        result_code = pthread_join(threads[i], NULL);
        if (result_code != 0) {return result_code;}
    }

    return 0;
}

Triggering Functions

CodeSonar ships with library models that allow it to recognize a number of functions that pass arguments to threads, across many different libraries. Some examples are shown in the table below. If one of these functions is passed a local variable, a warning will be issued.

If you have created a custom library model for some function f() in terms of one of these existing models, calls to f() will also be capable of triggering Local Variable Passed to Thread warnings.

Functions that can trigger warnings include...
Apache Portable Runtime (APR) apr_thread_create()
ARINC 653 CREATE_PROCESS()
CMX-RTX K_Task_Create()
FreeRTOS xTaskCreate()
libc g_thread_create()
Linux Kernel kernel_thread()
Mac OS X kernel_thread_start()
Win32/MFC AfxBeginThread()
Netscape Portable Runtime (NSPR) PR_CreateThread()
Qt QThread::start()
ThreadX tx_thread_create()
uC/OS-III OSTaskCreate()
VxWorks taskInit()
Win32 CreateThread()
wxWidgets wxThread::Create()

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.