Skip to content

module: don't read a phantom name on the last mapping segment#63801

Open
dusanstanojeviccs wants to merge 1 commit into
nodejs:mainfrom
dusanstanojeviccs:fix-source-map-trailing-name
Open

module: don't read a phantom name on the last mapping segment#63801
dusanstanojeviccs wants to merge 1 commit into
nodejs:mainfrom
dusanstanojeviccs:fix-source-map-trailing-name

Conversation

@dusanstanojeviccs
Copy link
Copy Markdown

Fixes a stale-name bug in SourceMap's mappings parser. The optional name field is read with !isSeparator(peek()) and no hasNext() guard, so at end-of-string the parser reads a phantom VLQ (0) and stamps the previous nameIndex's name onto a segment that has none.

const { SourceMap } = require('node:module');
const sm = new SourceMap({ version: 3, sources: ['a.js'], names: ['foo'], mappings: 'AAAAA,CAAC' });
sm.findEntry(0, 1).name; // 'foo' before, undefined after

User-visible effect: with --enable-source-maps, the bottom global frame of a stack trace inherits the last-named function name. Introduced by #36042 (first released in v15.4.0 / v14.17.1); confirmed absent in v14.17.0.

Includes a regression test covering both a nameless trailing segment and a legitimately-named one.

Fixes: #63795

SourceMap#parseMap reads the optional name VLQ guarded only by
!isSeparator(peek()). At the end of the mappings string peek() returns
'', which is not a separator, so the parser decodes a phantom VLQ (0),
keeps the previous nameIndex, and assigns names[nameIndex] to a segment
that has no name field. SourceMap.findEntry() then returns a stale name,
which surfaces as the wrong function name on the bottom global frame of
--enable-source-maps stack traces.

Guard the name read with hasNext() so the phantom field is not read at
end of string. A trailing segment that genuinely carries a name still
has characters left to read, so it is unaffected.

Refs: nodejs#63795
Signed-off-by: Dusan Stanojevic <dusan.stanojevic.cs@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. source maps Issues and PRs related to source map support.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SourceMap.findEntry() returns a stale name for a final mapping segment that has no name index

2 participants