Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,10 +1233,13 @@ if (!open_i2c_sensor_fd(i2c_adapter_nr))
}



}

}

/* All buses probed, nothing answered. Without this the function fell off
* the end (UB): the bool came back indeterminate — often true — and the
* caller then formatted an uninitialised ctx into a garbage name. */
return false;
}

#ifndef STANDALONE_LIBRARY
Expand Down Expand Up @@ -1275,20 +1278,36 @@ cJSON *detect_sensors() {

#endif

/* getsensorid() drives i2c detection through the process-global i2c_adapter_nr
* and both entry points below format into one shared sensor_indentity buffer,
* none of it locked. Two threads calling these at once stomp the adapter number
* mid-probe (wrong bus -> detection fails -> garbage) and tear the buffer write.
* Serialise the whole detect-and-format so every returned value is one complete,
* valid string. */
static pthread_mutex_t sensor_indentity_mtx = PTHREAD_MUTEX_INITIALIZER;
static char sensor_indentity[16];
const char *getsensoridentity() {
pthread_mutex_lock(&sensor_indentity_mtx);
Comment thread
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
sensor_ctx_t ctx;
if (!getsensorid(&ctx))
return NULL;
lsnprintf(sensor_indentity, sizeof(sensor_indentity), "%s_%s",
ctx.sensor_id, ctx.control);
return sensor_indentity;
const char *ret = NULL;
if (getsensorid(&ctx)) {
lsnprintf(sensor_indentity, sizeof(sensor_indentity), "%s_%s",
ctx.sensor_id, ctx.control);
ret = sensor_indentity;
}
pthread_mutex_unlock(&sensor_indentity_mtx);
return ret;
}

const char *getsensorshort() {
pthread_mutex_lock(&sensor_indentity_mtx);
sensor_ctx_t ctx;
if (!getsensorid(&ctx))
return NULL;
lsnprintf(sensor_indentity, sizeof(sensor_indentity), "%s", ctx.sensor_id);
return sensor_indentity;
const char *ret = NULL;
if (getsensorid(&ctx)) {
lsnprintf(sensor_indentity, sizeof(sensor_indentity), "%s",
ctx.sensor_id);
ret = sensor_indentity;
}
pthread_mutex_unlock(&sensor_indentity_mtx);
return ret;
}