Skip to content

Bug: Missing result validation before JSON parsing in getPowerLevel #173

@jfreeda

Description

@jfreeda

Problem/Opportunity

Problem

The method getPowerLevel attempts to parse the result of _performCommand("emeter", json=True) using json.loads() without validating whether the result is a non-empty, well-formed JSON string.
If the command returns an empty string or malformed output—due to device issues, timeouts, or communication errors—this leads to an unhandled json.decoder.JSONDecodeError.
This lack of validation can cause the application to crash unexpectedly during runtime, especially in environments where device responses are unreliable or intermittent.

Steps to reproduce

While running L4 WiFi Ethernet suite issue, we are able to reproduce it with Windows environment
No response

Expected Behavior

  • Safely handle cases where the command result is empty, null, or not valid JSON.
  • Validate the response before attempting to parse it with json.loads().
  • Raise a meaningful and descriptive exception (e.g., ValueError) if the result is invalid or cannot be parsed.
  • Only proceed with extracting the power_mw value if the JSON is successfully parsed and contains the expected key.

Actual Behavior

Currently, the getPowerLevel method directly attempts to parse the result of _performCommand("emeter", json=True) using json.loads() without validating the response. If the command returns an empty string or malformed JSON (e.g., due to a device error or communication failure), the method raises an unhandled exception:
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Notes (Optional)

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions