From: Pietro Cerutti Date: Fri, 4 Aug 2017 07:36:06 +0000 (+0000) Subject: Do not rely on backends setting error codes on success X-Git-Tag: neomutt-20170907~50 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1806e521dddb6b60fd6c939e86818b72c04c95ec;p=neomutt Do not rely on backends setting error codes on success 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 --- diff --git a/hcache/kc.c b/hcache/kc.c index 1c09b97ed..a6414ac52 100644 --- a/hcache/kc.c +++ b/hcache/kc.c @@ -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) diff --git a/hcache/qdbm.c b/hcache/qdbm.c index 605aa36da..a2a5ada6a 100644 --- a/hcache/qdbm.c +++ b/hcache/qdbm.c @@ -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) diff --git a/hcache/tc.c b/hcache/tc.c index 62c3fdbd2..e1c39f140 100644 --- a/hcache/tc.c +++ b/hcache/tc.c @@ -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)