Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix one-byte string Latin-1 to UTF-8 conversion #338

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions ext/mini_racer_extension/mini_racer_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,24 @@ static VALUE str_encode_bang(VALUE v)

static void des_string8(void *arg, const uint8_t *s, size_t n)
{
rb_encoding *e;
DesCtx *c;
VALUE v;

c = arg;
v = rb_enc_str_new((char *)s, n, rb_ascii8bit_encoding());
if (c->transcode_latin1)
if (*c->err)
return;
if (c->transcode_latin1) {
e = rb_enc_find("ISO-8859-1"); // TODO cache?
if (!e) {
snprintf(c->err, sizeof(c->err), "no ISO-8859-1 encoding");
return;
}
v = rb_enc_str_new((char *)s, n, e);
v = str_encode_bang(v); // cannot fail
} else {
v = rb_enc_str_new((char *)s, n, rb_ascii8bit_encoding());
}
put(c, v);
}

Expand Down
1 change: 1 addition & 0 deletions test/mini_racer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,7 @@ def test_function_property

def test_string_encoding
context = MiniRacer::Context.new
assert_equal "ä", context.eval("'ä'")
assert_equal "ok", context.eval("'ok'".encode("ISO-8859-1"))
assert_equal "ok", context.eval("'ok'".encode("ISO8859-1"))
assert_equal "ok", context.eval("'ok'".encode("UTF-16LE"))
Expand Down
Loading