Commit e217071
authored
Fix TString::FormImp buffer safety and va_list handling (#22228)
As reported here: https://github.com/root-project/root/security/code-scanning/1843
- Fixes #22218
- Fixes https://github.com/root-project/root/security/code-scanning/1843
TString::FormImp used a heuristic buffer size and passed an assumed
length to vsnprintf, which static analyzers could not prove matched
the actual allocated buffer. In addition, the same va_list was reused
across multiple vsnprintf calls, resulting in undefined behavior on
some platforms.
The implementation was rewritten to use a two‑pass vsnprintf approach:
the first pass computes the exact required length, and Clobber() is
used to allocate sufficient space including the null terminator.
A second pass formats the string into the allocated buffer using a
fresh va_list copy.
This change:
- Guarantees that the size passed to vsnprintf matches the allocated buffer
- Eliminates undefined behavior from va_list reuse
- Removes heuristic resizing loops
- Silences static analysis warnings for legitimate reasons
- Preserves existing TString semantics and limits1 parent 7142ae7 commit e217071
1 file changed
Lines changed: 24 additions & 23 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2315 | 2315 | | |
2316 | 2316 | | |
2317 | 2317 | | |
2318 | | - | |
2319 | | - | |
| 2318 | + | |
| 2319 | + | |
2320 | 2320 | | |
2321 | | - | |
2322 | | - | |
| 2321 | + | |
| 2322 | + | |
| 2323 | + | |
2323 | 2324 | | |
2324 | | - | |
2325 | | - | |
2326 | | - | |
2327 | | - | |
2328 | | - | |
2329 | | - | |
2330 | | - | |
2331 | | - | |
2332 | | - | |
2333 | | - | |
2334 | | - | |
2335 | | - | |
2336 | | - | |
2337 | | - | |
2338 | | - | |
| 2325 | + | |
| 2326 | + | |
| 2327 | + | |
| 2328 | + | |
2339 | 2329 | | |
2340 | | - | |
2341 | | - | |
2342 | | - | |
2343 | 2330 | | |
2344 | | - | |
| 2331 | + | |
| 2332 | + | |
| 2333 | + | |
| 2334 | + | |
| 2335 | + | |
| 2336 | + | |
| 2337 | + | |
| 2338 | + | |
| 2339 | + | |
| 2340 | + | |
| 2341 | + | |
| 2342 | + | |
| 2343 | + | |
| 2344 | + | |
| 2345 | + | |
2345 | 2346 | | |
2346 | 2347 | | |
2347 | 2348 | | |
| |||
0 commit comments