Repository: angr/claripy

Issue:
note to self: ite_dict is misbalanced (need: split_val = keys[(len(keys)-1) // 2])
### Description

a

### Steps to reproduce the bug

a

### Environment

a

### Additional context

a

Discussion:

Patch:
diff --git a/claripy/ast/bool.py b/claripy/ast/bool.py
index 0e9ab8a0..a3ba13e6 100644
--- a/claripy/ast/bool.py
+++ b/claripy/ast/bool.py
@@ -205,7 +205,7 @@ def ite_dict(i, d, default):
     # Find the median:
     keys = list(d.keys())
     keys.sort()
-    split_val = keys[len(keys) // 2]
+    split_val = keys[(len(keys) - 1) // 2]
 
     # split the dictionary
     dictLow = {c: v for c, v in d.items() if c <= split_val}


Test patch:
diff --git a/tests/test_bool.py b/tests/test_bool.py
new file mode 100644
index 00000000..0588d88b
--- /dev/null
+++ b/tests/test_bool.py
@@ -0,0 +1,36 @@
+# pylint: disable=missing-class-docstring,no-self-use
+from __future__ import annotations
+
+from unittest import TestCase
+
+from claripy import BVS, ite_dict
+
+
+class TestBool(TestCase):
+    def test_ite_dict_is_balanced(self):
+        case_even = ite_dict(
+            BVS("A", 8),
+            {
+                1: 11,
+                2: 22,
+                3: 33,
+                4: 44,
+            },
+            BVS("B", 8),
+        )
+
+        assert case_even.args[1].depth == case_even.args[2].depth
+
+        case_odd = ite_dict(
+            BVS("A", 8),
+            {
+                1: 11,
+                2: 22,
+                3: 33,
+                4: 44,
+                5: 55,
+            },
+            BVS("B", 8),
+        )
+
+        assert case_odd.args[1].depth == case_odd.args[2].depth + 1