From 55ed45d15038554215d9abd059f91265833f9675 Mon Sep 17 00:00:00 2001 From: Dylan Wood Date: Fri, 24 Jan 2014 16:02:55 -0800 Subject: [PATCH 1/2] handle_get() to truncate data on last "\nEND\n" Searching the buffer string for "END" causes problems if the data inside contains the substring "END". For example, if I retrieve the string "Please do not EXTEND beyond the edge" from my memcached server, the "END" in EXTEND will signal the end of the data, and only "Please do Not E" will be returned. This is fixed by looking for ``crlf + "END" + crlf``. On the off chance that the data contains that string, it is advisable to search for the last occurrence of it, instead of the first. --- lib/memcache.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index 8b826ad..a1d4536 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -275,8 +275,8 @@ Client.prototype.handle_get = function(buffer) { return [result_value, end_indicator_len + crlf_len]; } else if (buffer.indexOf('VALUE') == 0 && buffer.indexOf('END') != -1) { first_line_len = buffer.indexOf(crlf) + crlf_len; - var end_indicator_start = buffer.indexOf('END'); - result_len = end_indicator_start - first_line_len - crlf_len; + var end_indicator_start = buffer.lastIndexOf(crlf + 'END' + crlf); + result_len = end_indicator_start - first_line_len; result_value = buffer.substr(first_line_len, result_len); return [result_value, first_line_len + parseInt(result_len, 10) + crlf_len + end_indicator_len + crlf_len] } else { From 28992695e7ac2d04d8cdd8a735dc1cc2682b1408 Mon Sep 17 00:00:00 2001 From: Dylan Wood Date: Mon, 27 Jan 2014 13:22:09 -0800 Subject: [PATCH 2/2] further modify END detection to use crlf Based on other pull requests to node-memcache that solve the same problem --- lib/memcache.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/memcache.js b/lib/memcache.js index a1d4536..5840ed8 100644 --- a/lib/memcache.js +++ b/lib/memcache.js @@ -271,9 +271,10 @@ Client.prototype.handle_get = function(buffer) { var end_indicator_len = 3; var result_len = 0; - if (buffer.indexOf('END') == 0) { + if (buffer.indexOf('END' + crlf) == 0) { return [result_value, end_indicator_len + crlf_len]; - } else if (buffer.indexOf('VALUE') == 0 && buffer.indexOf('END') != -1) { + } else if (buffer.indexOf('VALUE') == 0 + && buffer.indexOf(crlf + 'END' + crlf) != -1) { first_line_len = buffer.indexOf(crlf) + crlf_len; var end_indicator_start = buffer.lastIndexOf(crlf + 'END' + crlf); result_len = end_indicator_start - first_line_len;