Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-2061

decode_file_iter raises ValueError instead of InvalidBSON when reading an improperly-formatted size

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 3.10
    • Affects Version/s: None
    • Component/s: BSON
    • Labels:
      None
    • Environment:
      Various Linuxes (Ubuntu 19.04, Rasbpian Buster), Python 3.x
    • Fully Compatible

      When reading an actual file with decode_file_iter, reading a stretch of bytes with an invalid size will raise an unexpected ValueError, rather than the expected InvalidBSON.

      To reproduce:

      import bson
      with open('example.bson', 'wb') as outfile:
        outfile.write(b'\0\0\0\0\0\0\0\0')
      with open('example.bson', 'rb') as infile:
      {{ list(bson.decode_file_iter(infile))}}
      # This will raise 'ValueError: read length must be non-negative or -1'

      The expected result is to raise an InvalidBSON, which is what the tests think will be raised. However, StringIO is more permissive than raw file input, in that it will allow reading arbitrary negative lengths.

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            paul@pfish.zone Paul Fisher
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: