Commit 14cd548
committed
sqlite: fix crash on db.close() from inside a user function
Calling db.close() from inside a user-defined function callback while
sqlite3_step is on the call stack caused two distinct crashes:
1. DatabaseSync::Close ran sqlite3_finalize on the statement whose
sqlite3_step frame was still active, freeing the VM that step was
executing. The outer step then operated on freed memory.
2. Even if (1) is avoided, StatementExecutionHelper::Run dereferenced
db->Connection() via sqlite3_last_insert_rowid / sqlite3_changes64
after step returned. The reentrant close zeroed connection_, so
the deref crashed.
Add a MarkStepping() RAII guard wrapped around every sqlite3_step
caller. If Finalize() is called while stepping_, defer it; the
guard's destructor runs the deferred finalize after step returns.
Add a connection-null check in StatementExecutionHelper::Run before
the connection-dependent reads, throwing ERR_INVALID_STATE.
Fixes: #63180
Signed-off-by: Matthew McEachen <matthew@photostructure.com>1 parent bbf51ad commit 14cd548
3 files changed
Lines changed: 119 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2573 | 2573 | | |
2574 | 2574 | | |
2575 | 2575 | | |
| 2576 | + | |
| 2577 | + | |
| 2578 | + | |
| 2579 | + | |
| 2580 | + | |
| 2581 | + | |
| 2582 | + | |
2576 | 2583 | | |
2577 | 2584 | | |
2578 | 2585 | | |
| |||
2898 | 2905 | | |
2899 | 2906 | | |
2900 | 2907 | | |
| 2908 | + | |
| 2909 | + | |
| 2910 | + | |
| 2911 | + | |
2901 | 2912 | | |
2902 | 2913 | | |
2903 | 2914 | | |
| |||
3026 | 3037 | | |
3027 | 3038 | | |
3028 | 3039 | | |
3029 | | - | |
3030 | | - | |
3031 | 3040 | | |
| 3041 | + | |
| 3042 | + | |
3032 | 3043 | | |
3033 | 3044 | | |
3034 | 3045 | | |
| |||
3076 | 3087 | | |
3077 | 3088 | | |
3078 | 3089 | | |
| 3090 | + | |
3079 | 3091 | | |
3080 | 3092 | | |
3081 | 3093 | | |
| |||
3100 | 3112 | | |
3101 | 3113 | | |
3102 | 3114 | | |
| 3115 | + | |
3103 | 3116 | | |
3104 | 3117 | | |
3105 | 3118 | | |
| |||
3364 | 3377 | | |
3365 | 3378 | | |
3366 | 3379 | | |
| 3380 | + | |
3367 | 3381 | | |
3368 | 3382 | | |
3369 | 3383 | | |
| |||
3435 | 3449 | | |
3436 | 3450 | | |
3437 | 3451 | | |
| 3452 | + | |
3438 | 3453 | | |
3439 | 3454 | | |
3440 | 3455 | | |
| |||
3473 | 3488 | | |
3474 | 3489 | | |
3475 | 3490 | | |
3476 | | - | |
3477 | 3491 | | |
| 3492 | + | |
| 3493 | + | |
3478 | 3494 | | |
3479 | 3495 | | |
3480 | 3496 | | |
| |||
3701 | 3717 | | |
3702 | 3718 | | |
3703 | 3719 | | |
| 3720 | + | |
3704 | 3721 | | |
3705 | 3722 | | |
3706 | 3723 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
284 | 284 | | |
285 | 285 | | |
286 | 286 | | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
287 | 300 | | |
288 | 301 | | |
289 | 302 | | |
| |||
295 | 308 | | |
296 | 309 | | |
297 | 310 | | |
| 311 | + | |
| 312 | + | |
298 | 313 | | |
299 | 314 | | |
300 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
411 | 411 | | |
412 | 412 | | |
413 | 413 | | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
414 | 498 | | |
0 commit comments