Skip to content

Disallow converting a buffer into itself in primitive_convert - #19918

Merged
nobu merged 1 commit into
ruby:masterfrom
afsozer:fix-econv-primitive-convert-same-buffer
Nov 16, 2026
Merged

nobu merged 1 commit into
ruby:masterfrom
afsozer:fix-econv-primitive-convert-same-buffer

Conversation

@afsozer

@afsozer afsozer commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Encoding::Converter#primitive_convert (econv_primitive_convert in transcode.c) resizes output and updates RSTRING_LEN(output) before reading RSTRING_PTR(input) and RSTRING_LEN(input). When input and output resolve to the same String object (either directly or via #to_str), rb_econv_convert reads beyond the original input length and corrupts buffer accounting.

Reject self-aliasing with ArgumentError ("cannot convert into the source string"), matching Array#pack (93f1010f70) and StringIO#read/pread (1d2d4ac734), while preserving Copy-on-Write behavior for distinct String objects (src.dup).

Encoding::Converter#primitive_convert resizes output and updates
RSTRING_LEN(output) before reading RSTRING_PTR(input) and
RSTRING_LEN(input). When input and output resolve to the same String
object (directly or via #to_str), rb_econv_convert reads beyond the
original input length and corrupts buffer accounting.

Raise ArgumentError when input == output, matching Array#pack and
StringIO#read.
@nobu
nobu enabled auto-merge (rebase) November 16, 2026 10:42
@nobu
nobu merged commit 553ad7e into ruby:master Nov 16, 2026
136 of 138 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants