mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-10-26 22:10:45 +00:00
[P163] Check sensor pointer before use
isInitialized() now also requires a valid sensor pointer, so plugin_read() and plugin_write() never dereference a null sensor. setOutputValues() checks it as well, and init() deletes a previous sensor object before allocating a new one, so calling it again does not leak. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
646a95bf5b
commit
d1e06e5507
2 files changed
+5
-1
No files matched your search
@@ -25,6 +25,8 @@ P163_data_struct::~P163_data_struct() {
|
||||
* init
|
||||
*****************************************************/
|
||||
bool P163_data_struct::init(struct EventStruct *event) {
|
||||
initialized = false;
|
||||
delete sensor; // In case init() is called again
|
||||
sensor = new (std::nothrow) CG_RadSens(RS_DEFAULT_I2C_ADDRESS);
|
||||
|
||||
if ((nullptr != sensor) && sensor->init()) {
|
||||
@@ -60,6 +62,8 @@ bool P163_data_struct::plugin_read(struct EventStruct *event) {
|
||||
bool P163_data_struct::setOutputValues(struct EventStruct *event) {
|
||||
bool result = false;
|
||||
|
||||
if (nullptr == sensor) { return result; }
|
||||
|
||||
const uint32_t count = _readIncrement ? sensor->getNumberOfPulses() : sensor->getNumberOfNewPulses();
|
||||
const uint32_t incCount = sensor->getNumberOfPulses();
|
||||
const float iDynamic = sensor->getRadIntensyDynamic();
|
||||
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
bool plugin_write(struct EventStruct *event,
|
||||
String & string);
|
||||
bool isInitialized() const {
|
||||
return initialized;
|
||||
return initialized && (nullptr != sensor);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Reference in new issue
Block a user