]> granicus.if.org Git - check/commitdiff
Use size_t for maximal message size, allow it to be modified.
authorOren Ben-Kiki <oren@ben-kiki.org>
Wed, 30 Aug 2017 16:57:41 +0000 (19:57 +0300)
committerOren Ben-Kiki <oren@ben-kiki.org>
Wed, 30 Aug 2017 16:57:41 +0000 (19:57 +0300)
src/check.h.in
src/check_pack.c
tests/check_set_max_msg_size.c

index 458e3d1185e16f053b030abd6cafd7cb5bc9bda0..705bfd8a1c626b8253f35a10d038a0137fd92fbf 100644 (file)
@@ -2272,16 +2272,14 @@ CK_DLL_EXP void CK_EXPORT check_waitpid_and_exit(pid_t pid) CK_ATTRIBUTE_NORETUR
  * The usual size for a message is less than 80 bytes.
  *
  * If the environment variable CK_MAX_MSG_SIZE is defined to a positive value, it is used.
- * Otherwise, if a maximal message size is set via this function, it is used.
+ * Otherwise, if a positive maximal message size is set via this function, it is used.
  * Otherwise, the maximal message size is one assigned at compile time (4K bytes).
  *
- * If used, this function must be called once before any assertion is made.
- *
  * @param max_msg_size the maximal assertion message size.
  *
  * @since 0.12.0
  */
-CK_DLL_EXP void CK_EXPORT check_set_max_msg_size(int max_msg_size);
+CK_DLL_EXP void CK_EXPORT check_set_max_msg_size(size_t max_msg_size);
 
 #ifdef __cplusplus
 CK_CPPEND
index 6512fefcf1cb692dd01da443e760b307fddd2482..4f300e7204c46f4eea3500d60f1d5b7a0de3a2bf 100644 (file)
@@ -38,7 +38,7 @@
 #endif
 
 /* Maximum size for one message in the message stream. */
-static int ck_max_msg_size = 0;
+static size_t ck_max_msg_size = 0;
 #ifndef DEFAULT_MAX_MSG_SIZE
 #define DEFAULT_MAX_MSG_SIZE 4096
 #endif
@@ -51,21 +51,22 @@ static int ck_max_msg_size = 0;
  * Problems were seen in the wild with up to 4 GB reallocations.
  */
 
-void check_set_max_msg_size(int max_msg_size) {
-    if (ck_max_msg_size <= 0) {
-        char *env = getenv("CK_MAX_MSG_SIZE");
-        if (env)
-            ck_max_msg_size = atoi(env);
-        if (ck_max_msg_size <= 0)
-            ck_max_msg_size = max_msg_size;
-        if (ck_max_msg_size <= 0)
-            ck_max_msg_size = DEFAULT_MAX_MSG_SIZE;
-    }
+void check_set_max_msg_size(size_t max_msg_size)
+{
+    ck_max_msg_size = 0;
+    char *env = getenv("CK_MAX_MSG_SIZE");
+    if (env)
+        ck_max_msg_size = (size_t)strtoul(env, NULL, 10); // Cast in case size_t != unsigned long.
+    if (ck_max_msg_size == 0)
+        ck_max_msg_size = max_msg_size;
+    if (ck_max_msg_size == 0)
+        ck_max_msg_size = DEFAULT_MAX_MSG_SIZE;
 }
 
-static int get_max_msg_size(void) {
-    if (ck_max_msg_size <= 0)
-        check_set_max_msg_size(0);
+static size_t get_max_msg_size(void)
+{
+    if (ck_max_msg_size == 0)      // If check_set_max_msg_size was not called,
+        check_set_max_msg_size(0); // initialize from the environment variable (or default).
     return ck_max_msg_size;
 }
 
index 25c33f65a4409782359ba1e428326552c9767216..6ebfc8e4cc5cd005b3479cf7fce1eaa8c19e5e05 100644 (file)
@@ -64,7 +64,8 @@ int main (int argc, char *argv[])
      * Run the test suite. This is intended to trigger the "Message is too long" error.
      * Actual success/failure is determined by examining the output.
      */
-    check_set_max_msg_size(atoi(argv[1]));
+    check_set_max_msg_size(32);            // 1st call has no effect since
+    check_set_max_msg_size(atoi(argv[1])); // the 2nd call will override it.
     sr = srunner_create(make_set_max_msg_size_suite());
     srunner_run_all(sr, CK_NORMAL);
     srunner_free(sr);