-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
repl: fix crash with large buffer tab completion #13817
Conversation
If the buffer or array is too large to completion, make a dummy smallest substitute object for it and emit a warning. Fixes: nodejs#3136
lib/repl.js
Outdated
warning.type, | ||
undefined, | ||
undefined, | ||
true); |
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.
There is no boolean fifth argument to process.emitWarning()
so I'm not sure what this is doing.
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.
refer https://github.com/nodejs/node/blob/v8.1.2/lib/internal/process/warning.js#L153
if it's true
, it won't be emitted on next tick.
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.
Ugh... forgot about that change. It wasn't added to the docs and wasn't very keen on it in the first place. In this case, why does the warning need to be emitted immediately?
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.
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.
OK.
lib/repl.js
Outdated
warning.message = | ||
'Instance is too large that the completion may missing ' + | ||
'some customized properties.'; | ||
warning.type = 'REPLWarning'; |
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.
Why not just emit the warning here instead of building this warning object?
lib/repl.js
Outdated
@@ -732,10 +751,12 @@ function complete(line, callback) { | |||
} | |||
} | |||
|
|||
var self = this; |
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.
would prefer not to introduce a new var self = this
into this.
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.
But I need use this
.
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.
Can you please use an arrow function so that you don't need the var self = this
trick?
@jasnell I've updated the code and added some test cases. |
lib/repl.js
Outdated
@@ -689,8 +690,31 @@ function intFilter(item) { | |||
return /^[A-Za-z_$]/.test(item); | |||
} | |||
|
|||
const DEFAULT_PROPERTIES = { |
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'm not sure if these should be uppercased. (I'd prefer them not to be, fwiw.)
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 think it would be better by using uppercase on such a constant.
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.
To be clear: I'm fine with it either way, it just looks a bit weird to me in this specific case, especially the uppercased properties, not the object name itself.
lib/repl.js
Outdated
@@ -732,10 +751,12 @@ function complete(line, callback) { | |||
} | |||
} | |||
|
|||
var self = this; |
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.
Can you please use an arrow function so that you don't need the var self = this
trick?
@@ -23,6 +23,7 @@ | |||
|
|||
const common = require('../common'); | |||
const assert = require('assert'); | |||
const Buffer = require('buffer').Buffer; |
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'm pretty sure you don't need to import it in the test.
lib/repl.js
Outdated
if (mayBeLargeObject(obj) && obj.length > 1e6) { | ||
this._writeToOutput('\r\n'); | ||
process.emitWarning( | ||
'Instance is too large that the completion may missing ' + |
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 feel like "so" might be better than "that" here (but I'm not a native speaker).
|
||
common.hijackStderr(common.mustCall((err) => { | ||
process.nextTick(function() { | ||
assert.ok(/REPLWarning: Instance is too large that the/.test(err)); |
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.
Can you please test the full warning message here?
} | ||
|
||
common.hijackStderr(common.mustCall((err) => { | ||
process.nextTick(function() { |
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.
Small and not essential nit: could you please use an arrow function here to be consistent with the surrounding code?
lib/repl.js
Outdated
this._writeToOutput('\r\n'); | ||
process.emitWarning( | ||
'Instance is too large that the completion may missing ' + | ||
'some customized properties.', |
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 "custom" might be better than "customized" here. (As for me, "customized" implies properties that have been changed, not necessarily added.)
221000d
to
876ab6e
Compare
@@ -305,6 +305,36 @@ testMe.complete('.b', common.mustCall((error, data) => { | |||
assert.deepStrictEqual(data, [['break'], 'b']); | |||
})); | |||
|
|||
// tab completion for large buffer | |||
const warningRegEx = | |||
/\(node:\d+\) REPLWarning: Instance is too large so the completion may missing some custom properties\./; |
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.
This line is longer than 80 characters, but the linter passes.
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.
Shall I split it into two lines?
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 say yes.
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.
We should report this issue to V8. Specifically, Object.keys
throws an error instead of crashing, which seems what Object.getOwnPropertyNames
should do as well
Object.keys(Buffer.alloc(2147483647))
^
RangeError: Invalid array length
at Function.keys (<anonymous>)
at [eval]:1:8
lib/repl.js
Outdated
@@ -689,8 +690,31 @@ function intFilter(item) { | |||
return /^[A-Za-z_$]/.test(item); | |||
} | |||
|
|||
const defaultProperties = { | |||
ARRAY: Object.getOwnPropertyNames([]).filter(intFilter), | |||
BUFFER: Object.getOwnPropertyNames(Buffer.alloc(1)).filter(intFilter) |
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.
Why not Buffer.alloc(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.
Sorry I don't know that we can alloc a zero-size buffer.
lib/repl.js
Outdated
}; | ||
|
||
function mayBeLargeObject(obj) { | ||
return (Array.isArray(obj) || Buffer.isBuffer(obj)); |
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.
This doesn't address other TypedArray types.
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.
Also, currently, Buffer.prototype
passes the Buffer.isBuffer
test. On the other hand, getting Buffer.prototype.length
will throw the following error:
> Buffer.prototype.length
TypeError: Method get TypedArray.prototype.length called on incompatible receiver [object Object]
at Uint8Array.get length [as length] (<anonymous>)
at repl:1:17
at ContextifyScript.Script.runInThisContext (vm.js:44:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:433:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
at REPLServer.Interface._onLine (readline.js:278:10)
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.
Also, currently, Buffer.prototype passes the Buffer.isBuffer test.
How about obj instanceof Buffer
?
lib/repl.js
Outdated
function filteredOwnPropertyNames(obj) { | ||
if (!obj) return []; | ||
if (mayBeLargeObject(obj) && obj.length > 1e6) { |
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.
Can you use a constant for 1e6
like const ARRAY_LENGTH_THRESHOLD
?
lib/repl.js
Outdated
'REPLWarning', | ||
undefined, | ||
undefined, | ||
true); |
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.
Personally I'm okay with not showing the warning. Autocomplete is not a feature that necessarily has to work 100% of the time.
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.
But if there's no warning, I think after this PR landed, developers use Node.js may open several issue about this bug "why autocompletion is wrong", though it's not exactly a bug.
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.
@XadillaX fwiw, the autocompletion in REPL is far from perfect and I, as a user, would not and do not expect it to always show all the completions (though it would be an awesome thing). Even IDEs don't do that reliably for JavaScript.
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.
@aqrln The point is:
> var ele = Buffer.alloc(1);
> ele.biu = 1;
> ...
> ele.<tab>
ele.__defineGetter__ ele.__defineSetter__ ele.__lookupGetter__ ele.__lookupSetter__
ele.__proto__ ele.constructor ele.hasOwnProperty ele.isPrototypeOf
ele.propertyIsEnumerable ele.toLocaleString ele.toString ele.valueOf
...
ele.biu
> var ele = Buffer.alloc(1e6 + 1); ele.biu = 1;
> ele.<tab>
(node:3635) REPLWarning: Instance is too large so the completion may missing some custom properties.
ele.__defineGetter__ ele.__defineSetter__ ele.__lookupGetter__ ele.__lookupSetter__
ele.__proto__ ele.constructor ele.hasOwnProperty ele.isPrototypeOf
ele.propertyIsEnumerable ele.toLocaleString ele.toString ele.valueOf
...
Without warning, the developers may be confused that why there's no biu
, that completion is what truly they want to find.
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.
Okay. In that case, the message could be clearer:
The current object has too many own properties. Completion output may be truncated.
or
The current array, Buffer, or TypedArray has too many entries. Certain properties may be missing from completion output.
I think we may ask V8 to provide an API to get non-enumarable properties. |
lib/repl.js
Outdated
// `Buffer.prototype` passes the `Buffer.isBuffer` and | ||
// `instanceof Uint8Array`. | ||
// | ||
// Refs: https://github.com/nodejs/node/pull/11961 |
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.
This will still throw an error on e.g. Object.create(Uint8Array.prototype)
.
Instead of all the checks below, it could be implemented simply and reliably as:
const { isTypedArray } = process.binding('util');
function maybeLargeObject(obj) {
return Array.isArray(obj) || isTypedArray(obj);
}
(Buffers pass the isTypedArray
test.)
lib/repl.js
Outdated
[ Float32Array, | ||
Object.getOwnPropertyNames(new Float32Array()).filter(intFilter) ], | ||
[ Float64Array, | ||
Object.getOwnPropertyNames(new Float64Array()).filter(intFilter) ] |
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.
Of all the types enumerated, only plain arrays have own properties by default ('length'
). No TypedArray objects have own properties by default.
lib/repl.js
Outdated
'REPLWarning', | ||
undefined, | ||
undefined, | ||
true); |
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.
Okay. In that case, the message could be clearer:
The current object has too many own properties. Completion output may be truncated.
or
The current array, Buffer, or TypedArray has too many entries. Certain properties may be missing from completion output.
1093500
to
573e252
Compare
I think this PR needs CI again. |
could this be landed now? |
if (Array.isArray(obj)) { | ||
return obj.length > ARRAY_LENGTH_THRESHOLD ? ['length'] : null; | ||
} else if (utilBinding.isTypedArray(obj)) { | ||
return obj.length > ARRAY_LENGTH_THRESHOLD ? [] : null; |
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.
But typed arrays have length
too, don't they?
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.
TypedArrays' length
is implemented as a getter on the prototype instead of an own property, as is the case with Arrays.
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.
Timothy is 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.
So I think this PR could be landed now. @jasnell |
For landing, whom to /ping? |
Replaced |
If the buffer or array is too large to completion, make a dummy smallest substitute object for it and emit a warning. PR-URL: #13817 Fixes: #3136 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]>
@XadillaX IRC is a good place for such pings too. And sorry that it took a while, as a person who constantly pinged people for CI and landing some months ago, I know how frustrating it feels sometimes. FWIW, I thought about landing it, but then I was on a little vacation and I did no GitHub, and today I've opened this tab just to see that Timothy beat me to it. I hope that soon you'll be able to land your patches yourself :) |
If the buffer or array is too large to completion, make a dummy smallest substitute object for it and emit a warning. PR-URL: #13817 Fixes: #3136 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]>
If the buffer or array is too large to completion, make a dummy smallest substitute object for it and emit a warning. PR-URL: #13817 Fixes: #3136 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]>
If the buffer or array is too large to completion, make a dummy smallest substitute object for it and emit a warning. PR-URL: #13817 Fixes: #3136 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Alexey Orlenko <[email protected]>
Should this be backported to The test will need to be rewritten as |
If the buffer or array is too large to completion, make a dummy smallest
substitute object for it and emit a warning.
Fixes: #3136
Checklist
make -j4 test
(UNIX)Affected core subsystem(s)
repl