Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.3k
buffer: add buffer.isUtf8 for utf8 validation#45947
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
be10b36bcb19ec6c8ac38103e807b590f06e940f59File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -511,6 +511,7 @@ class ArrayBufferViewContents{ | ||
| inline void Read(v8::Local<v8::ArrayBufferView> abv); | ||
| inline void ReadValue(v8::Local<v8::Value> buf); | ||
| inline bool WasDetached() const{return was_detached_} | ||
| inline const T* data() const{return data_} | ||
| inline size_t length() const{return length_} | ||
| @@ -525,6 +526,7 @@ class ArrayBufferViewContents{ | ||
| T stack_storage_[kStackStorageSize]; | ||
| T* data_ = nullptr; | ||
| size_t length_ = 0; | ||
| bool was_detached_ = false; | ||
anonrig marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| }; | ||
| class Utf8Value : public MaybeStackBuffer<char>{ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| 'use strict'; | ||
| require('../common'); | ||
| constassert=require('assert'); | ||
| const{ isUtf8, Buffer }=require('buffer'); | ||
| const{ TextEncoder }=require('util'); | ||
| constencoder=newTextEncoder(); | ||
| assert.strictEqual(isUtf8(encoder.encode('hello')),true); | ||
| assert.strictEqual(isUtf8(encoder.encode('ğ')),true); | ||
| assert.strictEqual(isUtf8(Buffer.from([])),true); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does a zero length buffer return true? I would expect this to be false. MemberAuthor
| ||
| // Taken from test/fixtures/wpt/encoding/textdecoder-fatal.any.js | ||
| [ | ||
| [0xFF],// 'invalid code' | ||
| [0xC0],// 'ends early' | ||
| [0xE0],// 'ends early 2' | ||
| [0xC0,0x00],// 'invalid trail' | ||
| [0xC0,0xC0],// 'invalid trail 2' | ||
| [0xE0,0x00],// 'invalid trail 3' | ||
| [0xE0,0xC0],// 'invalid trail 4' | ||
| [0xE0,0x80,0x00],// 'invalid trail 5' | ||
| [0xE0,0x80,0xC0],// 'invalid trail 6' | ||
| [0xFC,0x80,0x80,0x80,0x80,0x80],// '> 0x10FFFF' | ||
| [0xFE,0x80,0x80,0x80,0x80,0x80],// 'obsolete lead byte' | ||
| // Overlong encodings | ||
| [0xC0,0x80],// 'overlong U+0000 - 2 bytes' | ||
| [0xE0,0x80,0x80],// 'overlong U+0000 - 3 bytes' | ||
| [0xF0,0x80,0x80,0x80],// 'overlong U+0000 - 4 bytes' | ||
| [0xF8,0x80,0x80,0x80,0x80],// 'overlong U+0000 - 5 bytes' | ||
| [0xFC,0x80,0x80,0x80,0x80,0x80],// 'overlong U+0000 - 6 bytes' | ||
| [0xC1,0xBF],// 'overlong U+007F - 2 bytes' | ||
| [0xE0,0x81,0xBF],// 'overlong U+007F - 3 bytes' | ||
| [0xF0,0x80,0x81,0xBF],// 'overlong U+007F - 4 bytes' | ||
| [0xF8,0x80,0x80,0x81,0xBF],// 'overlong U+007F - 5 bytes' | ||
| [0xFC,0x80,0x80,0x80,0x81,0xBF],// 'overlong U+007F - 6 bytes' | ||
| [0xE0,0x9F,0xBF],// 'overlong U+07FF - 3 bytes' | ||
| [0xF0,0x80,0x9F,0xBF],// 'overlong U+07FF - 4 bytes' | ||
| [0xF8,0x80,0x80,0x9F,0xBF],// 'overlong U+07FF - 5 bytes' | ||
| [0xFC,0x80,0x80,0x80,0x9F,0xBF],// 'overlong U+07FF - 6 bytes' | ||
| [0xF0,0x8F,0xBF,0xBF],// 'overlong U+FFFF - 4 bytes' | ||
| [0xF8,0x80,0x8F,0xBF,0xBF],// 'overlong U+FFFF - 5 bytes' | ||
| [0xFC,0x80,0x80,0x8F,0xBF,0xBF],// 'overlong U+FFFF - 6 bytes' | ||
| [0xF8,0x84,0x8F,0xBF,0xBF],// 'overlong U+10FFFF - 5 bytes' | ||
| [0xFC,0x80,0x84,0x8F,0xBF,0xBF],// 'overlong U+10FFFF - 6 bytes' | ||
| // UTF-16 surrogates encoded as code points in UTF-8 | ||
| [0xED,0xA0,0x80],// 'lead surrogate' | ||
| [0xED,0xB0,0x80],// 'trail surrogate' | ||
| [0xED,0xA0,0x80,0xED,0xB0,0x80],// 'surrogate pair' | ||
| ].forEach((input)=>{ | ||
| assert.strictEqual(isUtf8(Buffer.from(input)),false); | ||
| }); | ||
| [ | ||
| null, | ||
| undefined, | ||
| 'hello', | ||
| true, | ||
| false, | ||
| ].forEach((input)=>{ | ||
| assert.throws( | ||
| ()=>{isUtf8(input);}, | ||
| { | ||
| code: 'ERR_INVALID_ARG_TYPE', | ||
| }, | ||
| ); | ||
| }); | ||
| { | ||
| // Test with detached array buffers | ||
| constarrayBuffer=newArrayBuffer(1024); | ||
| structuredClone(arrayBuffer,{transfer: [arrayBuffer]}); | ||
| assert.throws( | ||
| ()=>{isUtf8(arrayBuffer);}, | ||
| { | ||
| code: 'ERR_INVALID_STATE' | ||
| } | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's after the fact but why does the buffer being detached matter here? It would be otherwise indistinguishable from zero-length which we should just return false for anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Detached buffers create false sense of UTF8 validation, if there isn’t an error in here, since there is no way of accessing the underlying data store, and validating for UTF-8, I believe this error is valid.