From f2f10796b07bf75f663d4a1f3cda22228be29403 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Thu, 25 Aug 2011 13:10:27 -0400
Subject: [PATCH] Fix bad read/writes in debug tests

Also fixes several buffer overrun issues.

It was causing failures of the tests when compiled with
FORTIFIED_SOURCE
---
 src/tests/debug-tests.c |  117 +++++++++++++++++++++++++++++-----------------
 1 files changed, 74 insertions(+), 43 deletions(-)

diff --git a/src/tests/debug-tests.c b/src/tests/debug-tests.c
index b8e159d3105917b92b37b4708a69aef24940ceb6..2708cec1d8b8c1507d86c2e0cdc40d75813120cf 100644
--- a/src/tests/debug-tests.c
+++ b/src/tests/debug-tests.c
@@ -52,9 +52,10 @@ START_TEST(test_debug_convert_old_level_old_format)
     for (old_level = 0; old_level <= 9; old_level++) {
         expected_level |= levels[old_level];
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Invalid conversion of %d", old_level);
+        char *msg = NULL;
+        msg = talloc_asprintf(NULL, "Invalid conversion of %d", old_level);
         fail_unless(debug_convert_old_level(old_level) == expected_level, msg);
+        talloc_free(msg);
     }
 }
 END_TEST
@@ -185,6 +186,7 @@ int test_helper_debug_check_message(int level, int msgmode)
     const char *function = __FUNCTION__;
     const char *body = "some error\n";
     int filesize;
+    int fsize;
     int fd;
     int set_fd_result;
     int ret;
@@ -220,37 +222,52 @@ int test_helper_debug_check_message(int level, int msgmode)
     filesize = ftell(file);
 
     rewind(file);
-    msg = talloc_array(ctx, char, filesize);
+
+    msg = talloc_array(ctx, char, filesize+1);
     if (msg == NULL) {
         ret = ENOMEM;
         goto done;
     }
