Skip to content

Commit

Permalink
Fix bug with more than 16 Dynamic parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
schungx committed Nov 28, 2023
1 parent 02ea03e commit 3350517
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
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

0 comments on commit 3350517

Please sign in to comment.