[multiple] Add division by zero guards (#14634)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick+github@koston.org>
This commit is contained in:
Jonathan Swoboda
2026-03-09 00:10:48 -04:00
committed by GitHub
parent f3ca86b670
commit 0db9137d91
8 changed files with 38 additions and 7 deletions

View File

@@ -173,7 +173,7 @@ void BL0942::received_package_(DataPacket *data) {
float i_rms = (uint24_t) data->i_rms / current_reference_;
float watt = (int24_t) data->watt / power_reference_;
float total_energy_consumption = cf_cnt / energy_reference_;
float frequency = 1000000.0f / data->frequency;
float frequency = data->frequency != 0 ? 1000000.0f / data->frequency : NAN;
if (voltage_sensor_ != nullptr) {
voltage_sensor_->publish_state(v_rms);

View File

@@ -163,7 +163,7 @@ void MeanCombinationComponent::handle_new_value(float value) {
return;
float sum = 0.0;
size_t count = 0.0;
size_t count = 0;
for (const auto &sensor : this->sensors_) {
if (std::isfinite(sensor->state)) {
@@ -172,6 +172,10 @@ void MeanCombinationComponent::handle_new_value(float value) {
}
}
if (count == 0) {
this->publish_state(NAN);
return;
}
float mean = sum / count;
this->publish_state(mean);
@@ -238,6 +242,11 @@ void RangeCombinationComponent::handle_new_value(float value) {
}
}
if (sensor_states.empty()) {
this->publish_state(NAN);
return;
}
sort(sensor_states.begin(), sensor_states.end());
float range = sensor_states.back() - sensor_states.front();

View File

@@ -171,7 +171,7 @@ void Graph::draw(Display *buff, uint16_t x_offset, uint16_t y_offset, Color colo
bool prev_b = false;
int16_t prev_y = 0;
for (uint32_t i = 0; i < this->width_; i++) {
float v = (trace->get_tracedata()->get_value(i) - ymin) / yrange;
float v = yrange != 0 ? (trace->get_tracedata()->get_value(i) - ymin) / yrange : NAN;
if (!std::isnan(v) && (thick > 0)) {
int16_t x = this->width_ - 1 - i + x_offset;
uint8_t bit = 1 << ((i % (thick * LineType::PATTERN_LENGTH)) / thick);

View File

@@ -59,6 +59,12 @@ void MICS4514Component::update() {
return;
}
if (this->red_calibration_ == 0 || this->ox_calibration_ == 0) {
ESP_LOGW(TAG, "Calibration values are zero, retrying");
this->status_set_warning();
this->initial_ = true;
return;
}
float red_f = (float) (power - red) / this->red_calibration_;
float ox_f = (float) (power - ox) / this->ox_calibration_;

View File

@@ -70,6 +70,10 @@ float TSL2561Sensor::calculate_lx_(uint16_t ch0, uint16_t ch1) {
return NAN;
}
if (ch0 == 0) {
ESP_LOGVV(TAG, "No light detected");
return 0.0f;
}
float d0 = ch0, d1 = ch1;
float ratio = d1 / d0;

View File

@@ -1,5 +1,6 @@
#include "esphome/core/log.h"
#include "ufire_ec.h"
#include <cmath>
namespace esphome {
namespace ufire_ec {
@@ -60,9 +61,15 @@ float UFireECComponent::measure_temperature_() { return this->read_data_(REGISTE
float UFireECComponent::measure_ms_() { return this->read_data_(REGISTER_MS); }
void UFireECComponent::set_solution_(float solution, float temperature) {
solution /= (1 - (this->temperature_coefficient_ * (temperature - 25)));
bool UFireECComponent::set_solution_(float solution, float temperature) {
float denom = 1 - (this->temperature_coefficient_ * (temperature - 25));
if (std::abs(denom) < 1e-6f) {
ESP_LOGE(TAG, "Temperature compensation denominator is zero");
return false;
}
solution /= denom;
this->write_data_(REGISTER_SOLUTION, solution);
return true;
}
void UFireECComponent::set_compensation_(float temperature) { this->write_data_(REGISTER_COMPENSATION, temperature); }
@@ -72,7 +79,8 @@ void UFireECComponent::set_coefficient_(float coefficient) { this->write_data_(R
void UFireECComponent::set_temperature_(float temperature) { this->write_data_(REGISTER_TEMP, temperature); }
void UFireECComponent::calibrate_probe(float solution, float temperature) {
this->set_solution_(solution, temperature);
if (!this->set_solution_(solution, temperature))
return;
this->write_byte(REGISTER_TASK, COMMAND_CALIBRATE_PROBE);
}

View File

@@ -44,7 +44,7 @@ class UFireECComponent : public PollingComponent, public i2c::I2CDevice {
protected:
float measure_temperature_();
float measure_ms_();
void set_solution_(float solution, float temperature);
bool set_solution_(float solution, float temperature);
void set_compensation_(float temperature);
void set_coefficient_(float coefficient);
void set_temperature_(float temperature);

View File

@@ -7,6 +7,8 @@ static const uint32_t DELTA = 0x9e3779b9;
#define MX ((((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (k[(p ^ e) & 7] ^ z)))
void encrypt(uint32_t *v, size_t n, const uint32_t *k) {
if (n == 0)
return;
uint32_t z, y, sum, e;
size_t p;
size_t q = 6 + 52 / n;
@@ -25,6 +27,8 @@ void encrypt(uint32_t *v, size_t n, const uint32_t *k) {
}
void decrypt(uint32_t *v, size_t n, const uint32_t *k) {
if (n == 0)
return;
uint32_t z, y, sum, e;
size_t p;
size_t q = 6 + 52 / n;