Skip to content

fix / #160 unhandled error with 0 col in related information - #162

Merged
davidramnero merged 4 commits into
mainfrom
fix/#160-unhandled-error-with-0-col-in-related-information
Nov 19, 2026
Merged

davidramnero merged 4 commits into
mainfrom
fix/#160-unhandled-error-with-0-col-in-related-information

Conversation

@davidramnero

Copy link
Copy Markdown
Collaborator

No description provided.

@danmar

danmar commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Suggestion: some tests for this fix, added to the existing Related information suite in src/test/extension.test.ts. They cover column 0, a missing column, and column 0 in a file that can't be opened.

With this PR, all tests pass (165 passing). Without the fix, all 3 new tests fail, and the column-0 cases throw Illegal argument: character must be non-negative, the crash from #160.

diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts
index 8c3b408..951d1c4 100644
--- a/src/test/extension.test.ts
+++ b/src/test/extension.test.ts
@@ -35,6 +35,44 @@ suite('Related information', () => {
 		// 0-indexing vs 1-indexing -> column: 5 becomes 4
 		assert.strictEqual(infos[0].location.range.start.character, 4);
 	});
+
+	// #160: cppcheck may report column 0, which used to become -1 and make vscode.Range throw
+	test('Location with column 0 is clamped to character 0', async () => {
+		const locations = [
+			{ $: { info: 'Primary', line: '1', column: '1', file: sourceFile } },
+			{ $: { info: 'Column zero', line: '3', column: '0', file: sourceFile } },
+		];
+		const infos = await extractRelatedInformation(locations as any);
+		assert.strictEqual(infos.length, 2);
+		assert.strictEqual(infos[0].message, 'Column zero');
+		assert.strictEqual(infos[0].location.range.start.line, 2);
+		assert.strictEqual(infos[0].location.range.start.character, 0);
+		// Range spans to end of line ' return x/0;'
+		assert.strictEqual(infos[0].location.range.end.character, 12);
+	});
+
+	test('Location with missing column is clamped to character 0', async () => {
+		const locations = [
+			{ $: { info: 'Primary', line: '1', column: '1', file: sourceFile } },
+			{ $: { info: 'No column', line: '2', file: sourceFile } },
+		];
+		const infos = await extractRelatedInformation(locations as any);
+		assert.strictEqual(infos.length, 2);
+		assert.strictEqual(infos[0].message, 'No column');
+		assert.strictEqual(infos[0].location.range.start.character, 0);
+	});
+
+	test('Location with column 0 in a file that cannot be opened', async () => {
+		const locations = [
+			{ $: { info: 'Primary', line: '1', column: '1', file: sourceFile } },
+			{ $: { info: 'Missing file', line: '1', column: '0', file: path.join(dir, 'does-not-exist.c') } },
+		];
+		const infos = await extractRelatedInformation(locations as any);
+		assert.strictEqual(infos.length, 2);
+		assert.strictEqual(infos[0].message, 'Missing file');
+		assert.strictEqual(infos[0].location.range.start.character, 0);
+		assert.strictEqual(infos[0].location.range.end.character, 0);
+	});
 });
 
 suite('Argument parsing', () => {

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The fix for #160 works: a related location with column 0 or no column is now clamped to character 0 and no longer makes vscode.Range throw. The tests that C1 suggested were added, and they pass along with all the existing ones.

One new problem: extractRelatedInformation can still throw for another bad input in the same function. It checks the line only for NaN and < 0, not against the document's lineCount, so a related location past the end of the file throws Illegal value for \line`fromrelatedDocument.lineAt(lLine). The main-location code in extension.tsalready checksline >= document.lineCount`, but this function does not. I reproduced this with a test. I found no other problems.

Comments

# Comment By Where Status
1 Add tests for column 0, missing column, and column 0 in a missing file @danmar general ✅ Fixed
2 Related line past end of document still throws in lineAt AI review src/helpers/diagnosticHelpers.ts:61 🆕 New

@davidramnero, please fix, resolve or reply to the open and new comments: #2.

Testing

Build: in /work/pr, npm run compile and npm run lint both ran with no errors and no warnings.

Test suite: the system node (v18) can't run vscode-test, because string-width uses the regex v flag. So I ran the test CLI with VS Code's Electron as node, using this config in /work/scratch/test.mjs (with files: 'out/test/**/*.test.js'):

import { defineConfig } from '@vscode/test-cli';
export default defineConfig({
  files: 'out/test/**/*.test.js',
  useInstallation: { fromPath: '/vscode/code' },
  launchArgs: ['--no-sandbox','--disable-gpu','--user-data-dir','/work/scratch/ud'],
  mocha: { timeout: 60000 }
});
cd /work/pr
PATH=/work/cppcheck:$PATH ELECTRON_RUN_AS_NODE=1 xvfb-run -a /vscode/code node_modules/@vscode/test-cli/out/bin.mjs --config .scratch-test.mjs

Result: 166 passing, 1 failing. The 166 are the 165 tests of the PR, including the 3 new ones, plus my test for column 0 on an empty line, which passes. The 1 failure is my other test, below.

Reproduced problem, related location past the end of the file (src/test/scratch.test.ts, copy kept in /work/scratch/scratch.test.ts):

test('line past EOF', async () => {
	const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'scr-'));
	const f = path.join(dir, 'a.c');
	fs.writeFileSync(f, 'int main() {\n return 0;\n}\n');
	const locations = [
		{ $: { info: 'Primary', line: '1', column: '1', file: f } },
		{ $: { info: 'Past EOF', line: '50', column: '0', file: f } },
	];
	const infos = await extractRelatedInformation(locations as any);
});

