From d44ab5893845801ec95e058b9b56a3a75b9720f7 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周日, 04 10月 2015 19:35:26 +0800
Subject: [PATCH] Fix access outside buffer

---
 libscpi/test/test_scpi_utils.c |   30 +++++++++++++++++++++++++++++-
 1 files changed, 29 insertions(+), 1 deletions(-)

diff --git a/libscpi/test/test_scpi_utils.c b/libscpi/test/test_scpi_utils.c
index af5aadf..d4472f3 100644
--- a/libscpi/test/test_scpi_utils.c
+++ b/libscpi/test/test_scpi_utils.c
@@ -142,6 +142,31 @@
     TEST_STR_TO_LONG("18", 1, 1, 8); // octal 1, 8 is ignored
 }
 
+static void test_strToULong() {
+    size_t result;
+    uint32_t val;
+
+#define TEST_STR_TO_ULONG(s, r, v, b)                    \
+    do {                                                \
+        result = strToULong(s, &val, b);                 \
+        CU_ASSERT_EQUAL(val, v);                        \
+        CU_ASSERT_EQUAL(result, r);                     \
+    } while(0)                                          \
+
+    TEST_STR_TO_ULONG("", 0, 0, 10);
+    TEST_STR_TO_ULONG("1", 1, 1, 10);
+    TEST_STR_TO_ULONG("10", 2, 10, 10);
+    TEST_STR_TO_ULONG("100MHz", 3, 100, 10);
+    TEST_STR_TO_ULONG("MHz", 0, 0, 10);
+    TEST_STR_TO_ULONG("1.4", 1, 1, 10);
+    TEST_STR_TO_ULONG(" 1", 2, 1, 10);
+    TEST_STR_TO_ULONG(" +100", 5, 100, 10); // space and +
+    TEST_STR_TO_ULONG("FF", 2, 255, 16); // hexadecimal FF
+    TEST_STR_TO_ULONG("77", 2, 63, 8); // octal 77
+    TEST_STR_TO_ULONG("18", 1, 1, 8); // octal 1, 8 is ignored
+    TEST_STR_TO_ULONG("FFFFFFFF", 8, 0xffffffffu, 16); // octal 1, 8 is ignored
+}
+
 static void test_strToDouble() {
     double val;
     size_t result;
@@ -439,6 +464,7 @@
 }
 
 int main() {
+    unsigned int result;
     CU_pSuite pSuite = NULL;
 
     /* Initialize the CUnit test registry */
@@ -458,6 +484,7 @@
             || (NULL == CU_add_test(pSuite, "longToStr", test_longToStr))
             || (NULL == CU_add_test(pSuite, "doubleToStr", test_doubleToStr))
             || (NULL == CU_add_test(pSuite, "strToLong", test_strToLong))
+            || (NULL == CU_add_test(pSuite, "strToULong", test_strToULong))
             || (NULL == CU_add_test(pSuite, "strToDouble", test_strToDouble))
             || (NULL == CU_add_test(pSuite, "compareStr", test_compareStr))
             || (NULL == CU_add_test(pSuite, "compareStrAndNum", test_compareStrAndNum))
@@ -472,6 +499,7 @@
     /* Run all tests using the CUnit Basic interface */
     CU_basic_set_mode(CU_BRM_VERBOSE);
     CU_basic_run_tests();
+    result = CU_get_number_of_tests_failed();
     CU_cleanup_registry();
-    return CU_get_error();
+    return result ? result : CU_get_error();
 }

--
Gitblit v1.9.1