Skip to content

Commit 2099976

Browse files
authored
Fix processing of bits type (#364)
* Add a default of -1 to max(allowed_bits), to cover cases where allowed_bits is empty (e.g. in the absence of any position statements). This will yield an initial implicit position of 0. * allowed_bits is a dict. Take the maximum of its values. * Raise ValueError, if pos exceeds its bounds. * Add test case.
1 parent cc02b9f commit 2099976

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

pyangbind/plugin/pybind.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,9 @@ def build_elemtype(ctx, et, prefix=False):
13591359
if position is not None:
13601360
pos = position.arg
13611361
else:
1362-
pos = 1 + max(allowed_bits)
1362+
pos = 1 + max(allowed_bits.values(), default=-1)
1363+
if pos < 0 or 4294967295 < pos:
1364+
raise ValueError("position out of bounds")
13631365
allowed_bits[bit.arg] = pos
13641366
cls = "restricted-bits"
13651367
elemtype = {

tests/bits/bits.yang

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ module bits {
4646
}
4747
default "flag1 flag3";
4848
}
49+
50+
// a leaf containing the bits type definition without position statement
51+
leaf bits3 {
52+
type bits {
53+
bit position0;
54+
}
55+
}
4956
}

tests/bits/run.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ def test_bits_position(self):
5656
self.instance.bits2.add("foo")
5757
self.assertEqual(str(self.instance.bits2), "foo bar baz")
5858

59+
def test_bits_no_position(self):
60+
self.assertEqual(self.instance.bits3._allowed_bits["position0"], 0)
61+
5962

6063
if __name__ == "__main__":
6164
unittest.main()

0 commit comments

Comments
 (0)