Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix Null Bulk String response parsing in cluster library #1104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 9, 2017

Conversation

albertofem
Copy link
Contributor

This PR fixes an issue when running LUA commands that don't return any values in a cluster context. In these cases, Redis will return a Null Bulk String as specified in the protocol:

RESP Bulk Strings can also be used in order to signal non-existence of a value using a special format that is used to represent a Null value. In this special format the length is -1, and there is no data, so a Null is represented as:

"$-1\r\n"

This is called a Null Bulk String.

The client library API should not return an empty string, but a nil object, when the server replies with a Null Bulk String. For example a Ruby library should return 'nil' while a C library should return NULL (or set a special flag in the reply object), and so forth.

Right now, this case is not handled and it causes and immediate Segmentation fault, because the RETVAL_STRINGL is trying to make sense of a bulk string response with no length at all. The issue is easily reproducible: just pick the failing test commit and run the test against your cluster. You should see an immediate Segmentation fault.

The issue is solved by returning a NULL value when the length is -1, as specified in the Redis protocol documentation.

Maintainers of this repository will probably know a better and more proper way of solving this issue, but here is my two-cents.

Props to @ajdiaz and @josledp for helping to find this issue!

@yatsukhnenko
Copy link
Member

yatsukhnenko commented Feb 8, 2017

Thanks, @albertofem! Nice catch!
Could you also add similar check for the else branch in this function?

@@ -1870,7 +1870,11 @@ PHP_REDIS_API void cluster_variant_resp(INTERNAL_FUNCTION_PARAMETERS, redisClust
RETVAL_TRUE;
break;
case TYPE_BULK:
RETVAL_STRINGL(r->str, r->len);
if (r->len > -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner if check looks like if (r->len < 0) { instead of if (r->len > -1) {

@albertofem
Copy link
Contributor Author

Oh, I see. Thank you for making the changes yourself 👍 Is it ready to be merged now?

@yatsukhnenko
Copy link
Member

@albertofem, it would be great if you will drop my commit and make similar changes by yourself

@yatsukhnenko yatsukhnenko merged commit 058753e into phpredis:develop Feb 9, 2017
michael-grunder pushed a commit that referenced this pull request Mar 16, 2017
* Failing test case when running LUA with bulk empty response
* Fix issue when parsing bulk array response from eval commands
* Added test for bulk LUA responses and changed condition
* Added multi tests and fixes in C code format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants