Skip to content

Commit 388f429

Browse files
Introduce asString to call toString() safely on process stdout/stderr
1 parent 5fe5f70 commit 388f429

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { CliConfig } from "../config";
1919
import type { DistributionProvider } from "./distribution";
2020
import { FindDistributionResultKind } from "./distribution";
2121
import {
22+
asString,
2223
assertNever,
2324
getErrorMessage,
2425
getErrorStack,
@@ -1605,8 +1606,8 @@ export function spawnServer(
16051606
command: string[],
16061607
commandArgs: string[],
16071608
logger: Logger,
1608-
stderrListener: (data: any) => void,
1609-
stdoutListener?: (data: any) => void,
1609+
stderrListener: (data: unknown) => void,
1610+
stdoutListener?: (data: unknown) => void,
16101611
progressReporter?: ProgressReporter,
16111612
): ChildProcessWithoutNullStreams {
16121613
// Enable verbose logging.
@@ -1626,7 +1627,7 @@ export function spawnServer(
16261627
);
16271628
}
16281629

1629-
let lastStdout: any = undefined;
1630+
let lastStdout: unknown = undefined;
16301631
child.stdout!.on("data", (data) => {
16311632
lastStdout = data;
16321633
});
@@ -1643,7 +1644,7 @@ export function spawnServer(
16431644
// If the process exited abnormally, log the last stdout message,
16441645
// It may be from the jvm.
16451646
if (code !== 0 && lastStdout !== undefined) {
1646-
void logger.log(`Last stdout was "${lastStdout.toString()}"`);
1647+
void logger.log(`Last stdout was "${asString(lastStdout)}"`);
16471648
}
16481649
});
16491650
child.stderr!.on("data", stderrListener);

extensions/ql-vscode/src/common/helpers-pure.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ export function asError(e: unknown): Error {
6868
return e instanceof Error ? e : new Error(String(e));
6969
}
7070

71+
/**
72+
* Converts a unknown value to a string, using `toString` if possible,
73+
* or returning the human-readable strings "null" or "undefined".
74+
*/
75+
export function asString(x: unknown): string {
76+
if (x === null) {
77+
return "null";
78+
} else if (x === undefined) {
79+
return "undefined";
80+
} else {
81+
return x.toString();
82+
}
83+
}
84+
7185
/**
7286
* Get error message when the error may have come from a method from the `child_process` module.
7387
*/

extensions/ql-vscode/src/language-support/language-client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LanguageClient, NotificationType } from "vscode-languageclient/node";
55
import { shouldDebugLanguageServer, spawnServer } from "../codeql-cli/cli";
66
import type { QueryServerConfig } from "../config";
77
import { languageServerLogger } from "../common/logging/vscode";
8+
import { asString } from "../common/helpers-pure";
89

910
/**
1011
* Managing the language client and corresponding server process for CodeQL.
@@ -74,9 +75,9 @@ async function spawnLanguageServer(
7475
args,
7576
languageServerLogger,
7677
(data) =>
77-
languageServerLogger.log(data.toString(), { trailingNewline: false }),
78+
languageServerLogger.log(asString(data), { trailingNewline: false }),
7879
(data) =>
79-
languageServerLogger.log(data.toString(), { trailingNewline: false }),
80+
languageServerLogger.log(asString(data), { trailingNewline: false }),
8081
progressReporter,
8182
);
8283
return { writer: child.stdin!, reader: child.stdout! };

extensions/ql-vscode/src/query-server/query-server-client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type { ProgressCallback, ProgressTask } from "../common/vscode/progress";
1818
import { withProgress } from "../common/vscode/progress";
1919
import { ServerProcess } from "./server-process";
2020
import type { App } from "../common/app";
21+
import { asString } from "../common/helpers-pure";
2122

2223
type ServerOpts = {
2324
logger: Logger;
@@ -214,7 +215,7 @@ export class QueryServerClient extends DisposableObject {
214215
args,
215216
this.logger,
216217
(data) =>
217-
this.activeQueryLogger.log(data.toString(), {
218+
this.activeQueryLogger.log(asString(data), {
218219
trailingNewline: false,
219220
}),
220221
undefined, // no listener for stdout

0 commit comments

Comments
 (0)