clone on BSON\Javascript with scope triggers C assertion failure in libbson

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Fixed
    • Priority: Major - P3
    • 2.3.0
    • Affects Version/s: 2.2.1
    • Component/s: BSON
    • None
    • None
    • PHP Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Calling PHP's clone operator on a MongoDB\BSON\Javascript object that has a scope causes the process to abort with a C-level assertion failure inside libbson. The crash is not catchable via PHP's exception or error handling.

      Javascript objects without a scope clone without issue.

      Environment

      Key Value
      ext-mongodb 2.2.1
      PHP 8.5.2 (macOS, Apple Silicon)
      libbson assertion bson_copy(): assertion failed: bson at libbson/src/bson/bson.c:2084

      Reproducer

      <?php
      
      // Works fine — no scope
      $js = new MongoDB\BSON\Javascript('function(x) { return x; }');
      $clone = clone $js;
      echo "no scope: OK\n";
      
      // Aborts the process — scope present
      $jsWithScope = new MongoDB\BSON\Javascript('function(x) { return x + n; }', ['n' => 42]);
      $clone = clone $jsWithScope; // Fatal: C assertion failure, process exits
      echo "with scope: OK\n"; // never reached
      

      Expected behavior

      Either clone succeeds and returns an independent copy, or it throws a catchable PHP Error/Exception.

      Actual behavior

      The process aborts with:

      bson_copy(): assertion failed: bson
      src/libmongoc/src/libbson/src/bson/bson.c:2084
      Abort trap: 6
      

      Root cause (hypothesis)

      The Javascript object stores its scope as a bson_t (libbson document). The __clone handler likely passes an uninitialized or already-freed bson_t * to bson_copy(), which asserts that the pointer is non-NULL and valid. The scope bson_t may not be properly initialized during the clone operation.

            Assignee:
            Jérôme Tamarelle
            Reporter:
            Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: