swoft-cloud / swoft

🚀 PHP Microservice Full Coroutine Framework
https://swoft.org
Apache License 2.0
5.58k stars 788 forks source link

rpc调用 swoft-limiter组件出现Undefined offset: 1的错误 #1438

Open jeristiano opened 3 years ago

jeristiano commented 3 years ago
Q A
Bug report? yes
Feature request? no
Swoft version v2.0.11
Swoole version 4.6.7
PHP version 7.2.20
Runtime environment Win10/WSL2/Docker

Describe the bug

使用swoft rpc-client 调用微服务时出现:

[ERROR] App\Exception\Handler\HttpExceptionHandler:handle(40) Undefined offset: 1. (At /var/www/html/card_rpc/vendor/swoft/rpc-client/src/Concern/ServiceTrait.php line 87)

Screenshots image

Details

定义微服务接口方法参数存在默认值,例如:encrypt (string $value, string $field = 'default'),客户端发起调用rpc服务,第二个参数($field)不传参,那么将会得到以上错误。跟踪代码发现问题出现在服务端的limiter组件

报错位置 :


// \Swoft\Limiter\RateLimter::evaluateKey
private function evaluateKey(string $key, string $className, string $method, array $params): string
    {
        $values   = [];
        $rcMethod = Reflections::get($className);
        $rcParams = $rcMethod['methods'][$method]['params'] ?? [];

        $index = 0;
        foreach ($rcParams as $rcParam) {
            [$pName] = $rcParam;
            $values[$pName] = $params[$index]; //此处由于没有对数组做越界处理,导致 Undefined offset: 2
            $index++;
        }

        // Inner vars
        $values['CLASS']  = $className;
        $values['METHOD'] = $method;

        // Parse express language
        $el = new ExpressionLanguage();
        return $el->evaluate($key, $values);
    }

How to fix

...
    $values[$pName] = $params[$index]??null; // 建议此处进行越界处理
...