yii2-mongodb icon indicating copy to clipboard operation
yii2-mongodb copied to clipboard

ext-mongodb crashes if empty collection name

Open xwz opened this issue 3 years ago • 1 comments

What steps will reproduce the problem?

<?php
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');

use yii\mongodb\Connection;

$connection = new Connection(['dsn' => 'mongodb://localhost:27017/test']);
var_dump($connection->getCollection('')->findOne());

What's expected?

Throw an exception

What do you get instead?

PHP crashes

mongo-c-driver/work/mongo-c-driver-1.17.1/src/libmongoc/src/mongoc/mongoc-cursor.c:1118
_mongoc_cursor_collection(): precondition failed: *collection_len > 0

Possible fix: in yii2-mongodb/src/Database.php

public function getCollection($name, $refresh = false)
{
    if (empty($name)) {
        throw new InvalidParamException('A non-empty collection "$name" is required');
    }
    ...
}

xwz avatar Nov 20 '21 02:11 xwz

That's a bit werid behavior for a mongodb driver. A check for empty collection won't hurt anyway.

samdark avatar Nov 21 '21 13:11 samdark