diff --git a/lib/internal/source_map/source_map.js b/lib/internal/source_map/source_map.js index 42e7bca3c4c5c0..cda2dabc9ab7a5 100644 --- a/lib/internal/source_map/source_map.js +++ b/lib/internal/source_map/source_map.js @@ -300,7 +300,8 @@ class SourceMap { sourceColumnNumber += decodeVLQ(stringCharIterator); let name; - if (!isSeparator(stringCharIterator.peek())) { + if (stringCharIterator.hasNext() && + !isSeparator(stringCharIterator.peek())) { nameIndex += decodeVLQ(stringCharIterator); name = map.names?.[nameIndex]; } diff --git a/test/parallel/test-source-map-trailing-name.js b/test/parallel/test-source-map-trailing-name.js new file mode 100644 index 00000000000000..55867b811b0010 --- /dev/null +++ b/test/parallel/test-source-map-trailing-name.js @@ -0,0 +1,34 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const { SourceMap } = require('node:module'); + +// Refs: https://github.com/nodejs/node/issues/63795 + +// A mapping whose final segment has 4 fields (no name index) must not +// inherit the previous segment's name. +{ + const sm = new SourceMap({ + version: 3, + sources: ['a.js'], + names: ['foo'], + mappings: 'AAAAA,CAAC', + }); + + assert.strictEqual(sm.findEntry(0, 0).name, 'foo'); + assert.strictEqual(sm.findEntry(0, 1).name, undefined); +} + +// A mapping whose final segment legitimately carries a name index must still +// resolve it: the end-of-string guard must not suppress a real trailing name. +{ + const sm = new SourceMap({ + version: 3, + sources: ['a.js'], + names: ['foo'], + mappings: 'AAAA,CAAAA', + }); + + assert.strictEqual(sm.findEntry(0, 0).name, undefined); + assert.strictEqual(sm.findEntry(0, 1).name, 'foo'); +}