ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 4.20.0
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • Python Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

      Detailed steps to reproduce the problem?

      Include full traceback, if possible

      The sanitizer Evergreen task (PYTHON-6048 patch) dies silently (exit 1, no output) at test/test_bson.py::TestBSON::test_encode_size_limit: buffer_grow in bson/buffer.c doubles an int size while searching for a buffer capacity, and a document whose encode buffer must grow past 1GiB evaluates 2^30 * 2, overflowing signed int, which is undefined behavior. The size <= old_size guard corrects the wrapped value, so gcc- and MSVC-built extensions behave correctly and the test passes uninstrumented, but computing the wrap is itself UB and UBSan with halt_on_error=1 (as the task configures) halts the process; pytest's fd-level capture swallows the report.

      Standalone reproduction of the verbatim loop, compiled with gcc -fsanitize=undefined and UBSAN_OPTIONS=halt_on_error=1:

      runtime error: signed integer overflow: 1073741824 * 2 cannot be represented in type 'int'
      

      Definition of done: what must be done to consider the task complete?

      • The doubling loop never evaluates an overflowing multiplication: growth past the doubling bound goes to exactly the requested size, matching the wrap-guard's outcome today.
      • Uninstrumented behavior is unchanged: the BSON suite passes and over-limit encodes still raise ValueError.
      • The PYTHON-6048 sanitizer task runs test_encode_size_limit to completion under UBSAN_OPTIONS=halt_on_error=1.

      The exact Python version used, with patch level:

      Version-independent (C extension arithmetic). Observed under CPython 3.14.7.

      The exact version of PyMongo used, with patch level:

      Present in all versions with the C extension. Observed at 4.20.0.dev0 (16d94e2b); requires pymongo.has_c().

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      Not applicable; the defect is client-side and triggers before anything is sent to a server.

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      Compiler-dependent, not OS-dependent. Discovered on the rhel8 sanitizer variant (gcc-built extension, UBSan-instrumented interpreter).

      Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)

      Not applicable.

      Security Vulnerabilities

      Not a vulnerability, it is residual UB from the PYTHON-5996. No memory corruption and no shipped-build misbehavior; the worst case from a compiler exploiting the UB is a hang during a self-initiated >1GiB encode. The fix removes the last overflow-dependent arithmetic in the file.

            Assignee:
            Steve Silvester
            Reporter:
            Steve Silvester
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: