Lock function without unlock function
This defect occurs when:
A task calls a lock function.
The task ends without a call to an unlock function.
In multitasking code, a lock function begins a critical section
of code and an unlock function ends it. When a task, my_task,
calls a lock function, my_lock, other tasks calling my_lock must
wait until my_task calls the corresponding unlock
function. Polyspace® requires that both lock and unlock functions
must have the form void func(void).
To find this defect, before analysis, you must specify the multitasking options. On the Configuration pane, select Multitasking.
An unlock function ends a critical section so that other waiting tasks can enter the critical section. A missing unlock function can result in tasks blocked for an unnecessary length of time.
Identify the critical section of code, that is, the section that you want to be executed as an atomic block. At the end of this section, call the unlock function that corresponds to the lock function used at the beginning of the section.
There can be other reasons and corresponding fixes for the defect. Perhaps you called the incorrect unlock function. Check the lock-unlock function pair in your Polyspace analysis configuration and fix the mismatch.
See examples of fixes below. To avoid the
issue, you can follow the practice of calling the lock and unlock functions in the
same module at the same level of abstraction. For instance, in this example,
func calls the lock and unlock function at the same level but
func2 does
not.
void func() {
my_lock();
{
...
}
my_unlock();
}
void func2() {
{
my_lock();
...
}
my_unlock();
}If you do not want to fix the issue, add comments to your result or code to avoid another review. See Address Results in Polyspace Access Through Bug Fixes or Justifications.
| Group: Concurrency |
| Language: C | C++ |
| Default: On |
Command-Line Syntax: BAD_LOCK |
| Impact: High |
| CWE ID: 667 |
Data race | Data race including atomic operations | Data race through standard library function call | Deadlock | Destruction of locked mutex | Double lock | Double unlock | Missing lock