-
Notifications
You must be signed in to change notification settings - Fork 30.5k
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: fix Buffer.isEncoding() return value for empty string #12847
buffer: fix Buffer.isEncoding() return value for empty string #12847
Conversation
lib/buffer.js
Outdated
@@ -375,6 +375,7 @@ Buffer.compare = function compare(a, b) { | |||
|
|||
Buffer.isEncoding = function(encoding) { | |||
return typeof encoding === 'string' && | |||
encoding !== '' && |
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'd prefer encoding.length > 0 &&
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.
@mscdex Updated, PTAL :=)
Hmm... given that |
3acad90
to
9e55470
Compare
@@ -17,7 +17,8 @@ const assert = require('assert'); | |||
assert.strictEqual(Buffer.isEncoding(enc), true); | |||
}); | |||
|
|||
[ 'utf9', | |||
[ '', | |||
'utf9', |
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.
Unrelated to this change but I think it makes sense to add undefined
.
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.
@lpinca Done :=)
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.
and null
, please
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.
@sam-github Done, updated :=)
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 also think that after line 35 the encoding should actually be used (perhaps Buffer.from()
?), its important that the isEncoding()
should agree with actual behaviour of APIs that use encodings, and its not clear to me ATM that it does, so I think the assert should ensure consistency.
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.
@sam-github So i think the problem we are facing now is that those APIs which use encodings identify invalid encodings inconsistently, and we can't tell whose behavior is actually right?
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 have no idea, I just did a quick check, and ''
was accepted as an encoding, but you said my node version was not master, so perhaps my check was wrong? I'm not entirely clear on what the purpose of the API is, but if it is intended to find encoding values that don't work, it should agree with usage.
So, are various Buffer APIs inconsistent? If so, that would be a good thing to fix! :-)
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.
LGTM. I thought this was by design, but I'm not sure. cc: @nodejs/buffer
@jasnell Hum... so I've edited the PR description ( |
9e55470
to
539dd23
Compare
I think this behavior is by-design.
Since |
I'm fine with this change if the CTC signs off on it and CITGM passes without incident. I think the current behavior is marginally better - but I have no strong feelings about that - and I want the change to be an informed decision by the project. |
I have same questions as above,
|
@sam-github so are
|
@sam-github And when it comes to > Buffer.from('foo').toString('')
TypeError: Unknown encoding:
at stringSlice (buffer.js:558:9)
at Buffer.toString (buffer.js:594:10)
// ... > Buffer.from('foo').toString(null)
TypeError: Unknown encoding: null
at stringSlice (buffer.js:558:9)
at Buffer.toString (buffer.js:594:10)
// ... > Buffer.from('foo').toString(undefined)
'foo' |
539dd23
to
78b41da
Compare
@@ -17,7 +17,8 @@ const assert = require('assert'); | |||
assert.strictEqual(Buffer.isEncoding(enc), true); | |||
}); | |||
|
|||
[ 'utf9', | |||
[ '', | |||
'utf9', |
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 also think that after line 35 the encoding should actually be used (perhaps Buffer.from()
?), its important that the isEncoding()
should agree with actual behaviour of APIs that use encodings, and its not clear to me ATM that it does, so I think the assert should ensure consistency.
@DavidCai1993 this needs a rebase and there is one open comment |
Note:
|
Closing due to long inactivity. There is actually little to do besides rebasing and the single comment and it would make sense to reopen or to open a new PR if someone wants to follow up on this. |
For now the return value of
Buffer.isEncoding('')
istrue
which seems not the preferred behavior:This PR is to fix this.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)