From 01a1fe378554dee10a77456754346347bbb20126 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 17 Oct 2011 14:42:46 +0200
Subject: [PATCH] Fixes debug-tests.c coverity issues: NEGATIVE_RETURNS,
 FORWARD_NULL

https://fedorahosted.org/sssd/ticket/1046
---
 src/tests/debug-tests.c |  184 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 137 insertions(+), 47 deletions(-)

diff --git a/src/tests/debug-tests.c b/src/tests/debug-tests.c
index cded13f..4fe8f30 100644
--- a/src/tests/debug-tests.c
+++ b/src/tests/debug-tests.c
@@ -188,27 +188,30 @@ int test_helper_debug_check_message(int level, int msgmode)
     int filesize;
     int fsize;
     int fd;
-    int set_fd_result;
     int ret;
+    int _errno = 0;
     FILE *file = NULL;
 
     strncpy(filename, "sssd_debug_tests.XXXXXX", 24);
     fd = mkstemp(filename);
     if (fd == -1) {
-        ret = errno;
+        _errno = errno;
         talloc_free(ctx);
-        return errno;
+        errno = _errno;
+        return DEBUG_TEST_NOK;
     }
 
     file = fdopen(fd, "r");
     if (file == NULL) {
-        ret = errno;
+        _errno = errno;
+        ret = DEBUG_TEST_NOK;
         goto done;
     }
 
-    set_fd_result = set_debug_file_from_fd(fd);
-    if (set_fd_result != EOK) {
-        ret = set_fd_result;
+    ret = set_debug_file_from_fd(fd);
+    if (ret != EOK) {
+        _errno = ret;
+        ret = DEBUG_TEST_NOK;
         goto done;
     }
 
@@ -218,19 +221,32 @@ int test_helper_debug_check_message(int level, int msgmode)
         DEBUG_MSG(level, __FUNCTION__, "some error");
     }
 
-    fseek(file, 0, SEEK_END);
+    ret = fseek(file, 0, SEEK_END);
+    if (ret == -1) {
+        _errno = errno;
+        ret = DEBUG_TEST_NOK;
+        goto done;
+    }
+
     filesize = ftell(file);
+    if (filesize == -1) {
+        _errno = errno;
+        ret = DEBUG_TEST_NOK;
+        goto done;
+    }
 
     rewind(file);
 
     msg = talloc_array(ctx, char, filesize+1);
     if (msg == NULL) {
-        ret = ENOMEM;
+        _errno = ENOMEM;
+        ret = DEBUG_TEST_NOK;
         goto done;
     }
     fsize = fread(msg, sizeof(char), filesize, file);
     if (fsize != filesize) {
-        ret = EIO;
+        _errno = EIO;
+        ret = DEBUG_TEST_NOK;
         goto done;
     }
     msg[fsize] = '\0';
@@ -261,7 +277,8 @@ int test_helper_debug_check_message(int level, int msgmode)
                                          time_hour, time_min, time_sec, time_year,
                                          debug_prg_name, function, level, body);
             if (compare_to == NULL) {
-                ret = ENOMEM;
+                _errno = ENOMEM;
+                ret = DEBUG_TEST_NOK;
                 goto done;
             }
         } else {
@@ -280,7 +297,8 @@ int test_helper_debug_check_message(int level, int msgmode)
                                          time_hour, time_min, time_sec, time_usec,
                                          time_year, debug_prg_name, function, level, body);
             if (compare_to == NULL) {
-                ret = ENOMEM;
+                _errno = ENOMEM;
+                ret = DEBUG_TEST_NOK;
                 goto done;
             }
         }
@@ -288,7 +306,8 @@ int test_helper_debug_check_message(int level, int msgmode)
         compare_to = talloc_asprintf(ctx, "[%s] [%s] (%#.4x): %s",
                                      debug_prg_name, function, level, body);
         if (compare_to == NULL) {
-            ret = ENOMEM;
+            _errno = ENOMEM;
+            ret = DEBUG_TEST_NOK;
             goto done;
         }
     }
@@ -296,8 +315,11 @@ int test_helper_debug_check_message(int level, int msgmode)
 
 done:
     talloc_free(ctx);
-    fclose(file);
+    if (file != NULL) {
+        fclose(file);
+    }
     remove(filename);
+    errno = _errno;
     return ret;
 }
 
@@ -306,24 +328,27 @@ int test_helper_debug_is_empty_message(int level, int msgmode)
     char filename[24] = {'\0'};
     int fd;
     int filesize;
-    int set_fd_result;
     int ret;
+    int _errno = 0;
     FILE *file;
 
     strncpy(filename, "sssd_debug_tests.XXXXXX", 24);
     fd = mkstemp(filename);
-    if (fd == -1)
-        return errno;
+    if (fd == -1) {
+        return DEBUG_TEST_NOK;
+    }
 
     file = fdopen(fd, "r");
     if (file == NULL) {
-        ret = errno;
+        _errno = errno;
+        ret = DEBUG_TEST_NOK;
         goto done;
     }
 
