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 |
A local variable is used as an argument for a thread.
| Class Name | Local Variable Passed to Thread | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||||||||
| Mnemonic | CONCURRENCY.LOCALARG | ||||||||||||
| Categories |
|
||||||||||||
| 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" |
#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;
}
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() |
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.