[PATCH] Fix handling of out-of-memory in get command.

Tomash Brechko tomash.brechko at gmail.com
Mon Nov 12 12:45:30 UTC 2007


Always send "SERVER_ERROR out of memory" when memory exhausted.
Before the fix the code tried to append "END\r\n" to whatever there
was in the buffer, which may produce bogus result because the buffer
might not end in "\r\n".  Also the result of add_iov() wasn't tested.
---
 trunk/server/memcached.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/trunk/server/memcached.c b/trunk/server/memcached.c
index 9e4271f..974eb81 100644
--- a/trunk/server/memcached.c
+++ b/trunk/server/memcached.c
@@ -1224,9 +1224,13 @@ static inline void process_get_command(conn *c, token_t *tokens, size_t ntokens,
 
     if (settings.verbose > 1)
         fprintf(stderr, ">%d END\n", c->sfd);
-    add_iov(c, "END\r\n", 5);
-
-    if (c->udp && build_udp_headers(c) != 0) {
+    /*
+      If the loop was terminated because of out-of-memory, it is not
+      reliable to add END\r\n to the buffer, because it might not end
+      in \r\n.  So we send SERVER_ERROR instead.
+    */
+    if (key_token->value != NULL || add_iov(c, "END\r\n", 5) != 0
+        || (c->udp && build_udp_headers(c) != 0)) {
         out_string(c, "SERVER_ERROR out of memory");
     }
     else {
-- 
1.5.3.5.529.ge3d6d


More information about the memcached mailing list