Output:

  1) Scratch related
       line past EOF:
     Error: Illegal value for `line`
  	at Object.lineAt (.../extensionHostProcess.js:176:67)
  	at extractRelatedInformation (out/helpers/diagnosticHelpers.js:98:55)

This was already the behaviour before the PR, but it is the same kind of unhandled error as #160, and it is left in the function that this PR fixes.


Automated review of d85b3e7 by Claude. The pull request was built and tested locally.

Comment thread src/helpers/diagnosticHelpers.ts Resolved

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

C3 is not fixed, although its thread is marked resolved. Commit 26b052d adds the lLine >= relatedDocument.lineCount check at src/helpers/diagnosticHelpers.ts:80, but it runs too late. The column check on line 76 calls relatedDocument.lineAt(lLine) first, so a related location past the end of the file still throws Illegal value for \line`. I reproduced this with the same test as C2/C3. The fix is to put the lineCount` check before line 76, or merge it with the early check on line 61. The column-0 fix for #160 works. I found no new problems beyond this.

Comments

# Comment By Where Status
1 Add tests for column 0, missing column, and column 0 in a missing file @danmar general ✅ Fixed
2 Related line past end of document still throws in lineAt AI review src/helpers/diagnosticHelpers.ts:61 ☑️ Resolved, code unchanged

Testing

Build: in /work/pr, npm run compile and npm run lint both ran with no errors.

Test suite: I ran it with VS Code's Electron as node, using /work/scratch/test.mjs. The config has useInstallation: { fromPath: '/vscode/code' }, files: 'out/test/**/*.test.js' and the launch args --no-sandbox --disable-gpu --disable-dev-shm-usage --user-data-dir /work/scratch/ud. The first run without --disable-dev-shm-usage stopped because the test window's renderer crashed (code 133). That is a container problem, not the PR.

cd /work/pr
PATH=/work/cppcheck:$PATH ELECTRON_RUN_AS_NODE=1 xvfb-run -a /vscode/code node_modules/@vscode/test-cli/out/bin.mjs --config .scratch-test.mjs

Result: 165 passing, 2 pending, 1 failing. All the tests of the PR pass, including the 3 new ones. The one failure is my scratch test below.

Reproduced: a related location past the end of the file still throws (src/test/scratch.test.ts, copy in /work/scratch/scratch.test.ts):

test('line past EOF', async () => {
	const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'scr-'));
	const f = path.join(dir, 'a.c');
	fs.writeFileSync(f, 'int main() {\n return 0;\n}\n');
	const locations = [
		{ $: { info: 'Primary', line: '1', column: '1', file: f } },
		{ $: { info: 'Past EOF', line: '50', column: '0', file: f } },
	];
	const infos = await extractRelatedInformation(locations as any);
	assert.strictEqual(infos.length, 1);
});
  1) Scratch related
       line past EOF:
     Error: Illegal value for `line`
  	at Object.lineAt (.../extensionHostProcess.js:176:67)
  	at extractRelatedInformation (out/helpers/diagnosticHelpers.js:98:55)

diagnosticHelpers.js:98 is the column check lCol > relatedDocument.lineAt(lLine).text.length (TS line 76), which runs before the new lineCount guard on line 80.


Automated review of 26b052d by Claude. The pull request was built and tested locally.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no new problems. Commit 1ab6fb7 fixes C3: the lLine >= relatedDocument.lineCount check now runs before the first relatedDocument.lineAt(lLine) call, at src/helpers/diagnosticHelpers.ts:76. A related location past the end of the file is now skipped and no longer throws Illegal value for \line`. The fix for #160 also works: column 0` or no column is set to character 0. All tests pass, including my edge-case tests.

Comments

# Comment By Where Status
1 Add tests for column 0, missing column, and column 0 in a missing file @danmar general ✅ Fixed
2 Related line past end of document still throws in lineAt AI review src/helpers/diagnosticHelpers.ts:61 ✅ Fixed

Testing

Build: in /work/pr, npm run compile and npm run lint both ran with no errors.

Test suite: the system node is v18, so I ran the test CLI with VS Code's Electron as node. The config in /work/pr/.scratch-test.mjs has useInstallation: { fromPath: '/vscode/code' }, files: 'out/test/**/*.test.js', --no-sandbox --disable-gpu --disable-dev-shm-usage --user-data-dir /work/scratch/ud and a mocha timeout of 60 s.

cd /work/pr
PATH=/work/cppcheck:$PATH ELECTRON_RUN_AS_NODE=1 xvfb-run -a /vscode/code node_modules/@vscode/test-cli/out/bin.mjs --config .scratch-test.mjs

Result: 171 passing, 2 pending, 0 failing. That is the tests of the PR plus my 6 scratch tests.

Scratch tests (src/test/scratch.test.ts, copy in /work/scratch/scratch.test.ts). The source is a 3-line file, int main() {\n return 0;\n}\n, at a path with spaces (scr dir-*/a b.c). The tests cover:

  • Line 50 with column 0, past the end of the file. This is the C2/C3 reproducer: the location is skipped, nothing throws, and infos.length === 1.
  • Line 4, the empty line after the trailing newline, which equals lineCount: kept, with character 0.
  • Line 5: skipped.
  • Line 0 and a missing line: skipped.
  • Column -3: set to character 0.
  • Line 999 in a file that does not exist: kept with an empty range, which is the same as the existing missing-file behaviour.

All of them pass. Afterwards I removed the scratch files from pr/ and recompiled.


Automated review of 1ab6fb7 by Claude. The pull request was built and tested locally.

@davidramnero
davidramnero merged commit e3ac8d5 into main Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants