Skip to content

Commit

Permalink
assert: handle undefined filename in getErrMessage
Browse files Browse the repository at this point in the history
When generating an assertion error message,
`filename` might be undefined,
e.g. if `assert` is called in `eval`.

Handle this case gracefully instead of failing with
`Cannot read property 'endsWith' of undefined`.

Fixes: #20847

PR-URL: #20848
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Khaidi Chu <i@2333.moe>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
jeysal authored and targos committed May 25, 2018
1 parent ed9e964 commit ea702e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ function getBuffer(fd, assertLine) {

function getErrMessage(call) {
const filename = call.getFileName();
if (!filename) {
return;
}

const line = call.getLineNumber() - 1;
const column = call.getColumnNumber() - 1;
const identifier = `${filename}${line}${column}`;
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,16 @@ common.expectsError(
}
);

// works in eval
common.expectsError(
() => new Function('assert', 'assert(1 === 2);')(assert),
{
code: 'ERR_ASSERTION',
type: assert.AssertionError,
message: 'false == true'
}
);

// Do not try to check Node.js modules.
{
const e = new EventEmitter();
Expand Down

0 comments on commit ea702e2

Please sign in to comment.