Skip to content

Commit

Permalink
FlowChecker: Fix segfault with error reporting parameter name
Browse files Browse the repository at this point in the history
Summary:
It's possible that the parameter doesn't have a name when reporting an
error, so check before dereferencing it.

Reviewed By: tmikov

Differential Revision: D66665056

fbshipit-source-id: 7e89b8aecf2c47ab7cc24d72e074336acddc20a7
  • Loading branch information
avp authored and facebook-github-bot committed Dec 3, 2024
1 parent 92984b4 commit 2dd2314
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/Sema/FlowChecker-expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1894,10 +1894,17 @@ class FlowChecker::ExprVisitor {
auto [argTypeNarrow, cf] = tryNarrowType(argType, expectedType);

if (!cf.canFlow) {
outer_.sm_.error(
arg->getSourceRange(),
"ft: " + calleeName + " parameter '" + param.first.str() +
"' type mismatch");
if (param.first.isValid()) {
outer_.sm_.error(
arg->getSourceRange(),
"ft: " + calleeName + " parameter '" + param.first.str() +
"' type mismatch");
} else {
outer_.sm_.error(
arg->getSourceRange(),
"ft: " + calleeName + " parameter #" + llvh::Twine(argIndex + 1) +
" type mismatch");
}
return false;
}
// If a cast is needed, replace the argument with the cast.
Expand Down
19 changes: 19 additions & 0 deletions test/Sema/flow/regress-function-param-no-name-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

// RUN: (! %shermes -Werror -ferror-limit=0 -typed -dump-sema %s 2>&1 ) | %FileCheckOrRegen --match-full-lines %s

function f(cb: number => string) {
cb('a');
}

// Auto-generated content below. Please do not modify manually.

// CHECK:{{.*}}regress-function-param-no-name-error.js:11:6: error: ft: function parameter #1 type mismatch
// CHECK-NEXT: cb('a');
// CHECK-NEXT: ^~~
// CHECK-NEXT:Emitted 1 errors. exiting.

0 comments on commit 2dd2314

Please sign in to comment.