set_variable: expression equality and hashing use different identities

Author: aketurbalCreated Sep 29, 2026Updated Sep 29, 2026

Details

Hi! During a static review of Karabiner-Elements 16.3.0, I noticed an equality/hash mismatch in manipulator_environment_variable_set_variable.

Reviewed commit: 9312593e1a3bf72b94c63c524ebabe2637442e8a.

  • operator== compares expression_ and key_up_expression_ through exprtk_utility::compare (lines 81–87).
  • That helper compares the expression strings when both pointers are non-null, rather than their addresses (exprtk_utility.hpp:190–196).
  • The std::hash specialization instead passes get_expression() and get_key_up_expression() directly to pqrs::hash::combine (lines 184 and 186). These return shared_ptr values, and combine delegates to std::hash, so this hashes pointer identity.

Consequently, two separately allocated expression wrappers containing the same text can produce set_variable objects that compare equal (with all other fields equal) but are not guaranteed to have equal hashes. This conflicts with the equality/hash contract.

Source for equality and hashing: https://github.com/pqrs-org/Karabiner-Elements/blob/9312593e1a3bf72b94c63c524ebabe2637442e8a/src/share/types/manipulator_environment_variable_set_variable.hpp

Expression comparison: https://github.com/pqrs-org/Karabiner-Elements/blob/9312593e1a3bf72b94c63c524ebabe2637442e8a/src/share/exprtk_utility.hpp#L190-L196

On September 29, I checked these files at main commit d130433e5854eeff125f6ed45f6ac1bdad0aeeab: both match the reviewed files, and pqrs::hash::combine still delegates to std::hash.

Would it make sense to hash the expression text for both expression fields, handling null consistently with equality? A useful regression case would construct separate wrappers with identical text and check that equal set_variable objects have equal hashes.

This is a static contract inconsistency. I have not executed that regression case or reproduced an observable effect on key events, configuration reloads, or other application behavior.

Thank you!

Source: pqrs-org/Karabiner-Elements