Skip to content

SimpleDecoder.__getitem__ incorrectly validates exclusive [stop] of Python slice #5

Description

@NikilXYZ

Library version: PyNvVideoCodec 2.1.0 (pip, pynvvideocodec==2.1.0)

Bug:
SimpleDecoder.__getitem__ calls self.validate_index(key.stop) on the exclusive stop of a Python slice. This raises IndexError when stop == len(decoder), which is the normal Python convention for "all remaining elements."

Location: SimpleDecoder.py, lines 88–94

elif isinstance(key, slice):
    indices = range(key.start, key.stop, key.step)  # stop is exclusive here ✓
    idxs = list(iter(indices))
    if not idxs:
        raise ValueError(f"Invalid input.")
    self.validate_index(key.start)
    self.validate_index(key.stop)      # ← BUG: validates exclusive stop as if inclusive
    return self.simple_decoder[idxs]

Reproduce:

import PyNvVideoCodec as nvc

dec = nvc.SimpleDecoder("any_video.mp4", output_color_type=nvc.OutputColorType.RGB)
n = len(dec)       # e.g. 107
frames = dec[0:n:1]  # IndexError: Index 107 is out of range [0, 106]

Expected: dec[0:n:1] should return all n frames, consistent with standard Python slice semantics where stop is exclusive.

Fix:
self.validate_index(key.stop - 1). Also, I'd probably move the self.validate(key.start) and self.validate(key.stop) before the whole range to list to reduce unnecessary computation before you know the range is safe.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions