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

Buffer#toString throw on unsupported encodings #2418

Open
wants to merge 2 commits into
base: v2-maintenance
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ and agreed to irrevocably license their contributions under the Duktape
* Luis de Bethencourt (https://github.com/luisbg)
* Ian Whyman (https://github.com/v00d00)
* Rick Sayre (https://github.com/whorfin)
* SheetJS (https://github.com/SheetJS)

Other contributions
===================
Expand Down
14 changes: 13 additions & 1 deletion src-input/duk_bi_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ DUK_INTERNAL duk_ret_t duk_bi_uint8array_plainof(duk_hthread *thr) {

#if defined(DUK_USE_BUFFEROBJECT_SUPPORT)
DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_hthread *thr) {
const char* encoding;
SheetJSDev marked this conversation as resolved.
Show resolved Hide resolved
duk_hbufobj *h_this;
duk_int_t start_offset, end_offset;
duk_uint8_t *buf_slice;
Expand All @@ -1196,7 +1197,18 @@ DUK_INTERNAL duk_ret_t duk_bi_nodejs_buffer_tostring(duk_hthread *thr) {
}
DUK_HBUFOBJ_ASSERT_VALID(h_this);

/* Ignore encoding for now. */
/* TODO: support other encodings. currently only 'utf8' is supported. */
if (duk_is_undefined(thr, 0)) {
encoding = "utf8";
} else if (duk_is_string(thr, 0)) {
encoding = duk_to_string(thr, 0);
DUK_ASSERT(duk_is_string(thr, 0));
if(DUK_STRCMP(encoding, "utf8") != 0) {
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
}
} else {
DUK_DCERROR_TYPE_INVALID_ARGS(thr);
}
SheetJSDev marked this conversation as resolved.
Show resolved Hide resolved

duk__clamp_startend_nonegidx_noshift(thr,
(duk_int_t) h_this->length,
Expand Down
10 changes: 5 additions & 5 deletions tests/ecmascript/test-bi-nodejs-buffer-tostring.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ false
true
"ABC"
"ABC"
"ABC"
"DEFG"
"EFG"
"E"
Expand Down Expand Up @@ -241,20 +240,21 @@ function nodejsBufferToStringTest() {
// buf.toString([encoding], [start], [end])

// Without arguments encoding defaults to UTF-8 and the entire
// buffer is converted to string. At least undefined and null
// buffer is converted to string. At least undefined
// are accepted as "not defined" for encoding.
b = new Buffer('ABC');
safePrintString(b.toString());
safePrintString(b.toString(undefined));
safePrintString(b.toString(null));
// null is not a valid encoding
try { safePrintString(b.toString(null)); } catch(e) { }
SheetJSDev marked this conversation as resolved.
Show resolved Hide resolved

// If the buffer is a slice of an underlying buffer, only that slice
// is string converted. Offsets are relative to the slice.
b = new Buffer('ABCDEFGH');
b = b.slice(3, 7); // DEFG
safePrintString(b.toString());
safePrintString(b.toString(null, 1));
safePrintString(b.toString(null, 1, 2));
safePrintString(b.toString(undefined, 1));
safePrintString(b.toString(undefined, 1, 2));

// When the buffer data is legal UTF-8 and the chosen encoding
// is UTF-8 (default), Duktape internal representation is correct
Expand Down