From 6833b0eb88c8779fd76c5628a03a987ec03dac83 Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Mon, 24 Jan 2011 11:33:50 +0100 Subject: [PATCH 3/5] Ensure error_string() never returns NULL A Coverity check indicated that ther are platforms where strerror() will return NULL for unknown, e.g. negative error numbers. Chances are that these platforms will have problems with NULL arguments to printf() too. --- dhash/examples/dhash_test.c | 13 ++++++++++++- 1 files changed, 12 insertions(+), 1 deletions(-) diff --git a/dhash/examples/dhash_test.c b/dhash/examples/dhash_test.c index 6c02de1..a2dfa77 100644 --- a/dhash/examples/dhash_test.c +++ b/dhash/examples/dhash_test.c @@ -35,10 +35,21 @@ int verbose = 0; const char *error_string(int error) { + const char *str; + if (IS_HASH_ERROR(error)) return hash_error_string(error); - return strerror(error); + if (error < 0) { + return "Negative error codes are not supported."; + } + + str = strerror(error); + if (str == NULL) { + return "strerror() returned NULL."; + } + + return str; } char *key_string(hash_key_t *key) -- 1.7.3.3