Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enum] verify'ing a Flag class with negative values results in MemoryError #99248

Closed
udhayprakash opened this issue Nov 8, 2022 · 2 comments
Closed
Assignees
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@udhayprakash
Copy link

udhayprakash commented Nov 8, 2022

Bug report

A clear and concise description of what the bug is.
Include a minimal, reproducible example (https://stackoverflow.com/help/minimal-reproducible-example), if possible.

Your environment

from enum import Flag, verify, NAMED_FLAGS

@verify(NAMED_FLAGS)
class Color(Flag):
    RED = -1 # 0
    GREEN = 2
    BLUE = 4
    WHITE = 9 
    NEON = 6

In this example, it is working good, if RED has value of 0, but is resulting in Memory Error, if its value is changed to -1

  • CPython versions tested on: 3.11.0
  • Operating system and architecture: Windows 10, 64 bit
@udhayprakash udhayprakash added the type-bug An unexpected behavior, bug, or error label Nov 8, 2022
@JelleZijlstra JelleZijlstra added the performance Performance or resource usage label Nov 8, 2022
@JelleZijlstra
Copy link
Member

The problem is that enum._iter_bits_lsb (

def _iter_bits_lsb(num):
) doesn't terminate for negative numbers, the number just grows unbounded.

Negative numbers don't really make sense as OR-able flags (which are conceptually unsigned), but we should fail with an obvious error instead of using all available memory.

cc @ethanfurman for enums

@AlexWaygood AlexWaygood added the stdlib Python modules in the Lib dir label Nov 8, 2022
@ethanfurman ethanfurman self-assigned this Nov 8, 2022
@ethanfurman ethanfurman changed the title Enum Flag class with one negative value is resulting in Memory Error [Enum] verify'ing a Flag class with negative values results in MemoryError Nov 8, 2022
ethanfurman added a commit that referenced this issue Nov 8, 2022
[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
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Nov 8, 2022
)

[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 0b4ffb0)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
miss-islington added a commit that referenced this issue Nov 8, 2022
[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 0b4ffb0)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
@ethanfurman
Copy link
Member

Thanks for the report. Fix merged into main and 3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants