about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorserpilliere <serpilliere@users.noreply.github.com>2020-06-16 16:59:56 +0200
committerGitHub <noreply@github.com>2020-06-16 16:59:56 +0200
commit09b4265344ea987d0a1e78559711f86c0985b574 (patch)
treefb317cf9be774683ba022534b6aba82a4e2b106c
parent975a527b4f268c4e41ab5ab259442c0941da6709 (diff)
parentf134c523757b3859aba2ba3e060a922995a84c08 (diff)
downloadmiasm-09b4265344ea987d0a1e78559711f86c0985b574.tar.gz
miasm-09b4265344ea987d0a1e78559711f86c0985b574.zip
Merge pull request #1256 from w4kfu/bitfield_fix
Fix issue #1255 ; Add unit test
-rw-r--r--miasm/core/types.py2
-rwxr-xr-xtest/core/test_types.py25
2 files changed, 26 insertions, 1 deletions
diff --git a/miasm/core/types.py b/miasm/core/types.py
index 466d136d..4f99627d 100644
--- a/miasm/core/types.py
+++ b/miasm/core/types.py
@@ -994,7 +994,7 @@ class BitField(Union):
         for name, bits in bit_list:
             fields.append((name, Bits(self._num, bits, offset)))
             offset += bits
-        if offset > self._num.size == 8:
+        if offset > self._num.size * 8:
             raise ValueError("sum of bit lengths is > to the backing num size")
         super(BitField, self).__init__(fields)
 
diff --git a/test/core/test_types.py b/test/core/test_types.py
index 1b15630c..13a7824e 100755
--- a/test/core/test_types.py
+++ b/test/core/test_types.py
@@ -351,6 +351,31 @@ assert bit.flags.f2_5 == 0b01110
 assert bit.flags.f3_8 == 0b01010101
 assert bit.flags.f4_1 == 1
 
+try:
+    class BitStruct(MemUnion):
+        fields = [
+            ("ValueB", BitField(Num("<Q"), [
+                ("field_00", 32),
+                ("field_01", 32),
+            ])),
+            ("Value", Num("<Q")),
+        ]
+except ValueError:
+    assert False, "Should not raise"
+
+try:
+    class BitStruct(MemUnion):
+        fields = [
+            ("ValueB", BitField(Num("<Q"), [
+                ("field_00", 32),
+                ("field_01", 32),
+                ("field_02", 1),
+            ])),
+            ("Value", Num("<Q")),
+        ]
+    assert False, "Should raise"
+except ValueError:
+    pass
 
 # Unhealthy ideas
 class UnhealthyIdeas(MemStruct):