Skip to content

Commit 0f91d37

Browse files
committed
Move DAP env validation logic into a separate util function: validateDAPEnv()
1 parent 840c9fc commit 0f91d37

File tree

1 file changed

+41
-13
lines changed

1 file changed

+41
-13
lines changed

lldb/tools/lldb-dap/src-ts/debug-adapter-factory.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,42 @@ async function findDAPExecutable(): Promise<string | undefined> {
6868
return undefined;
6969
}
7070

71+
/**
72+
* Validates the DAP environment provided in the debug configuration.
73+
* It must be a dictionary of string keys and values OR an array of string values.
74+
*
75+
* @param debugConfigEnv The supposed DAP environment that will be validated
76+
* @returns Whether or not the DAP environment is valid
77+
*/
78+
function validateDAPEnv(debugConfigEnv: any): boolean {
79+
// If the env is an object, it should have string keys and values.
80+
if (
81+
typeof debugConfigEnv === "object" &&
82+
Object.keys(debugConfigEnv).findIndex(
83+
(entry) => typeof entry !== "string",
84+
) !== -1 &&
85+
Object.values(debugConfigEnv).findIndex(
86+
(entry) => typeof entry !== "string",
87+
) !== -1
88+
) {
89+
return false;
90+
}
91+
92+
// If the env is an array, it should have string values which match the regex.
93+
if (
94+
Array.isArray(debugConfigEnv) &&
95+
debugConfigEnv.findIndex(
96+
(entry) =>
97+
typeof entry !== "string" || !/^((\\w+=.*)|^\\w+)$/.test(entry),
98+
) !== -1
99+
) {
100+
return false;
101+
}
102+
103+
// The env is valid.
104+
return true;
105+
}
106+
71107
/**
72108
* Retrieves the lldb-dap executable path either from settings or the provided
73109
* {@link vscode.DebugConfiguration}.
@@ -172,23 +208,13 @@ async function getDAPEnvironment(
172208
): Promise<{ [key: string]: string }> {
173209
const debugConfigEnv = configuration.debugAdapterEnv;
174210
if (debugConfigEnv) {
175-
if (
176-
(typeof debugConfigEnv !== "object" ||
177-
178-
Object.values(debugConfigEnv).findIndex(
179-
(entry) => typeof entry !== "string",
180-
) !== -1) &&
181-
(!Array.isArray(debugConfigEnv) ||
182-
debugConfigEnv.findIndex(
183-
(entry) =>
184-
typeof entry !== "string" || !/^((\\w+=.*)|^\\w+)$/.test(entry),
185-
) !== -1)
186-
) {
211+
if (validateDAPEnv(debugConfigEnv) === false) {
187212
throw new ErrorWithNotification(
188213
"The debugAdapterEnv property must be a dictionary of string keys and values OR an array of string values. Please update your launch configuration",
189214
new ConfigureButton(),
190215
);
191216
}
217+
192218
// Transform, so that the returned value is always a dictionary.
193219
if (Array.isArray(debugConfigEnv)) {
194220
const ret: { [key: string]: string } = {};
@@ -237,7 +263,9 @@ export async function createDebugAdapterExecutable(
237263
if (log_path) {
238264
env["LLDBDAP_LOG"] = log_path;
239265
} else if (
240-
vscode.workspace.getConfiguration("lldb-dap").get("captureSessionLogs", false)
266+
vscode.workspace
267+
.getConfiguration("lldb-dap")
268+
.get("captureSessionLogs", false)
241269
) {
242270
env["LLDBDAP_LOG"] = logFilePath.get(LogType.DEBUG_SESSION);
243271
}

0 commit comments

Comments
 (0)