gh-99248: [Enum] fix negative number infinite loop (GH-99256)

[Enum] fix negative number infinite loop

- _iter_bits_lsb() now raises a ValueError if a negative number
  is passed in

- verify() now skips checking negative numbers for named flags
(cherry picked from commit 0b4ffb08cc)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
Miss Islington (bot) 2022-11-08 12:33:21 -08:00 committed by GitHub
parent ca944628ac
commit 4f31171e3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 2 deletions

View file

@ -114,9 +114,12 @@ def _make_class_unpicklable(obj):
setattr(obj, '__module__', '<unknown>')
def _iter_bits_lsb(num):
# num must be an integer
# num must be a positive integer
original = num
if isinstance(num, Enum):
num = num.value
if num < 0:
raise ValueError('%r is not a positive integer' % original)
while num:
b = num & (~num + 1)
yield b
@ -1856,6 +1859,9 @@ class verify:
if name in member_names:
# not an alias
continue
if alias.value < 0:
# negative numbers are not checked
continue
values = list(_iter_bits_lsb(alias.value))
missed = [v for v in values if v not in member_values]
if missed: