From 703cd738dcd3b8f6def30f48637a7c8e352692e0 Mon Sep 17 00:00:00 2001 From: antirez Date: Wed, 3 Jul 2013 11:59:44 +0200 Subject: [PATCH] redis-cli --pipe: send final ECHO in a safer way. If the protocol read from stdin happened to contain grabage (invalid random chars), in the previous implementation it was possible to end with something like: dksfjdksjflskfjl*2\r\n$4\r\nECHO.... That is invalid as the *2 should start into a new line. Now we prefix the ECHO with a CRLF that has no effects on the server but prevents this issues most of the times. Of course if the offending wrong sequence is something like: $3248772349\r\n No one is going to save us as Redis will wait for data in the context of a big argument, so this fix does not cover all the cases. This partially fixes issue #681. --- src/redis-cli.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/redis-cli.c b/src/redis-cli.c index 5914fd2c4..8cf1c4646 100644 --- a/src/redis-cli.c +++ b/src/redis-cli.c @@ -1204,8 +1204,12 @@ static void pipeMode(void) { ssize_t nread = read(STDIN_FILENO,obuf,sizeof(obuf)); if (nread == 0) { + /* The ECHO sequence starts with a "\r\n" so that if there + * is garbage in the protocol we read from stdin, the ECHO + * will likely still be properly formatted. + * CRLF is ignored by Redis, so it has no effects. */ char echo[] = - "*2\r\n$4\r\nECHO\r\n$20\r\n01234567890123456789\r\n"; + "\r\n*2\r\n$4\r\nECHO\r\n$20\r\n01234567890123456789\r\n"; int j; eof = 1; @@ -1214,7 +1218,7 @@ static void pipeMode(void) { * to make sure everything was read from the server. */ for (j = 0; j < 20; j++) magic[j] = rand() & 0xff; - memcpy(echo+19,magic,20); + memcpy(echo+21,magic,20); memcpy(obuf,echo,sizeof(echo)-1); obuf_len = sizeof(echo)-1; obuf_pos = 0;