Handle malformed session statistics safely
codexaardvarkReview effort 3/5
### **User description**
## Motivation
Malformed or incomplete `session-stats` responses can raise exceptions while
the Statistics tab is updated. In the synchronized RPC refresh path, those
exceptions can unwind the refresh thread and disconnect the client without
being handled as a normal RPC response error.
## Changes
- Require `arguments`, `current-stats`, and `cumulative-stats` to exist as\n JSON objects.
- Validate and convert every displayed field before updating any grid cell.
Byte counters must be finite and non-negative; integer counters must also
be integral and fit the UI's integer range.
- Clear only the Statistics grid when validation fails, then report the
malformed response through the existing RPC status path.
- Surface delayed floating-point failures from JSON number parsing while the
response is still inside the RPC error handler, and release any parsed JSON
object on that path.
- Keep very large byte counts within the highest supported display unit
instead of indexing beyond the unit table.
## Testing
- `lazbuild -B transgui.lpi --lazarusdir=/usr/lib/lazarus/default`
- Targeted validation harness covering valid data, missing objects and fields,
wrong types, fractional and negative counters, integer overflow, huge JSON
integers and exponents, and the 1 PiB display boundary
- `git diff --check`
Manual UI testing was not performed.
___
### **PR Type**
Bug fix
___
### **Description**
- Validate session-statistics fields before grid updates.
- Clear invalid statistics and show one nonfatal error.
- Contain JSON parsing failures within RPC handling.
- Prevent byte-unit overflow at maximum scale.
___
### Diagram Walkthrough
```mermaid
flowchart LR
Response["RPC session-stats response"]
Validation["Validate response and statistics fields"]
StatisticsGrid["Statistics grid"]
ErrorNotice["Nonfatal invalid-response notification"]
Response -- "is checked" --> Validation
Validation -- "valid data updates" --> StatisticsGrid
Validation -- "invalid data clears grid and reports" --> ErrorNotice
```
<details> <summary><h3> File Walkthrough</h3></summary>
<table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Bug fix</strong></td><td><table>
<tr>
<td>
<details>
<summary><strong>main.pas</strong><dd><code>Validate and safely render session statistics</code> </dd></summary>
<hr>
main.pas
<ul><li>Make <code>FillStatistics</code> validate all displayed counters before rendering.<br> <li> Clear and disable only the Statistics grid for missing or malformed <br>data.<br> <li> Allow <code>CheckStatus</code> to display explicit nonfatal response errors.<br> <li> Cap <code>GetHumanSize</code> at the highest supported unit.</ul>
</details>
</td>
<td><a href="https://github.com/transmission-remote-gui/transgui/pull/1565/files#diff-449243edd8ba91d6d43d39c7bb109819a01e7c4e75770a16698291e7a6eb2363">+112/-32</a></td>
</tr>
</table></td></tr><tr><td><strong>Error handling</strong></td><td><table>
<tr>
<td>
<details>
<summary><strong>rpc.pas</strong><dd><code>Contain malformed RPC responses and statistics errors</code> </dd></summary>
<hr>
rpc.pas
<ul><li>Add a <code>SendRequest</code> overload that returns response errors separately.<br> <li> Validate RPC result and <code>arguments</code> response objects before extraction.<br> <li> Safely free parser and decompression resources on parsing failures.<br> <li> Report invalid statistics responses once without disconnecting the <br>client.</ul>
</details>
</td>
<td><a href="https://github.com/transmission-remote-gui/transgui/pull/1565/files#diff-2ed877f1eb0cc61eeb41c8377461565d3043af70aaf9ac97b3a78679d020fa76">+124/-33</a></td>
</tr>
</table></td></tr></tr></tbody></table>
</details>
___
合并状态:未合并 21 条评论