git » nmdb » commit 075f691

nmdb: Improve check for broken values in cache_incr()

author Alberto Bertogli
2010-10-06 16:37:41 UTC
committer Alberto Bertogli
2010-10-06 16:37:41 UTC
parent b60037201816ecebef83f4dd5023efe82a1dbc62

nmdb: Improve check for broken values in cache_incr()

The code did not check that we do not give strtoll() a NULL value. We know it
should never happen, but it's better to check.

This patch modifies the previous test for 0-terminated strings (which actually
did check for val != NULL before accessing it), making it also return in the
case where val == NULL.

Caught by the clang-analyzer tool.

Signed-off-by: Alberto Bertogli <albertito@blitiri.com.ar>

nmdb/cache.c +4 -3

diff --git a/nmdb/cache.c b/nmdb/cache.c
index 5d1e2c8..c94bd64 100644
--- a/nmdb/cache.c
+++ b/nmdb/cache.c
@@ -438,9 +438,10 @@ int cache_incr(struct cache *cd, const unsigned char *key, size_t ksize,
 	val = e->val;
 	vsize = e->vsize;
 
-	/* the value must be a NULL terminated string, otherwise strtoll might
-	 * cause a segmentation fault */
-	if (val && val[vsize - 1] != '\0')
+	/* The value must be a 0-terminated string, otherwise strtoll might
+	 * cause a segmentation fault. Note that val should never be NULL, but
+	 * it doesn't hurt to check just in case */
+	if (val == NULL || val[vsize - 1] != '\0')
 		return -2;
 
 	intval = strtoll((char *) val, NULL, 10);