Check if a hook array entry is ours (current matcher or a known old one). * Matcher identity is never sufficient because users commonly choose the same * catch-all or lifecycle matchers. Ownership always requires the exact command * bytes installed by this version. */
| 4623 | * catch-all or lifecycle matchers. Ownership always requires the exact command |
| 4624 | * bytes installed by this version. */ |
| 4625 | static bool find_cmm_hook_in_entry(yyjson_mut_val *entry, const char *matcher_str, |
| 4626 | const char *const *old_matchers, |
| 4627 | const char *require_command_exact, |
| 4628 | const char *const *require_args, size_t require_arg_count, |
| 4629 | const char *const *old_commands, size_t *hook_index_out) { |
| 4630 | if (!entry || !require_command_exact) { |
| 4631 | return false; |
| 4632 | } |
| 4633 | if (matcher_str) { |
| 4634 | yyjson_mut_val *matcher = yyjson_mut_obj_get(entry, "matcher"); |
| 4635 | if (!matcher || !yyjson_mut_is_str(matcher)) { |
| 4636 | return false; |
| 4637 | } |
| 4638 | const char *val = yyjson_mut_get_str(matcher); |
| 4639 | if (!val) { |
| 4640 | return false; |
| 4641 | } |
| 4642 | bool matcher_ok = strcmp(val, matcher_str) == 0; |
| 4643 | /* Also match old versions for backwards-compatible upgrade */ |
| 4644 | for (int i = 0; !matcher_ok && old_matchers && old_matchers[i]; i++) { |
| 4645 | if (strcmp(val, old_matchers[i]) == 0) { |
| 4646 | matcher_ok = true; |
| 4647 | } |
| 4648 | } |
| 4649 | if (!matcher_ok) { |
| 4650 | return false; |
| 4651 | } |
| 4652 | } |
| 4653 | yyjson_mut_val *hooks = yyjson_mut_obj_get(entry, "hooks"); |
| 4654 | if (!hooks || !yyjson_mut_is_arr(hooks)) { |
| 4655 | return false; |
| 4656 | } |
| 4657 | size_t idx; |
| 4658 | size_t max; |
| 4659 | yyjson_mut_val *h; |
| 4660 | yyjson_mut_arr_foreach(hooks, idx, max, h) { |
| 4661 | yyjson_mut_val *cmd = yyjson_mut_is_obj(h) ? yyjson_mut_obj_get(h, "command") : NULL; |
| 4662 | yyjson_mut_val *type = yyjson_mut_is_obj(h) ? yyjson_mut_obj_get(h, "type") : NULL; |
| 4663 | if (cmd && yyjson_mut_is_str(cmd) && type && yyjson_mut_is_str(type) && |
| 4664 | strcmp(yyjson_mut_get_str(type), "command") == 0) { |
| 4665 | const char *cs = yyjson_mut_get_str(cmd); |
| 4666 | bool command_ok = cs && strcmp(cs, require_command_exact) == 0 && |
| 4667 | cmm_hook_args_equal(h, require_args, require_arg_count); |
| 4668 | for (size_t i = 0U; !command_ok && cs && old_commands && old_commands[i]; i++) { |
| 4669 | command_ok = strcmp(cs, old_commands[i]) == 0 && cmm_hook_args_equal(h, NULL, 0U); |
| 4670 | } |
| 4671 | if (command_ok) { |
| 4672 | if (hook_index_out) { |
| 4673 | *hook_index_out = idx; |
| 4674 | } |
| 4675 | return true; |
| 4676 | } |
| 4677 | } |
| 4678 | } |
| 4679 | return false; |
| 4680 | } |
| 4681 | |
| 4682 | static bool cmm_hook_outer_is_canonical(yyjson_mut_val *entry, bool has_matcher) { |
no test coverage detected