]> granicus.if.org Git - neomutt/commitdiff
Do not rely on backends setting error codes on success
authorPietro Cerutti <gahr@gahr.ch>
Fri, 4 Aug 2017 07:36:06 +0000 (07:36 +0000)
committerRichard Russon <rich@flatcap.org>
Fri, 4 Aug 2017 11:00:07 +0000 (12:00 +0100)
From the API documentation of KyotoCabinet, TokyoCabinet and QDBM, I am
not convinced that the error code routines consistently return 0 whenever
the previous operation succeeded. Also, I am not convinced that they
return 1 whenever the previous operation failed.
It is important that we do not return 0 on failure, so let's try to get
the error code and fallback to -1 if we couldn't get anything more
specific.

Issue #696

hcache/kc.c
hcache/qdbm.c
hcache/tc.c

index 1c09b97eded0097d285a4e6b40d625a93bc077f4..a6414ac52d5f359650c7e3e03d712f7e1fb06440 100644 (file)
@@ -92,8 +92,12 @@ static int hcache_kyotocabinet_store(void *ctx, const char *key, size_t keylen,
     return -1;
 
   KCDB *db = ctx;
-  kcdbset(db, key, keylen, data, dlen);
-  return kcdbecode(db);
+  if (!kcdbset(db, key, keylen, data, dlen))
+  {
+    int ecode = kcdbecode(db);
+    return ecode ? ecode : -1;
+  }
+  return 0;
 }
 
 static int hcache_kyotocabinet_delete(void *ctx, const char *key, size_t keylen)
@@ -102,8 +106,12 @@ static int hcache_kyotocabinet_delete(void *ctx, const char *key, size_t keylen)
     return -1;
 
   KCDB *db = ctx;
-  kcdbremove(db, key, keylen);
-  return kcdbecode(db);
+  if (!kcdbremove(db, key, keylen))
+  {
+    int ecode = kcdbecode(db);
+    return ecode ? ecode : -1;
+  }
+  return 0;
 }
 
 static void hcache_kyotocabinet_close(void **ctx)
index 605aa36dab7073f0b510c2144baa04e91d922c9c..a2a5ada6aebbc02ff6563e8febed82627fb01248 100644 (file)
@@ -71,7 +71,7 @@ static int hcache_qdbm_store(void *ctx, const char *key, size_t keylen, void *da
   /* Not sure if dbecode is reset on success, so better to explicitely return 0
    * on success */
   bool success = vlput(db, key, keylen, data, dlen, VL_DOVER);
-  return success ? 0 : dpecode;
+  return success ? 0 : dpecode ? dpecode : -1;
 }
 
 static int hcache_qdbm_delete(void *ctx, const char *key, size_t keylen)
@@ -83,7 +83,7 @@ static int hcache_qdbm_delete(void *ctx, const char *key, size_t keylen)
   /* Not sure if dbecode is reset on success, so better to explicitely return 0
    * on success */
   bool success = vlout(db, key, keylen);
-  return success ? 0 : dpecode;
+  return success ? 0 : dpecode ? dpecode : -1;
 }
 
 static void hcache_qdbm_close(void **ctx)
index 62c3fdbd24d31f6d236e8a91a5c3bca9dd51ece8..e1c39f1401c1b0c062b15492731986b4bb8f0140 100644 (file)
@@ -82,8 +82,12 @@ static int hcache_tokyocabinet_store(void *ctx, const char *key, size_t keylen,
     return -1;
 
   TCBDB *db = ctx;
-  bool success = tcbdbput(db, key, keylen, data, dlen);
-  return success ? 0 : -1; /* TODO - how to get error code? */
+  if (!tcbdbput(db, key, keylen, data, dlen))
+  {
+    int ecode = tchdbecode(db);
+    return ecode ? ecode : -1;
+  }
+  return 0;
 }
 
 static int hcache_tokyocabinet_delete(void *ctx, const char *key, size_t keylen)
@@ -92,8 +96,12 @@ static int hcache_tokyocabinet_delete(void *ctx, const char *key, size_t keylen)
     return -1;
 
   TCBDB *db = ctx;
-  bool success = tcbdbout(db, key, keylen);
-  return success ? 0 : -1; /* TODO - how to get error code? */
+  if (!tcbdbout(db, key, keylen))
+  {
+    int ecode = tchdbecode(db);
+    return ecode ? ecode : -1;
+  }
+  return 0;
 }
 
 static void hcache_tokyocabinet_close(void **ctx)