dankogai / p5-encode

Encode - character encodings (for Perl 5.8 or better)
https://metacpan.org/release/Encode
37 stars 51 forks source link

Fix crash caused by undefined behaviour between two sequence points #72

Closed pali closed 8 years ago

pali commented 8 years ago

Construction like this:

ST(0) = encodemethod(aTHX enc, enc->f_utf8, src, check, NULL, Nullsv, NULL, fallback_cb);

with src = ST(1) and fallback_cb as subroutine which reallocate perl stack leads to undefined behaviour when pointers to perl stack variables are modified more times between two sequence points.

When that code is compiled under gcc 4.6 then at first is evaluated pointer for ST(0), second is evaluation of encode_method() function and third is assignment of return value. But encode_method() modified pointers to perl stack variables and old pointer for ST(0) from first step does not have to be correct...

With this patch ST(0) is not directly modified between two sequence points but via temporary variable RETVAL. Tested under valgrind that memory corruption disappeared.

Fixes bug: https://rt.cpan.org/Public/Bug/Display.html?id=113164

dankogai commented 8 years ago

Thank you!