Skip to content

ext-mongodb crashes if empty collection name #345

@xwz

Description

@xwz

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');
    }
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions