Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic parameters bug. #787

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Bug fixes
* Fixed crash when parsing unterminated comment (found via fuzzing).
* Fixed crash when parsing deeply-nested right-associated operators such as `**` (found via fuzzing).
* Fixed crash when parsing combo-chaining expressions such as `(a.b).c` (found via fuzzing).
* Fixed crash when calling functions that have `Dynamic` parameters with more than 16 parameters (found via fuzzing).

Deprecated API's
----------------
Expand Down
33 changes: 19 additions & 14 deletions src/func/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,30 +235,32 @@ impl Engine {
}

// Check `Dynamic` parameters for functions with parameters
let max_dynamic_count = usize::min(num_args, MAX_DYNAMIC_PARAMETERS);

if allow_dynamic && max_bitmask == 0 && num_args > 0 {
let is_dynamic = self
let has_dynamic = self
.global_modules
.iter()
.any(|m| m.may_contain_dynamic_fn(hash_base));

#[cfg(not(feature = "no_function"))]
let is_dynamic = is_dynamic
let has_dynamic = has_dynamic
|| _global
.lib
.iter()
.any(|m| m.may_contain_dynamic_fn(hash_base));

#[cfg(not(feature = "no_module"))]
let is_dynamic = is_dynamic
let has_dynamic = has_dynamic
|| _global.may_contain_dynamic_fn(hash_base)
|| self
.global_sub_modules
.values()
.any(|m| m.may_contain_dynamic_fn(hash_base));

// Set maximum bitmask when there are dynamic versions of the function
if is_dynamic {
max_bitmask = 1usize << usize::min(num_args, MAX_DYNAMIC_PARAMETERS);
if has_dynamic {
max_bitmask = 1usize << max_dynamic_count;
}
}

Expand Down Expand Up @@ -310,12 +312,13 @@ impl Engine {
hash = calc_fn_hash_full(
hash_base,
args.as_ref().unwrap().iter().enumerate().map(|(i, a)| {
let mask = 1usize << (num_args - i - 1);
if bitmask & mask == 0 {
a.type_id()
} else {
if i < max_dynamic_count
&& bitmask & (1usize << (max_dynamic_count - i - 1)) != 0
{
// Replace with `Dynamic`
TypeId::of::<Dynamic>()
} else {
a.type_id()
}
}),
);
Expand Down Expand Up @@ -1490,20 +1493,22 @@ impl Engine {
// (expected because closures cannot be qualified).
if func.is_none() && !args.is_empty() {
let num_args = args.len();
let max_bitmask = 1usize << usize::min(num_args, MAX_DYNAMIC_PARAMETERS);
let max_dynamic_count = usize::min(num_args, MAX_DYNAMIC_PARAMETERS);
let max_bitmask = 1usize << max_dynamic_count;
let mut bitmask = 1usize; // Bitmask of which parameter to replace with `Dynamic`

// Try all permutations with `Dynamic` wildcards
while bitmask < max_bitmask {
let hash_qualified_fn = calc_fn_hash_full(
hash,
args.iter().enumerate().map(|(i, a)| {
let mask = 1usize << (num_args - i - 1);
if bitmask & mask == 0 {
a.type_id()
} else {
if i < max_dynamic_count
&& bitmask & (1usize << (max_dynamic_count - i - 1)) != 0
{
// Replace with `Dynamic`
TypeId::of::<Dynamic>()
} else {
a.type_id()
}
}),
);
Expand Down
Loading