libmemcached: fix #125: allocation failure on negative expiration
[awesomized/libmemcached] / src / libmemcached / storage.cc
index 0700c0558ecc025451dbbfa096c8ebd79dcae3cd..31eadb1e2e9fa9aa8416a18ee673014f6c421854 100644 (file)
@@ -1,6 +1,6 @@
 /*
     +--------------------------------------------------------------------+
-    | libmemcached - C/C++ Client Library for memcached                  |
+    | libmemcached-awesome - C/C++ Client Library for memcached          |
     +--------------------------------------------------------------------+
     | Redistribution and use in source and binary forms, with or without |
     | modification, are permitted under the terms of the BSD license.    |
@@ -9,7 +9,7 @@
     | the terms online at: https://opensource.org/licenses/BSD-3-Clause  |
     +--------------------------------------------------------------------+
     | Copyright (c) 2006-2014 Brian Aker   https://datadifferential.com/ |
-    | Copyright (c) 2020 Michael Wallner   <mike@php.net>                |
+    | Copyright (c) 2020-2021 Michael Wallner        https://awesome.co/ |
     +--------------------------------------------------------------------+
 */
 
@@ -20,31 +20,39 @@ enum memcached_storage_action_t { SET_OP, REPLACE_OP, ADD_OP, PREPEND_OP, APPEND
 /* Inline this */
 static inline const char *storage_op_string(memcached_storage_action_t verb) {
   switch (verb) {
-  case REPLACE_OP: return "replace ";
+  case REPLACE_OP:
+    return "replace ";
 
-  case ADD_OP: return "add ";
+  case ADD_OP:
+    return "add ";
 
-  case PREPEND_OP: return "prepend ";
+  case PREPEND_OP:
+    return "prepend ";
 
-  case APPEND_OP: return "append ";
+  case APPEND_OP:
+    return "append ";
 
-  case CAS_OP: return "cas ";
+  case CAS_OP:
+    return "cas ";
 
-  case SET_OP: break;
+  case SET_OP:
+    break;
   }
 
   return "set ";
 }
 
-static inline bool can_by_encrypted(const memcached_storage_action_t verb) {
+static inline bool can_be_encrypted(const memcached_storage_action_t verb) {
   switch (verb) {
   case SET_OP:
   case ADD_OP:
   case CAS_OP:
-  case REPLACE_OP: return true;
+  case REPLACE_OP:
+    return true;
 
   case APPEND_OP:
-  case PREPEND_OP: break;
+  case PREPEND_OP:
+    break;
   }
 
   return false;
@@ -53,30 +61,40 @@ static inline bool can_by_encrypted(const memcached_storage_action_t verb) {
 static inline uint8_t get_com_code(const memcached_storage_action_t verb, const bool reply) {
   if (reply == false) {
     switch (verb) {
-    case SET_OP: return PROTOCOL_BINARY_CMD_SETQ;
+    case SET_OP:
+      return PROTOCOL_BINARY_CMD_SETQ;
 
-    case ADD_OP: return PROTOCOL_BINARY_CMD_ADDQ;
+    case ADD_OP:
+      return PROTOCOL_BINARY_CMD_ADDQ;
 
     case CAS_OP: /* FALLTHROUGH */
-    case REPLACE_OP: return PROTOCOL_BINARY_CMD_REPLACEQ;
+    case REPLACE_OP:
+      return PROTOCOL_BINARY_CMD_REPLACEQ;
 
-    case APPEND_OP: return PROTOCOL_BINARY_CMD_APPENDQ;
+    case APPEND_OP:
+      return PROTOCOL_BINARY_CMD_APPENDQ;
 
-    case PREPEND_OP: return PROTOCOL_BINARY_CMD_PREPENDQ;
+    case PREPEND_OP:
+      return PROTOCOL_BINARY_CMD_PREPENDQ;
     }
   }
 
   switch (verb) {
-  case SET_OP: break;
+  case SET_OP:
+    break;
 
-  case ADD_OP: return PROTOCOL_BINARY_CMD_ADD;
+  case ADD_OP:
+    return PROTOCOL_BINARY_CMD_ADD;
 
   case CAS_OP: /* FALLTHROUGH */
-  case REPLACE_OP: return PROTOCOL_BINARY_CMD_REPLACE;
+  case REPLACE_OP:
+    return PROTOCOL_BINARY_CMD_REPLACE;
 
-  case APPEND_OP: return PROTOCOL_BINARY_CMD_APPEND;
+  case APPEND_OP:
+    return PROTOCOL_BINARY_CMD_APPEND;
 
-  case PREPEND_OP: return PROTOCOL_BINARY_CMD_PREPEND;
+  case PREPEND_OP:
+    return PROTOCOL_BINARY_CMD_PREPEND;
   }
 
   return PROTOCOL_BINARY_CMD_SET;
@@ -171,9 +189,9 @@ memcached_send_ascii(Memcached *ptr, memcached_instance_st *instance, const char
         memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
   }
 
-  char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1];
-  int expiration_buffer_length = snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu",
-                                          (unsigned long long) expiration);
+  char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1 + 1];
+  int expiration_buffer_length = snprintf(expiration_buffer, sizeof(expiration_buffer), " %lld",
+                                          (long long) expiration);
   if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer) or expiration_buffer_length < 0)
   {
     return memcached_set_error(
@@ -278,7 +296,7 @@ memcached_send(memcached_st *shell, const char *group_key, size_t group_key_leng
   hashkit_string_st *destination = NULL;
 
   if (memcached_is_encrypted(ptr)) {
-    if (can_by_encrypted(verb) == false) {
+    if (can_be_encrypted(verb) == false) {
       return memcached_set_error(
           *ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
           memcached_literal_param("Operation not allowed while encyrption is enabled"));