Commit bb576ba
authored
Lexer: Fix possible OOB read in quoted strings (#374)
## Summary
Fixes an out-of-bounds string access in
`WP_MySQL_Lexer::read_quoted_text()` that produces PHP warnings when
lexing unclosed quoted strings with trailing backslashes:
```
Uninitialized string offset N in class-wp-mysql-lexer.php on line 2855
```
This appears to happen in streaming SQL processing (e.g., WordPress
Playground's `runSql` blueprint step) when a buffer boundary splits a
quoted string literal and a backslash falls at the end of the buffer.
Standard MySQL dumps with escaped string literals contain thousands of
backslashes, making this likely to hit in practice.
## The bug
The backslash-counting loop in `read_quoted_text()` ran **before** the
EOF check. When `strcspn()` reached the end of the string without
finding a closing quote:
1. `$at` pointed to `strlen($sql)` (one past the last byte).
2. The backslash loop accessed `$this->sql[$at - 1]` — valid, but if it
was `\`:
3. The loop treated the absent quote as escaped and did `$at += 1` (now
past end).
4. Next iteration: `strcspn` returned 0, `$at` stayed past end.
5. The backslash loop accessed `$this->sql[strlen($sql)]` — **out of
bounds**.
## Fix
Two changes to the `while (true)` loop body:
1. **Move the EOF check before the backslash-counting loop.** When
`strcspn` reaches end-of-string without finding the quote,
`$this->sql[$at] ?? null` won't match `$quote`, so we return `null`
immediately — the backslash loop is never reached.
2. **Add a lower-bound guard to the backslash loop.** The `for`
condition now includes `($at - $i - 1) >= 0` to prevent underflow when a
quote appears near the start of the string. Belt-and-suspenders — the
EOF reorder already prevents the primary bug.
## Tests
- Unclosed strings with odd/even trailing backslashes (single and double
quotes).
- Regression tests for valid escaped strings, doubled quotes, and
backtick identifiers.
- Chunk boundary test simulating a streaming SQL processor splitting
input at a backslash inside a quoted string.
## Use of AI
This was diagnosed and implemented with the help of Claude Code.1 parent eb3146a commit bb576ba
2 files changed
Lines changed: 115 additions & 8 deletions
File tree
- packages/mysql-on-sqlite
- src/mysql
- tests/mysql
Lines changed: 6 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2825 | 2825 | | |
2826 | 2826 | | |
2827 | 2827 | | |
2828 | | - | |
2829 | | - | |
2830 | 2828 | | |
2831 | 2829 | | |
2832 | 2830 | | |
| |||
2842 | 2840 | | |
2843 | 2841 | | |
2844 | 2842 | | |
| 2843 | + | |
| 2844 | + | |
| 2845 | + | |
| 2846 | + | |
| 2847 | + | |
2845 | 2848 | | |
2846 | 2849 | | |
2847 | 2850 | | |
| |||
2852 | 2855 | | |
2853 | 2856 | | |
2854 | 2857 | | |
2855 | | - | |
| 2858 | + | |
2856 | 2859 | | |
2857 | 2860 | | |
2858 | 2861 | | |
2859 | 2862 | | |
2860 | 2863 | | |
2861 | 2864 | | |
2862 | | - | |
2863 | | - | |
2864 | | - | |
2865 | | - | |
2866 | | - | |
2867 | 2865 | | |
2868 | 2866 | | |
2869 | 2867 | | |
| |||
Lines changed: 109 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
258 | 258 | | |
259 | 259 | | |
260 | 260 | | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
261 | 370 | | |
262 | 371 | | |
263 | 372 | | |
| |||
0 commit comments