Skip to content

Commit 4cb4b23

Browse files
bors[bot]matklad
andauthored
Merge #9084 #9087
9084: fix: avoid panics in match case diagnostic r=matklad a=matklad bors r+ 🤖 closes #8809 9087: fix: fix shell injection in task spawning r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
3 parents 020610f + 159922d + ee995db commit 4cb4b23

File tree

3 files changed

+43
-118
lines changed

3 files changed

+43
-118
lines changed

crates/hir_ty/src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ impl fmt::Display for CaseType {
349349

350350
#[derive(Debug)]
351351
pub enum IdentType {
352-
Argument,
353352
Constant,
354353
Enum,
355354
Field,
356355
Function,
356+
Parameter,
357357
StaticVariable,
358358
Structure,
359359
Variable,
@@ -363,11 +363,11 @@ pub enum IdentType {
363363
impl fmt::Display for IdentType {
364364
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
365365
let repr = match self {
366-
IdentType::Argument => "Argument",
367366
IdentType::Constant => "Constant",
368367
IdentType::Enum => "Enum",
369368
IdentType::Field => "Field",
370369
IdentType::Function => "Function",
370+
IdentType::Parameter => "Parameter",
371371
IdentType::StaticVariable => "Static variable",
372372
IdentType::Structure => "Structure",
373373
IdentType::Variable => "Variable",

crates/hir_ty/src/diagnostics/decl_check.rs

Lines changed: 35 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -150,29 +150,11 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
150150
expected_case: CaseType::LowerSnakeCase,
151151
});
152152

153-
// Check the param names.
154-
let fn_param_replacements = body
155-
.params
156-
.iter()
157-
.filter_map(|&id| match &body[id] {
158-
Pat::Bind { name, .. } => Some(name),
159-
_ => None,
160-
})
161-
.filter_map(|param_name| {
162-
Some(Replacement {
163-
current_name: param_name.clone(),
164-
suggested_text: to_lower_snake_case(&param_name.to_string())?,
165-
expected_case: CaseType::LowerSnakeCase,
166-
})
167-
})
168-
.collect();
169-
170153
// Check the patterns inside the function body.
154+
// This includes function parameters.
171155
let pats_replacements = body
172156
.pats
173157
.iter()
174-
// We aren't interested in function parameters, we've processed them above.
175-
.filter(|(pat_idx, _)| !body.params.contains(&pat_idx))
176158
.filter_map(|(id, pat)| match pat {
177159
Pat::Bind { name, .. } => Some((id, name)),
178160
_ => None,
@@ -190,11 +172,10 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
190172
.collect();
191173

192174
// If there is at least one element to spawn a warning on, go to the source map and generate a warning.
193-
self.create_incorrect_case_diagnostic_for_func(
194-
func,
195-
fn_name_replacement,
196-
fn_param_replacements,
197-
);
175+
if let Some(fn_name_replacement) = fn_name_replacement {
176+
self.create_incorrect_case_diagnostic_for_func(func, fn_name_replacement);
177+
}
178+
198179
self.create_incorrect_case_diagnostic_for_variables(func, pats_replacements);
199180
}
200181

@@ -203,100 +184,34 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
203184
fn create_incorrect_case_diagnostic_for_func(
204185
&mut self,
205186
func: FunctionId,
206-
fn_name_replacement: Option<Replacement>,
207-
fn_param_replacements: Vec<Replacement>,
187+
fn_name_replacement: Replacement,
208188
) {
209-
// XXX: only look at sources if we do have incorrect names
210-
if fn_name_replacement.is_none() && fn_param_replacements.is_empty() {
211-
return;
212-
}
213-
214189
let fn_loc = func.lookup(self.db.upcast());
215190
let fn_src = fn_loc.source(self.db.upcast());
216191

217192
// Diagnostic for function name.
218-
if let Some(replacement) = fn_name_replacement {
219-
let ast_ptr = match fn_src.value.name() {
220-
Some(name) => name,
221-
None => {
222-
never!(
223-
"Replacement ({:?}) was generated for a function without a name: {:?}",
224-
replacement,
225-
fn_src
226-
);
227-
return;
228-
}
229-
};
230-
231-
let diagnostic = IncorrectCase {
232-
file: fn_src.file_id,
233-
ident_type: IdentType::Function,
234-
ident: AstPtr::new(&ast_ptr),
235-
expected_case: replacement.expected_case,
236-
ident_text: replacement.current_name.to_string(),
237-
suggested_text: replacement.suggested_text,
238-
};
239-
240-
self.sink.push(diagnostic);
241-
}
242-
243-
// Diagnostics for function params.
244-
let fn_params_list = match fn_src.value.param_list() {
245-
Some(params) => params,
193+
let ast_ptr = match fn_src.value.name() {
194+
Some(name) => name,
246195
None => {
247-
always!(
248-
fn_param_replacements.is_empty(),
249-
"Replacements ({:?}) were generated for a function parameters which had no parameters list: {:?}",
250-
fn_param_replacements,
196+
never!(
197+
"Replacement ({:?}) was generated for a function without a name: {:?}",
198+
fn_name_replacement,
251199
fn_src
252200
);
253201
return;
254202
}
255203
};
256-
let mut fn_params_iter = fn_params_list.params();
257-
for param_to_rename in fn_param_replacements {
258-
// We assume that parameters in replacement are in the same order as in the
259-
// actual params list, but just some of them (ones that named correctly) are skipped.
260-
let ast_ptr: ast::Name = loop {
261-
match fn_params_iter.next() {
262-
Some(element) => {
263-
if let Some(ast::Pat::IdentPat(pat)) = element.pat() {
264-
if pat.to_string() == param_to_rename.current_name.to_string() {
265-
if let Some(name) = pat.name() {
266-
break name;
267-
}
268-
// This is critical. If we consider this parameter the expected one,
269-
// it **must** have a name.
270-
never!(
271-
"Pattern {:?} equals to expected replacement {:?}, but has no name",
272-
element,
273-
param_to_rename
274-
);
275-
return;
276-
}
277-
}
278-
}
279-
None => {
280-
never!(
281-
"Replacement ({:?}) was generated for a function parameter which was not found: {:?}",
282-
param_to_rename, fn_src
283-
);
284-
return;
285-
}
286-
}
287-
};
288204

289-
let diagnostic = IncorrectCase {
290-
file: fn_src.file_id,
291-
ident_type: IdentType::Argument,
292-
ident: AstPtr::new(&ast_ptr),
293-
expected_case: param_to_rename.expected_case,
294-
ident_text: param_to_rename.current_name.to_string(),
295-
suggested_text: param_to_rename.suggested_text,
296-
};
205+
let diagnostic = IncorrectCase {
206+
file: fn_src.file_id,
207+
ident_type: IdentType::Function,
208+
ident: AstPtr::new(&ast_ptr),
209+
expected_case: fn_name_replacement.expected_case,
210+
ident_text: fn_name_replacement.current_name.to_string(),
211+
suggested_text: fn_name_replacement.suggested_text,
212+
};
297213

298-
self.sink.push(diagnostic);
299-
}
214+
self.sink.push(diagnostic);
300215
}
301216

302217
/// Given the information about incorrect variable names, looks up into the source code
@@ -327,20 +242,25 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
327242
None => continue,
328243
};
329244

245+
let is_param = ast::Param::can_cast(parent.kind());
246+
330247
// We have to check that it's either `let var = ...` or `var @ Variant(_)` statement,
331248
// because e.g. match arms are patterns as well.
332249
// In other words, we check that it's a named variable binding.
333250
let is_binding = ast::LetStmt::can_cast(parent.kind())
334251
|| (ast::MatchArm::can_cast(parent.kind())
335252
&& ident_pat.at_token().is_some());
336-
if !is_binding {
253+
if !(is_param || is_binding) {
337254
// This pattern is not an actual variable declaration, e.g. `Some(val) => {..}` match arm.
338255
continue;
339256
}
340257

258+
let ident_type =
259+
if is_param { IdentType::Parameter } else { IdentType::Variable };
260+
341261
let diagnostic = IncorrectCase {
342262
file: source_ptr.file_id,
343-
ident_type: IdentType::Variable,
263+
ident_type,
344264
ident: AstPtr::new(&name_ast),
345265
expected_case: replacement.expected_case,
346266
ident_text: replacement.current_name.to_string(),
@@ -408,7 +328,7 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
408328
struct_name_replacement: Option<Replacement>,
409329
struct_fields_replacements: Vec<Replacement>,
410330
) {
411-
// XXX: only look at sources if we do have incorrect names
331+
// XXX: Only look at sources if we do have incorrect names.
412332
if struct_name_replacement.is_none() && struct_fields_replacements.is_empty() {
413333
return;
414334
}
@@ -723,10 +643,10 @@ fn NonSnakeCaseName() {}
723643
check_diagnostics(
724644
r#"
725645
fn foo(SomeParam: u8) {}
726-
// ^^^^^^^^^ Argument `SomeParam` should have snake_case name, e.g. `some_param`
646+
// ^^^^^^^^^ Parameter `SomeParam` should have snake_case name, e.g. `some_param`
727647
728648
fn foo2(ok_param: &str, CAPS_PARAM: u8) {}
729-
// ^^^^^^^^^^ Argument `CAPS_PARAM` should have snake_case name, e.g. `caps_param`
649+
// ^^^^^^^^^^ Parameter `CAPS_PARAM` should have snake_case name, e.g. `caps_param`
730650
"#,
731651
);
732652
}
@@ -1037,4 +957,9 @@ fn qualify() {
1037957
"#,
1038958
)
1039959
}
960+
961+
#[test] // Issue #8809.
962+
fn parenthesized_parameter() {
963+
check_diagnostics(r#"fn f((O): _) {}"#)
964+
}
1040965
}

editors/code/src/tasks.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function buildCargoTask(
8080
throwOnError: boolean = false
8181
): Promise<vscode.Task> {
8282

83-
let exec: vscode.ShellExecution | undefined = undefined;
83+
let exec: vscode.ProcessExecution | vscode.ShellExecution | undefined = undefined;
8484

8585
if (customRunner) {
8686
const runnerCommand = `${customRunner}.buildShellExecution`;
@@ -105,13 +105,13 @@ export async function buildCargoTask(
105105

106106
if (!exec) {
107107
// Check whether we must use a user-defined substitute for cargo.
108-
const cargoCommand = definition.overrideCargo ? definition.overrideCargo : toolchain.cargoPath();
108+
// Split on spaces to allow overrides like "wrapper cargo".
109+
const overrideCargo = definition.overrideCargo ?? definition.overrideCargo;
110+
const cargoCommand = overrideCargo?.split(" ") ?? [toolchain.cargoPath()];
109111

110-
// Prepare the whole command as one line. It is required if user has provided override command which contains spaces,
111-
// for example "wrapper cargo". Without manual preparation the overridden command will be quoted and fail to execute.
112-
const fullCommand = [cargoCommand, ...args].join(" ");
112+
const fullCommand = [...cargoCommand, ...args];
113113

114-
exec = new vscode.ShellExecution(fullCommand, definition);
114+
exec = new vscode.ProcessExecution(fullCommand[0], fullCommand.slice(1), definition);
115115
}
116116

117117
return new vscode.Task(

0 commit comments

Comments
 (0)