Skip to content

Internal errors only show exitcode is 1 not the addons output - #166

Open
davidramnero wants to merge 2 commits into
mainfrom
fix-/-#164-Internal-errors-only-show-exitcode-is-1-not-the-addons-output
Open

davidramnero wants to merge 2 commits into
mainfrom
fix-/-#164-Internal-errors-only-show-exitcode-is-1-not-the-addons-output

Conversation

@davidramnero

Copy link
Copy Markdown
Collaborator

No description provided.

@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

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 compile and npm run lint in /work/pr: both clean.
  • Test suite: the npx vscode-test CLI does not run on the container's Node 18 (SyntaxError: Invalid regular expression flags in string-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 used useInstallation: /vscode/code and --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 &lt;&amp;&gt;".
    The PR's replaceAll('\\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):
    const message = e.$.verbose ? e.$.verbose.replaceAll('\\012', '\n') : e.$.msg;
    Output:
    "...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"
    
    The ö and the tab are still shown as \303\266 and \011 in the diagnostic.

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

Comment thread src/extension.ts Outdated
// 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;

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

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 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

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 ⚠️ Open
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 compile and npm run lint in /work/pr: both clean.

  • Test suite: ran it through VS Code's Electron, because the vscode-test CLI 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 set files: '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 exercise decodeCppcheckEscapes.

  • 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.xml
    

    cppcheck 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 \n and 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));

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

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 \ooo literal 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.

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.

2 participants