From: Oren Ben-Kiki Date: Wed, 30 Aug 2017 16:57:41 +0000 (+0300) Subject: Use size_t for maximal message size, allow it to be modified. X-Git-Tag: 0.12.0~3^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=59775ceccb02d001f7023cca85579d6227b46a12;p=check Use size_t for maximal message size, allow it to be modified. --- diff --git a/src/check.h.in b/src/check.h.in index 458e3d1..705bfd8 100644 --- a/src/check.h.in +++ b/src/check.h.in @@ -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 diff --git a/src/check_pack.c b/src/check_pack.c index 6512fef..4f300e7 100644 --- a/src/check_pack.c +++ b/src/check_pack.c @@ -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; } diff --git a/tests/check_set_max_msg_size.c b/tests/check_set_max_msg_size.c index 25c33f6..6ebfc8e 100644 --- a/tests/check_set_max_msg_size.c +++ b/tests/check_set_max_msg_size.c @@ -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);