From d86380170ef0e82a6595e2da0f64fa67ff4a4efc Mon Sep 17 00:00:00 2001 From: Mark Getz Date: Wed, 16 Sep 2015 17:19:04 -0500 Subject: [PATCH 1/2] Fix for value length just under the max value size --- test/memcached-get-set.test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/memcached-get-set.test.js b/test/memcached-get-set.test.js index 2614ebf..6935e69 100644 --- a/test/memcached-get-set.test.js +++ b/test/memcached-get-set.test.js @@ -625,4 +625,28 @@ describe("Memcached GET SET", function() { done(); }); }); + + /** + * Set value size just under maximum amount of data (1MB), + * should trigger error, not crash. The actual value size to trigger the + * error should be value length + key length + length of metadata stored with the + * key value pair. + */ + it("set value length just under maximum data and check for correct error handling", function(done) { + var memcached = new Memcached(common.servers.single) + , message = new Array(32767).join('ThisIsMyTestMessageForThisData32') + , testnr = ++global.testnumbers + , callbacks = 0; + //Length of message is 1048512 + memcached.set("test:" + testnr, message, 1000, function(error, ok){ + ++callbacks; + + assert.equal(error, 'Error: The length of the value is greater than 1048576'); + ok.should.be.false; + + memcached.end(); // close connections + assert.equal(callbacks, 1); + done(); + }); + }); }); From ab861fa7ce57d878b34027b39e3b58c4563ca0a1 Mon Sep 17 00:00:00 2001 From: Mark Getz Date: Wed, 16 Sep 2015 17:32:13 -0500 Subject: [PATCH 2/2] The missing file to fix when value length is just under the max value size --- lib/memcached.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/memcached.js b/lib/memcached.js index 2f40a10..d831324 100644 --- a/lib/memcached.js +++ b/lib/memcached.js @@ -913,7 +913,7 @@ Client.config = { value = Utils.escapeValue(value); length = Buffer.byteLength(value); - if (length > this.maxValue) { + if (length + fullkey.length + 71 > this.maxValue) { return privates.errorResponse(new Error('The length of the value is greater than ' + this.maxValue), callback); }