edgexfoundry / edgexfoundry/device-sdk-c
onChangeThreshold crashes on mixed-type readings
- Dominant language
- C
- Stars
- 45
- Forks
- 55
- PR merge metrics
- No merged PRs in 30d
Description
# 🐞 Bug Report
### Affected Services [**REQUIRED**]
device-sdk-c (AutoEvent onChangeThreshold implementation)
### Is this a regression?
Unknown - appears to be a latent bug in the `values_exceed_threshold` function that would trigger when comparing mixed-type readings (numeric vs non-numeric) with `onChangeThreshold > 0`.
### Description and Minimal Reproduction [**REQUIRED**]
Sometimes the value of a parameter can be undefined caused by communication issues, this causes the code to crash, as only on the them needs to be true.
**Location:** `src/c/autoevent.c:61`
**Bug:** Incorrect logical operator causes null pointer dereference when comparing values of mixed types.
**Current code (WRONG):**
```c
if(curr_val_cast != NULL || prev_val_cast != NULL) // Line 61 - OR operator
{
double curr_val = iot_data_f64(curr_val_cast); // Line 63 - crashes if NULL
double prev_val = iot_data_f64(prev_val_cast); // Line 64 - crashes if NULL
```
**Fix:**
```c
if(curr_val_cast != NULL && prev_val_cast != NULL) // AND operator
```
**Reproduction:**
1. Configure AutoEvent with `onChangeThreshold > 0`
2. Device resource returns multiple readings with mixed types:
- Reading A: numeric (e.g., `42.5`)
- Reading B: non-numeric (e.g., `"ACTIVE"`)
3. Trigger AutoEvent
4. `values_exceed_threshold()` attempts `iot_data_f64(NULL)` → crash or undefined behavior
**Expected behavior:** Non-numeric values should fall through to `iot_data_equal()` comparison at line 72.
**Actual behavior:** Undefined behavior when calling `iot_data_f64()` with NULL pointer.
## 🔥 Exception or Error
Segmentation fault or undefined behavior when iot_data_f64(NULL) is called.
Exact error depends on iot_data_f64() implementation's null handling.
## 🌍 Your Environment
**Deployment Environment:** n/a
**EdgeX Version [**REQUIRED**]:** Latest main branch (commit 3f0e136)
**Anything else relevant:**
The else branch at line 71 correctly handles non-numeric comparisons using `iot_data_equal()`, but it's unreachable when values are of mixed types due to the OR operator bug.
Contributor guide
Assessment
This issue has not been assessed yet.