From 0118fcdfa77fba3cd8abbd35d52042abe5d3ef08 Mon Sep 17 00:00:00 2001 From: Tim Seckinger Date: Sun, 20 May 2018 14:37:28 +0200 Subject: [PATCH] assert: handle undefined filename in getErrMessage 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: https://github.com/nodejs/node/issues/20847 --- lib/assert.js | 4 ++++ test/parallel/test-assert.js | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/assert.js b/lib/assert.js index 1471ba91c12a52..18e1f6b2a4aef3 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -154,6 +154,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}`; diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index b731b9a73ea207..64f86c52eefad2 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -742,6 +742,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();