-    fread(msg, sizeof(char), filesize, file);
-
-    compare_to = talloc_array(ctx, char, filesize);
-    if (compare_to == NULL) {
-        ret = ENOMEM;
+    fsize = fread(msg, sizeof(char), filesize, file);
+    if (fsize != filesize) {
+        ret = EIO;
         goto done;
     }
+    msg[fsize] = '\0';
 
     if (debug_timestamps == 1) {
-        char time_day[4] = {'\0'};
-        char time_month[4] = {'\0'};
-        int time_day_num;
-        int time_hour;
-        int time_min;
-        int time_sec;
-        int time_year;
-        if (sscanf(msg, "(%s %s %d %d:%d:%d %d)", time_day, time_month,
-            &time_day_num, &time_hour, &time_min, &time_sec, &time_year) != 7) {
+        char time_day[4] = {'\0', '\0', '\0', '\0'};
+        char time_month[4] = {'\0', '\0', '\0', '\0'};
+        int time_day_num = 0;
+        int time_hour = 0;
+        int time_min = 0;
+        int time_sec = 0;
+        int time_year = 0;
+
+        int scan_return = sscanf(msg, "(%s %s %d %d:%d:%d %d)", time_day, time_month,
+                                 &time_day_num, &time_hour, &time_min, &time_sec, &time_year);
+
+        if (scan_return != 7) {
             ret = DEBUG_TEST_NOK_TS;
             goto done;
         }
-        sprintf(compare_to, "(%s %s %2d %.2d:%.2d:%.2d %.4d) [%s] [%s] (%#.4x): %s",
-                time_day, time_month, time_day_num, time_hour, time_min, time_sec, time_year,
-                debug_prg_name, function, level, body);
+        compare_to = talloc_asprintf(ctx,
+                                     "(%s %s %2d %.2d:%.2d:%.2d %.4d) "
+                                     "[%s] [%s] (%#.4x): %s",
+                                     time_day, time_month, time_day_num,
+                                     time_hour, time_min, time_sec, time_year,
+                                     debug_prg_name, function, level, body);
+        if (compare_to == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
     } else {
-        sprintf(compare_to, "[%s] [%s] (%#.4x): %s", debug_prg_name, function, level, body);
+        compare_to = talloc_asprintf(ctx, "[%s] [%s] (%#.4x): %s",
+                                     debug_prg_name, function, level, body);
+        if (compare_to == NULL) {
+            ret = ENOMEM;
+            goto done;
+        }
     }
     ret = strncmp(msg, compare_to, filesize) == 0 ? EOK : DEBUG_TEST_NOK;
 
@@ -332,9 +349,10 @@ START_TEST(test_debug_is_set_single_no_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - message don't match", levels[i]);
+        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
@@ -368,13 +386,15 @@ START_TEST(test_debug_is_set_single_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
+        char *msg = NULL;
 
-        sprintf(msg, "Test of level %#.4x failed - invalid timestamp", levels[i]);
+        msg = talloc_asprintf(NULL, "Test of level %#.4x failed - invalid timestamp", levels[i]);
         fail_if(result == DEBUG_TEST_NOK_TS, msg);
+        talloc_free(msg);
 
-        sprintf(msg, "Test of level %#.4x failed - message don't match", levels[i]);
+        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
@@ -409,8 +429,10 @@ START_TEST(test_debug_is_notset_no_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - message has been written", levels[i]);
+        char *msg = NULL;
+        msg = talloc_asprintf(NULL,
+                              "Test of level %#.4x failed - message has been written",
+                              levels[i]);
         fail_unless(result == EOK, msg);
     }
 }
@@ -446,9 +468,12 @@ START_TEST(test_debug_is_notset_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - message has been written", levels[i]);
+        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
@@ -482,8 +507,8 @@ START_TEST(test_debug_msg_is_set_single_no_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - message don't match", levels[i]);
+        char *msg = NULL;
+        msg = talloc_asprintf(NULL, "Test of level %#.4x failed - message don't match", levels[i]);
         fail_unless(result == EOK, msg);
     }
 }
@@ -518,13 +543,15 @@ START_TEST(test_debug_msg_is_set_single_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
+        char *msg = NULL;
 
-        sprintf(msg, "Test of level %#.4x failed - invalid timestamp", levels[i]);
+        msg = talloc_asprintf(NULL, "Test of level %#.4x failed - invalid timestamp", levels[i]);
         fail_if(result == DEBUG_TEST_NOK_TS, msg);
+        talloc_free(msg);
 
-        sprintf(msg, "Test of level %#.4x failed - message don't match", levels[i]);
+        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
@@ -559,9 +586,10 @@ START_TEST(test_debug_msg_is_notset_no_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - message has been written", levels[i]);
+        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
@@ -596,9 +624,10 @@ START_TEST(test_debug_msg_is_notset_timestamp)
         error_msg = strerror(result);
         fail_unless(result == EOK || result == DEBUG_TEST_NOK || result == DEBUG_TEST_NOK_TS, error_msg);
 
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - message has been written", levels[i]);
+        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
@@ -624,9 +653,10 @@ START_TEST(test_debug_is_set_true)
 
     for (i = 0; i <= 9; i++) {
         result = DEBUG_IS_SET(levels[i]);
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - result is 0x%.4x", levels[i], result);
+        char *msg = NULL;
+        msg = talloc_asprintf(NULL, "Test of level %#.4x failed - result is 0x%.4x", levels[i], result);
         fail_unless(result > 0, msg);
+        talloc_free(msg);
     }
 }
 END_TEST
@@ -653,9 +683,10 @@ START_TEST(test_debug_is_set_false)
         debug_level = all_set & ~levels[i];
 
         result = DEBUG_IS_SET(levels[i]);
-        char msg[50] = {'\0'};
-        sprintf(msg, "Test of level %#.4x failed - result is 0x%.4x", levels[i], result);
+        char *msg = NULL;
+        msg = talloc_asprintf(NULL, "Test of level %#.4x failed - result is 0x%.4x", levels[i], result);
         fail_unless(result == 0, msg);
+        talloc_free(msg);
     }
 }
 END_TEST
-- 
1.7.6

