Repository navigation
Commit 66110f7
Fix a use-after-free of the string of
`dup`, `clone` and `reopen(other)` make two or more `StringIO` objects
share one `struct StringIO`. `StringIO` is write barrier protected, but
`RB_OBJ_WRITE(self, &ptr->string, string)` records the write barrier
only for `self`. If another object that shares the data is in the old
generation, the generational GC doesn't know that it refers to the new
string. The string is collected when `self` is collected, and the other
object keeps a dangling reference:
g = StringIO.new("old".dup)
4.times {GC.start} # Promote g to the old generation
g.dup.string = "new" * 100
GC.start(full_mark: false)
g.string # refers to a collected object
Unprotect objects from the write barrier when they start sharing the
data. `StringIO` objects that don't share their data are still write
barrier protected.
TruffleRuby doesn't have write barriers and doesn't define
`RB_OBJ_WB_UNPROTECT()`, so define it as a no-op there.
This has existed since 009896b "Implement write barrier on StringIO".
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>StringIO that shares its data (#235)1 parent f434104 commit 66110f7
2 files changed
Lines changed: 21 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
46 | 51 | | |
47 | 52 | | |
48 | 53 | | |
| |||
785 | 790 | | |
786 | 791 | | |
787 | 792 | | |
788 | | - | |
789 | 793 | | |
790 | 794 | | |
791 | 795 | | |
792 | 796 | | |
793 | 797 | | |
794 | 798 | | |
795 | | - | |
796 | 799 | | |
797 | 800 | | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
798 | 806 | | |
799 | | - | |
800 | 807 | | |
801 | 808 | | |
802 | 809 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1206 | 1206 | | |
1207 | 1207 | | |
1208 | 1208 | | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
| 1218 | + | |
| 1219 | + | |
1209 | 1220 | | |
1210 | 1221 | | |
1211 | 1222 | | |
| |||
0 commit comments