Skip to content

Commit 9788e23

Browse files
committed
Fix GH-5696. When Redis formatting fails, an exception must be thrown instead of logging an error.
1 parent 6870611 commit 9788e23

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

Diff for: ext-src/swoole_redis_server.cc

+19-9
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ static void redis_response_format_array_item(String *buf, zval *item) {
273273
redis_response_format(buf, Redis::REPLY_MAP, item);
274274
}
275275
break;
276+
case IS_NULL:
277+
redis_response_format(buf, Redis::REPLY_NIL, item);
278+
break;
276279
default:
277280
redis_response_format(buf, Redis::REPLY_STRING, item);
278281
break;
@@ -299,23 +302,29 @@ static bool redis_response_format(String *buf, zend_long type, zval *value) {
299302
} else if (type == Redis::REPLY_STRING) {
300303
if (!value) {
301304
_no_value:
302-
php_swoole_fatal_error(E_WARNING, "require more parameters");
305+
zend_throw_exception(swoole_exception_ce, "require more parameters", SW_ERROR_INVALID_PARAMS);
303306
return false;
304307
}
305308
zend::String str_value(value);
306-
if (str_value.len() > SW_REDIS_MAX_STRING_SIZE || str_value.len() < 1) {
307-
php_swoole_fatal_error(E_WARNING, "invalid string size");
309+
if (sw_unlikely(str_value.len() > SW_REDIS_MAX_STRING_SIZE)) {
310+
zend_throw_exception(swoole_exception_ce,
311+
"the length of given string exceeds the maximum allowed value",
312+
SW_ERROR_INVALID_PARAMS);
308313
return false;
314+
} else if (sw_unlikely(str_value.len() == 0)) {
315+
buf->append("$0\r\n\r\n");
316+
} else {
317+
SW_STRING_FORMAT(buf, "$%zu\r\n", str_value.len());
318+
buf->append(str_value.val(), str_value.len());
319+
buf->append(SW_CRLF, SW_CRLF_LEN);
309320
}
310-
SW_STRING_FORMAT(buf, "$%zu\r\n", str_value.len());
311-
buf->append(str_value.val(), str_value.len());
312-
buf->append(SW_CRLF, SW_CRLF_LEN);
313321
} else if (type == Redis::REPLY_SET) {
314322
if (!value) {
315323
goto _no_value;
316324
}
317325
if (!ZVAL_IS_ARRAY(value)) {
318-
php_swoole_fatal_error(E_WARNING, "the second parameter should be an array");
326+
zend_throw_exception(
327+
swoole_exception_ce, "the second parameter should be an array", SW_ERROR_INVALID_PARAMS);
319328
}
320329
SW_STRING_FORMAT(buf, "*%d\r\n", zend_hash_num_elements(Z_ARRVAL_P(value)));
321330

@@ -329,7 +338,8 @@ static bool redis_response_format(String *buf, zend_long type, zval *value) {
329338
goto _no_value;
330339
}
331340
if (!ZVAL_IS_ARRAY(value)) {
332-
php_swoole_fatal_error(E_WARNING, "the second parameter should be an array");
341+
zend_throw_exception(
342+
swoole_exception_ce, "the second parameter should be an array", SW_ERROR_INVALID_PARAMS);
333343
}
334344
SW_STRING_FORMAT(buf, "*%d\r\n", 2 * zend_hash_num_elements(Z_ARRVAL_P(value)));
335345

@@ -347,7 +357,7 @@ static bool redis_response_format(String *buf, zend_long type, zval *value) {
347357
}
348358
ZEND_HASH_FOREACH_END();
349359
} else {
350-
php_swoole_error(E_WARNING, "Unknown type[%d]", (int) type);
360+
zend_throw_exception_ex(swoole_exception_ce, SW_ERROR_INVALID_PARAMS, "Unknown type[%d]", (int) type);
351361
return false;
352362
}
353363

Diff for: tests/swoole_redis_server/empty.phpt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
swoole_redis_server: empty
3+
--SKIPIF--
4+
<?php require __DIR__ . '/../include/skipif.inc'; ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../include/bootstrap.php';
8+
9+
use Swoole\Redis\Server;
10+
11+
$pm = new ProcessManager;
12+
$pm->parentFunc = function ($pid) use ($pm) {
13+
$redis = new Predis\Client('tcp://127.0.0.1:' . $pm->getFreePort());
14+
$map = $redis->get('map');
15+
Assert::eq($map, [0, '', false, null]);
16+
$pm->kill();
17+
};
18+
19+
$pm->childFunc = function () use ($pm) {
20+
$server = new Server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE);
21+
$server->setHandler('GET', function ($fd, $data) use ($server) {
22+
$server->send($fd, Server::format(Server::SET, [0, '', false, null]));
23+
});
24+
$server->on('WorkerStart', function ($server) use ($pm) {
25+
$pm->wakeup();
26+
});
27+
$server->start();
28+
};
29+
30+
$pm->childFirst();
31+
$pm->run();
32+
?>
33+
--EXPECT--

0 commit comments

Comments
 (0)