summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormichael-grunder <michael.grunder@gmail.com>2020-10-19 10:11:51 -0700
committermichael-grunder <michael.grunder@gmail.com>2021-02-25 21:25:17 -0800
commit410c24d2a977288ec97ffac23a303597b9f98d67 (patch)
tree6093c8cca18febdf37d6b1b4ed41716d02cb8ae4
parentbd7488d27d235a35a16519446173bef99fd4f200 (diff)
Fix off-by-one error in seekNewline
-rw-r--r--read.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/read.c b/read.c
index 24c2b58..de62b9a 100644
--- a/read.c
+++ b/read.c
@@ -123,21 +123,27 @@ static char *readBytes(redisReader *r, unsigned int bytes) {
/* Find pointer to \r\n. */
static char *seekNewline(char *s, size_t len) {
- char *_s = s, *ret;
- int _len = len-1;
+ char *ret;
- /* Exclude the last character from the searched length because the found
- * '\r' should be followed by a '\n' */
- while ((ret = memchr(_s, '\r', _len)) != NULL) {
+ /* We cannot match with fewer than 2 bytes */
+ if (len < 2)
+ return NULL;
+
+ /* Search up to len - 1 characters */
+ len--;
+
+ /* Look for the \r */
+ while ((ret = memchr(s, '\r', len)) != NULL) {
if (ret[1] == '\n') {
/* Found. */
break;
}
/* Continue searching. */
ret++;
- _len -= ret - _s;
- _s = ret;
+ len -= ret - s;
+ s = ret;
}
+
return ret;
}