Skip to content
Merged
Prev Previous commit
Next Next commit
add BOM test case to cover 0xFF in decodeLatin1
  • Loading branch information
Mert Can Altin
Mert Can Altin committed Oct 5, 2024
commit b92e571f04983a7cf029649e9b2fef5db312bb75
2 changes: 1 addition & 1 deletion src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void BindingData::DecodeLatin1(const FunctionCallbackInfo<Value>& args) {
const uint8_t* data = buffer.data();
size_t length = buffer.length();

if (ignore_bom && length > 0 && data[0] == 0xFEFF) {
if (ignore_bom && length > 0 && data[0] == 0xFF) {
data++;
length--;
}
Expand Down
19 changes: 19 additions & 0 deletions test/cctest/test_encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,24 @@ TEST_F(EncodingBindingTest, DecodeLatin1_IgnoreBOMAndFatal) {
EXPECT_STREQ(*utf8_result, "Áéó");
}

TEST_F(EncodingBindingTest, DecodeLatin1_BOMPresent) {
Environment* env = CreateEnvironment();
Isolate* isolate = env->isolate();
HandleScope handle_scope(isolate);

const uint8_t latin1_data[] = {0xFF, 0xC1, 0xE9, 0xF3};
Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(latin1_data));
memcpy(ab->GetBackingStore()->Data(), latin1_data, sizeof(latin1_data));

Local<Uint8Array> array = Uint8Array::New(ab, 0, sizeof(latin1_data));
Local<Value> args[] = {array};

Local<Value> result;
EXPECT_TRUE(RunDecodeLatin1(env, args, true, false, &result));

String::Utf8Value utf8_result(isolate, result);
EXPECT_STREQ(*utf8_result, "Áéó");
}

} // namespace encoding_binding
} // namespace node