redis / hiredis

Minimalistic C client for Redis >= 1.2
BSD 3-Clause "New" or "Revised" License
6.27k stars 1.82k forks source link

Problem while inserting string value with space #650

Closed toRatnesh closed 5 years ago

toRatnesh commented 5 years ago

I am able to set and get string value with space using redis-cli as following: 127.0.0.1:8006> set myname "Ratnesh Kumar Tiwari" -> Redirected to slot [12807] located at 127.0.0.1:8007 OK 127.0.0.1:8007> get myname "Ratnesh Kumar Tiwari" 127.0.0.1:8007> 127.0.0.1:8007> 127.0.0.1:8007> del myname (integer) 1 127.0.0.1:8007>

While doing the same using hiredis client I am getting syntax error: $ ./hiredis_c_client 127.0.0.1 8007 IP: 127.0.0.1 Port: 8007 Connecting to redis cluster Enter your command (or quit): set myname "Ratnesh Kumar Tiwari" cmdlen: 33, cmd = set myname "Ratnesh Kumar Tiwari" Executing command : set myname "Ratnesh Kumar Tiwari" Reply Error: ERR syntax error

Enter your command (or quit): quit

This is because the function 'redisvFormatCommand' inside the file 'hiredis.c' breaks "Ratnesh Kumar Tiwari" into 3 tokens "Ratnesh Kumar Tiwari"

How to set a string value with space ?

Part of the code where I am processing the user input command: fgets(cmd, 128, stdin); // reading command from stdin if('\n' == cmd[strlen(cmd) - 1])
cmd[strlen(cmd) - 1] = '\0';

if(0 == strncmp(cmd, "quit", strlen("quit"))){ break; }

fprintf(stdout, "cmdlen: %zu, cmd = %s\n", strlen(cmd), cmd); rreply = redisClusterCommand(rcontext, cmd); // executing the command

michael-grunder commented 5 years ago

Hi @ratnesh-tiwari,

This isn't a problem in hiredis itself, but in how you are parsing input from the user.

Also, I think you're using hiredis-vip and not hiredis since hiredis doesn't have any redisClusterCommand function.

I'm going to close the issue here, but using hiredis-vip you just need to pass the whole key with spaces to each variadict argument, like this:

redisClusterCommand(cc, "SET %s %s", "mykey", "Value with spaces");

How to parse input with spaces is a different question and not really relevant here.

Good luck! Mike