Repository navigation
Internal errors only show exitcode is 1 not the addons output - #166
davidramnero wants to merge 2 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
For internal errors, the PR now shows cppcheck's verbose text, so the addon's command and output appear in the Problems panel as intended. However, the new code decodes only the \012 escape. cppcheck's fixInvalidChars writes every non-printable byte as a backslash and three octal digits. Tabs therefore show as \011, CR as \015, and any non-ASCII text in the addon output or its paths as raw byte escapes: a path under /home/jörg shows as /home/j\303\266rg. The same expression is duplicated in runCppcheckOnFileXML and runFullAnalysis, and no test covers the change. I found no other problems.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Only \012 is decoded; tabs, CR and non-ASCII (UTF-8) bytes in the verbose text stay as octal escapes |
AI review | src/extension.ts:611 |
🆕 New |
@davidramnero, please fix, resolve or reply to the open and new comments: #1.
Testing
npm run compileandnpm run lintin/work/pr: both clean.- Test suite: the
npx vscode-testCLI does not run on the container's Node 18 (SyntaxError: Invalid regular expression flagsinstring-width). I ran it with VS Code's own Electron as Node instead:PATH=/work/cppcheck:$PATH ELECTRON_RUN_AS_NODE=1 xvfb-run -a /vscode/code node_modules/@vscode/test-cli/out/bin.mjs --config <cfg>. The config useduseInstallation: /vscode/codeand--no-sandbox --disable-gpu --user-data-dir /work/scratch/ud. Result: 165 passing, 2 pending. None of the tests exercise the new code. - Real cppcheck output: an addon that prints two lines and exits with code 3 gives
verbose="... - exitcode is 3: python3 .../runaddon.py bad.py --cli a.c.510.dump\012Output:\012boom error line1\012line2 <&>".
The PR'sreplaceAll('\\012', '\n')turns this into readable lines. - Escapes that are not decoded. Addon
bad2.py:import sys print("Fehler: Datei /home/jörg/x.c\tnicht gefunden") sys.exit(1)
cppcheck --xml --addon=bad2.py a.c 2>out2.xml. I parsed the result with xml2js and applied the PR's expression (/work/scratch/parse.js):Output:const message = e.$.verbose ? e.$.verbose.replaceAll('\\012', '\n') : e.$.msg;
The"...exitcode is 1: python3 /work/cppcheck/addons/runaddon.py bad2.py --cli a.c.3817.dump\nOutput:\nFehler: Datei /home/j\\303\\266rg/x.c\\011nicht gefunden"öand the tab are still shown as\303\266and\011in the diagnostic.
Automated review of 4aaaf55 by Claude. The pull request was built and tested locally.
| // Set up a vscode.diagnostic object | ||
| const colEnd = mainLocDocument ? mainLocDocument.lineAt(line).text.length : col; | ||
| const diagnostic = setUpDiagnostic(line, col, line, colEnd, e.$.id, e.$.msg, severity); | ||
| const message = isCriticalWarning(e.$.id) && e.$.verbose ? e.$.verbose.replaceAll('\\012', '\n') : e.$.msg; |
There was a problem hiding this comment.
AI review
cppcheck's ErrorMessage::fixInvalidChars writes every non-printable byte as a backslash and three octal digits, not just newline. That covers \011 (tab), \015 (CR) and each byte of a UTF-8 character. Addon output that has non-ASCII text, or a path such as a home directory with ö, is therefore shown garbled.
Reproduced with real cppcheck output. An addon that prints Fehler: Datei /home/jörg/x.c<TAB>nicht gefunden and exits with code 1 gives this message after this line runs:
...Output:
Fehler: Datei /home/j\303\266rg/x.c\011nicht gefunden
I suggest decoding all \ooo sequences into bytes and then decoding those as UTF-8, for example:
function decodeCppcheckEscapes(s: string): string {
const bytes: number[] = [];
for (let i = 0; i < s.length; i++) {
const m = /^\\([0-7]{3})/.exec(s.slice(i, i + 4));
if (m) { bytes.push(parseInt(m[1], 8)); i += 3; }
else { bytes.push(...Buffer.from(s[i])); }
}
return Buffer.from(bytes).toString('utf8');
}Put it in diagnosticHelpers.ts and call it from both places, here and at line 781. That also gives the helper a place for a unit test; the PR currently adds none.
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
New problem: decodeCppcheckEscapes decodes every backslash followed by three octal digits, but cppcheck's fixInvalidChars leaves a literal backslash alone, since it is printable. Windows paths in the addon command line or in its output therefore get mangled when a path component starts with three octal digits. C:\work\2024\src\misra.py is shown as C:\work�4\src\misra.py, and C:\100files would become C:\@files. Internal-error messages are mostly paths, so this case is common on Windows.
The earlier problem is fixed: only \012 was decoded, and now every escape is decoded, through a shared helper. The PR still adds no unit test for the decoder. I found nothing else.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Only \012 was decoded; tabs, CR and UTF-8 bytes stayed as octal escapes |
AI review | src/extension.ts:611 |
✅ Fixed |
| 2 | No test covers the change | AI review | general | |
| 3 | Literal backslashes (Windows paths such as C:\work\2024) are decoded as octal escapes and mangled |
AI review | src/helpers/diagnosticHelpers.ts:36 |
🆕 New |
@davidramnero, please fix, resolve or reply to the open and new comments: #2, #3.
Testing
-
npm run compileandnpm run lintin/work/pr: both clean. -
Test suite: ran it through VS Code's Electron, because the
vscode-testCLI needs a newer Node than the container has. Command:PATH=/work/cppcheck:$PATH ELECTRON_RUN_AS_NODE=1 xvfb-run -a /vscode/code node_modules/@vscode/test-cli/out/bin.mjs --config <cfg>. The config setfiles: 'out/test/**/*.test.js',useInstallation: /vscode/code, and--no-sandbox --disable-gpu --user-data-dir /work/scratch/ud. Result: 167 passing. None of the tests exercisedecodeCppcheckEscapes. -
Windows path mangled. I wrote an addon that prints a Windows-style path and a tab, ran real cppcheck on it, and applied the PR's decoder, copied verbatim (it can't be imported outside VS Code because the module imports
vscode)./work/scratch/winaddon.py:import sys print(r"Traceback: File C:\work\2024\src\misra.py, line 3") print("tab:\there") sys.exit(1)
/work/scratch/dec.js:const fs=require('fs');const xml2js=require('/work/pr/node_modules/xml2js'); function decodeCppcheckEscapes(s){const bytes=[];for(let i=0;i<s.length;i++){const m=/^\\([0-7]{3})/.exec(s.slice(i,i+4));if(m){bytes.push(parseInt(m[1],8));i+=3;}else{bytes.push(...Buffer.from(s[i]));}}return Buffer.from(bytes).toString('utf8');} xml2js.parseString(fs.readFileSync(process.argv[2],'utf8'),(e,r)=>{for(const er of r.results.errors[0].error){console.log(JSON.stringify(decodeCppcheckEscapes(er.$.verbose)));}});
Commands:
cd /work/scratch; echo 'int main(){return 0;}' > a.c /work/cppcheck/cppcheck --xml --addon=winaddon.py a.c 2>out.xml >/dev/null; node dec.js out.xmlcppcheck writes the backslash unescaped:
verbose="...\012Output:\012Traceback: File C:\work\2024\src\misra.py, line 3\012tab:\011here". The decoded message is:"...\nOutput:\nTraceback: File C:\\work�4\\src\\misra.py, line 3\ntab:\there"The
\nand tab escapes are decoded correctly. The path loses\202: it is read as the byte 0x82, which is invalid UTF-8 and becomes U+FFFD.
Automated review of 9986d38 by Claude. The pull request was built and tested locally.
| export function decodeCppcheckEscapes(s: string): string { | ||
| const bytes: number[] = []; | ||
| for (let i = 0; i < s.length; i++) { | ||
| const m = /^\\([0-7]{3})/.exec(s.slice(i, i + 4)); |
There was a problem hiding this comment.
AI review
cppcheck's fixInvalidChars escapes only characters that fail isprint. A literal \ is printable, so it is written unchanged. This regex still treats any backslash followed by three octal digits as an escape. Windows paths in the addon command line or in its output get mangled whenever a path component starts with three octal digits.
Reproduced with real cppcheck output, from an addon that prints C:\work\2024\src\misra.py. The XML has ...Output:\012Traceback: File C:\work\2024\src\misra.py, and this function returns C:\work�4\src\misra.py. \202 becomes byte 0x82, which is invalid UTF-8. Similarly, C:\100files would become C:\@files.
cppcheck never emits an escape for a printable ASCII character. A safer approach is:
- Leave
\oooliteral when its value is 0x20–0x7e. - Decode a run of escapes ≥ 0x80 only when it forms valid UTF-8, for example with
new TextDecoder('utf-8', { fatal: true }), and otherwise keep the original text.
The output stays ambiguous because of cppcheck's format, but this avoids corrupting ordinary paths. A unit test with a Windows path would cover it.
No description provided.