yiisoft / yii2-mongodb

Yii 2 MongoDB extension
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
325 stars 191 forks source link

ext-mongodb crashes if empty collection name #345

Open xwz opened 2 years ago

xwz commented 2 years ago

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');
    }
    ...
}
samdark commented 2 years ago

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