-    set_fd_result = set_debug_file_from_fd(fd);
-    if (set_fd_result != EOK) {
-        ret = set_fd_result;
+    ret = set_debug_file_from_fd(fd);
+    if (ret != EOK) {
+        _errno = ret;
+        ret = DEBUG_TEST_NOK;
         goto done;
     }
 
@@ -332,14 +357,29 @@ int test_helper_debug_is_empty_message(int level, int msgmode)
     } else {
         DEBUG_MSG(level, __FUNCTION__, "some error");
     }
-    fseek(file, 0, SEEK_END);
+
+    ret = fseek(file, 0, SEEK_END);
+    if (ret == -1) {
+        _errno = errno;
+        ret = DEBUG_TEST_NOK;
+        goto done;
+    }
+
     filesize = ftell(file);
+    if (filesize == -1) {
+        _errno = errno;
+        ret = DEBUG_TEST_NOK;
+        goto done;
+    }
 
     ret = filesize == 0 ? EOK : DEBUG_TEST_NOK;
 
 done:
-    fclose(file);
+    if (file != NULL) {
+        fclose(file);
+    }
     remove(filename);
+    errno = _errno;
     return ret;
 }
 
@@ -368,10 +408,14 @@ START_TEST(test_debug_is_set_single_no_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = levels[i];
+
+        errno = 0;
         result = test_helper_debug_check_message(levels[i], 0);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL, "Test of level %#.4x failed - message don't match", levels[i]);
@@ -406,10 +450,14 @@ START_TEST(test_debug_is_set_single_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = levels[i];
+
+        errno = 0;
         result = test_helper_debug_check_message(levels[i], 0);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
 
@@ -449,10 +497,14 @@ START_TEST(test_debug_is_set_single_timestamp_microseconds)
 
     for (i = 0; i <= 9; i++) {
         debug_level = levels[i];
+
+        errno = 0;
         result = test_helper_debug_check_message(levels[i], 0);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
 
@@ -493,16 +545,21 @@ START_TEST(test_debug_is_notset_no_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = all_set & ~levels[i];
+
+        errno = 0;
         result = test_helper_debug_is_empty_message(levels[i], 0);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL,
                               "Test of level %#.4x failed - message has been written",
                               levels[i]);
         fail_unless(result == EOK, msg);
+        talloc_free(msg);
     }
 }
 END_TEST
@@ -533,10 +590,14 @@ START_TEST(test_debug_is_notset_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = all_set & ~levels[i];
+
+        errno = 0;
         result = test_helper_debug_is_empty_message(levels[i], 0);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL,
@@ -574,10 +635,14 @@ START_TEST(test_debug_is_notset_timestamp_microseconds)
 
     for (i = 0; i <= 9; i++) {
         debug_level = all_set & ~levels[i];
+
+        errno = 0;
         result = test_helper_debug_is_empty_message(levels[i], 0);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL,
@@ -614,14 +679,19 @@ START_TEST(test_debug_msg_is_set_single_no_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = levels[i];
+
+        errno = 0;
         result = test_helper_debug_check_message(levels[i], 1);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL, "Test of level %#.4x failed - message don't match", levels[i]);
         fail_unless(result == EOK, msg);
+        talloc_free(msg);
     }
 }
 END_TEST
@@ -651,10 +721,14 @@ START_TEST(test_debug_msg_is_set_single_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = levels[i];
+
+        errno = 0;
         result = test_helper_debug_check_message(levels[i], 1);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
 
@@ -694,10 +768,14 @@ START_TEST(test_debug_msg_is_set_single_timestamp_microseconds)
 
     for (i = 0; i <= 9; i++) {
         debug_level = levels[i];
+
+        errno = 0;
         result = test_helper_debug_check_message(levels[i], 1);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
 
@@ -738,10 +816,14 @@ START_TEST(test_debug_msg_is_notset_no_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = all_set & ~levels[i];
+
+        errno = 0;
         result = test_helper_debug_is_empty_message(levels[i], 1);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL, "Test of level %#.4x failed - message has been written", levels[i]);
@@ -777,10 +859,14 @@ START_TEST(test_debug_msg_is_notset_timestamp)
 
     for (i = 0; i <= 9; i++) {
         debug_level = all_set & ~levels[i];
+
+        errno = 0;
         result = test_helper_debug_is_empty_message(levels[i], 1);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL, "Test of level %#.4x failed - message has been written", levels[i]);
@@ -816,10 +902,14 @@ START_TEST(test_debug_msg_is_notset_timestamp_microseconds)
 
     for (i = 0; i <= 9; i++) {
         debug_level = all_set & ~levels[i];
+
+        errno = 0;
         result = test_helper_debug_is_empty_message(levels[i], 1);
 
-        error_msg = strerror(result);
-        fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
+        if (result == DEBUG_TEST_NOK && errno != 0) {
+            error_msg = strerror(errno);
+            fail(error_msg);
+        }
 
         char *msg = NULL;
         msg = talloc_asprintf(NULL, "Test of level %#.4x failed - message has been written", levels[i]);
-- 
1.7.6.4
