Missing or double initialization of thread attribute (BAD_THREAD_ATTRIBUTE)
R2026bDuplicated initialization of thread attributes or noninitialized thread attribute used in functions that expect initialized attributes
Description
This defect occurs during one of these situations:
You initialize a thread attribute twice with a function such as
pthread_attr_initwithout an intermediate call to a function such aspthread_attr_destroy.The function
pthread_attr_destroydestroys a thread attribute object and enables the system to reclaim resources associated with the object.You use a noninitialized thread attribute in a function such as
pthread_create, which expects an initialized attribute. A thread attribute might be noninitialized because it was never initialized previously or destroyed with thepthread_attr_destroyfunction.Noninitialized thread attributes are detected for all functions in the POSIX® standard.
The Result Details pane describes whether the attribute is doubly initialized or noninitialized and also shows previous related events.
Note that a thread attribute is considered initialized only if the call to
pthread_attr_init is successful. For instance, the thread attribute is
not initialized in the if branch
here:
pthread_attr_t attr;
int thread_success;
thread_success = pthread_attr_init(&attr);
if(thread_success != 0) {
/* Thread attribute considered noninitialized */
}pthread_attr_init.Risk
Initializing a thread attribute without destroying the previously initialized attribute or using noninitialized thread attributes leads to undefined behavior.
Fix
Before using a thread attribute, initialize the attribute by using the pthread_attr_init function.
pthread_attr_t attr;
int thread_success;
/* Initialize attribute */
thread_success = pthread_attr_init(&attr);
if(thread_success != 0) {
/* Handle initialization error */
}
...
/* Use attribute */
thread_success = pthread_create(&thr, &attr, &thread_start, NULL);pthread_attr_destroy before initializing again:pthread_attr_t attr;
int thread_success;
/* Destroy attribute */
thread_success = pthread_attr_destroy(&attr);
if(thread_success != 0) {
/* Handle destruction error */
}
...
/* Reinitialize attribute */
thread_success = pthread_attr_init(&attr);Examples
Result Information
| Group: Concurrency |
| Language: C |
| Default: Off |
Command-Line Syntax:
BAD_THREAD_ATTRIBUTE
|
| Impact: Medium |
PQL Name:
std.defects.BAD_THREAD_ATTRIBUTE
|
Version History
Introduced in R2019b
See Also
Use of undefined thread
ID | Join or detach of a joined or detached
thread
Topics
- Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Polyspace Results Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)