Fix Coverity Scan #1619415

Data race in pthread_cond_timedwait. Unreleased, non-critical
This commit is contained in:
Roger A. Light
2025-08-22 12:55:16 +01:00
parent 8bdb6c69ef
commit 422ea6ed26
2 changed files with 15 additions and 11 deletions
+14 -11
View File
@@ -139,16 +139,22 @@ bool ctrl_shell_get_password(char *buf, size_t len)
return true;
}
static int response_wait(bool unlock)
{
struct timespec timeout;
int rc = 0;
data.response_received = false;
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 2;
if(pthread_cond_timedwait(&data.response_cond, &data.response_mutex, &timeout) == ETIMEDOUT){
ctrl_shell_printf("Timed out with no response.\n");
rc = 1;
while(data.response_received == false){
if(pthread_cond_timedwait(&data.response_cond, &data.response_mutex, &timeout) == ETIMEDOUT){
ctrl_shell_printf("Timed out with no response.\n");
rc = 1;
break;
}
}
if(unlock){
pthread_mutex_unlock(&data.response_mutex);
@@ -291,14 +297,7 @@ static int ctrl_shell__subscribe_blocking(const char *topic, void (*module_on_su
mosquitto_subscribe(data.mosq, NULL, topic, 1);
struct timespec timeout;
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += 2;
if(pthread_cond_timedwait(&data.response_cond, &data.response_mutex, &timeout) == ETIMEDOUT){
ctrl_shell_printf("Subscribe timed out with no response.\n");
rc = 1;
}
pthread_mutex_unlock(&data.response_mutex);
response_wait(true);
if(data.subscribe_rc >= 128){
rc = 1;
@@ -493,6 +492,7 @@ void ctrl_shell__on_connect(struct mosquitto *mosq, void *userdata, int rc)
data.connect_rc = rc;
data.response_received = true;
pthread_mutex_unlock(&data.response_mutex);
pthread_cond_signal(&data.response_cond);
}
@@ -519,6 +519,7 @@ void ctrl_shell__on_message(struct mosquitto *mosq, void *userdata, const struct
}
cJSON_Delete(j_tree);
data.response_received = true;
pthread_mutex_unlock(&data.response_mutex);
pthread_cond_signal(&data.response_cond);
}
@@ -535,6 +536,7 @@ void ctrl_shell__on_publish(struct mosquitto *mosq, void *userdata, int mid, int
data.publish_rc = reason_code;
}
data.response_received = true;
pthread_mutex_unlock(&data.response_mutex);
pthread_cond_signal(&data.response_cond);
}
@@ -550,6 +552,7 @@ void ctrl_shell__on_subscribe(struct mosquitto *mosq, void *userdata, int mid, i
data.subscribe_rc = granted_qos[0];
}
data.response_received = true;
pthread_mutex_unlock(&data.response_mutex);
pthread_cond_signal(&data.response_cond);
}
@@ -38,6 +38,7 @@ struct ctrl_shell{
rl_vcpfunc_t *line_callback;
pthread_cond_t response_cond;
pthread_mutex_t response_mutex;
bool response_received;
const char *request_topic;
struct completion_tree_root *commands;
void (*response_callback)(const char *command, cJSON *data, const char *payload);