From: Aki Tuomi Date: Wed, 19 Aug 2020 10:55:18 +0000 (+0300) Subject: global: Use i_rand_limit(limit) instead of i_rand() % limit X-Git-Tag: 2.3.13~214 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5c7b859710f98459d540efa348739f9d0849af76;p=thirdparty%2Fdovecot%2Fcore.git global: Use i_rand_limit(limit) instead of i_rand() % limit spatch with coccinelle/random-misuse.cocci --- diff --git a/coccinelle/random-misuse.cocci b/coccinelle/random-misuse.cocci new file mode 100644 index 0000000000..7522b87c2c --- /dev/null +++ b/coccinelle/random-misuse.cocci @@ -0,0 +1,13 @@ +@@ +expression E; +@@ + +-i_rand() % (E) ++i_rand_limit(E) + +@@ +expression E; +@@ + +-i_rand() % E ++i_rand_limit(E) diff --git a/src/director/director-test.c b/src/director/director-test.c index 811150f4a2..de5ff0c744 100644 --- a/src/director/director-test.c +++ b/src/director/director-test.c @@ -519,7 +519,7 @@ director_connection_disconnect_timeout(void *context ATTR_UNUSED) count++; if (count != 0) { - i = 0; count = i_rand() % count; + i = 0; count = i_rand_limit(count); for (conn = director_connections; i < count; conn = conn->next) { i_assert(conn != NULL); i++; diff --git a/src/lib-fts/test-fts-tokenizer.c b/src/lib-fts/test-fts-tokenizer.c index 146ba996b3..fcd4c6a329 100644 --- a/src/lib-fts/test-fts-tokenizer.c +++ b/src/lib-fts/test-fts-tokenizer.c @@ -490,7 +490,7 @@ static void test_fts_tokenizer_random(void) for (i = 0; i < 10000; i++) T_BEGIN { for (unsigned int j = 0; j < sizeof(addr); j++) - addr[j] = test_chars[i_rand() % N_ELEMENTS(test_chars)]; + addr[j] = test_chars[i_rand_limit(N_ELEMENTS(test_chars))]; str_truncate(str, 0); if (uni_utf8_get_valid_data(addr, sizeof(addr), str)) str_append_data(str, addr, sizeof(addr)); diff --git a/src/lib/test-array.c b/src/lib/test-array.c index 713da54340..1650e734df 100644 --- a/src/lib/test-array.c +++ b/src/lib/test-array.c @@ -117,7 +117,7 @@ static void test_array_swap(void) } } for (i = 0; i < 1000; i++) - array_swap(&foos[i_rand()%3], &foos[i_rand()%3]); + array_swap(&foos[i_rand_limit(3)], &foos[i_rand_limit(3)]); /* Just want size 3, 5, and 7 in any order */ test_assert(array_count(&foos[0]) * array_count(&foos[1]) * array_count(&foos[2]) == 3*5*7); test_assert(array_count(&foos[0]) + array_count(&foos[1]) + array_count(&foos[2]) == 3+5+7); @@ -201,10 +201,10 @@ static void test_array_cmp(void) test_assert(array_equal_fn_ctx(&arr1, &arr2, test_compare_ushort_fuzz, &fuzz) == TRUE); for (i = 0; i < 256; i++) { - unsigned int j = i_rand() % NELEMS; + unsigned int j = i_rand_limit(NELEMS); const unsigned short *ptmp = array_idx(&arr2, j); unsigned short tmp = *ptmp; - unsigned short repl = tmp + deltas[i_rand() % N_ELEMENTS(deltas)]; + unsigned short repl = tmp + deltas[i_rand_limit(N_ELEMENTS(deltas))]; array_idx_set(&arr2, j, &repl); test_assert_idx(array_cmp(&arr1, &arr2) == (tmp == repl), i); @@ -254,16 +254,16 @@ static void test_array_cmp_str(void) test_assert(array_cmp(&arr1, &arr2) == TRUE); /* pointers shared, so identical */ test_assert(array_equal_fn(&arr1, &arr2, test_compare_string) == TRUE); /* therefore value same */ for (i = 0; i < 2560; i++) { - unsigned int j = i_rand() % NELEMS; + unsigned int j = i_rand_limit(NELEMS); const char *const *ostr_p = array_idx(&arr2, j); const char *ostr = *ostr_p; unsigned int olen = strlen(ostr); - unsigned int rc = i_rand() % (olen + 1); + unsigned int rc = i_rand_limit(olen + 1); char ochar = ostr[rc]; char buf[12]; const char *bufp = buf; memcpy(buf, ostr, olen+1); - buf[rc] = i_rand() % (CHAR_MAX + 1 - CHAR_MIN) + CHAR_MIN; + buf[rc] = i_rand_limit(CHAR_MAX + 1 - CHAR_MIN) + CHAR_MIN; if(rc == olen) buf[rc+1] = '\0'; array_idx_set(&arr2, j, &bufp); diff --git a/src/lib/test-data-stack.c b/src/lib/test-data-stack.c index f96f3caf8a..a5551e0acd 100644 --- a/src/lib/test-data-stack.c +++ b/src/lib/test-data-stack.c @@ -44,7 +44,7 @@ static void test_ds_buffers(void) T_BEGIN { size_t bigleft = t_get_bytes_available(); size_t i; - for (i = 1; i < bigleft-64; i += i_rand()%32) T_BEGIN { + for (i = 1; i < bigleft-64; i += i_rand_limit(32)) T_BEGIN { unsigned char *p, *p2; size_t left; t_malloc_no0(i); @@ -131,8 +131,8 @@ static void test_ds_recursive(int count, int depth) test_begin("data-stack recursive"); for(i = 0; i < count; i++) T_BEGIN { - int number=i_rand()%100+50; - int size=i_rand()%100+50; + int number=i_rand_limit(100)+50; + int size=i_rand_limit(100)+50; test_ds_recurse(depth, number, size); } T_END; test_end(); diff --git a/src/lib/test-hash.c b/src/lib/test-hash.c index 641aa99f4d..8fe0e53f93 100644 --- a/src/lib/test-hash.c +++ b/src/lib/test-hash.c @@ -14,15 +14,15 @@ static void test_hash_random_pool(pool_t pool) keys = i_new(unsigned int, KEYMAX); keyidx = 0; hash_table_create_direct(&hash, pool, 0); for (i = 0; i < KEYMAX; i++) { - key = (i_rand() % KEYMAX) + 1; - if (i_rand() % 5 > 0) { + key = (i_rand_limit(KEYMAX)) + 1; + if (i_rand_limit(5) > 0) { if (hash_table_lookup(hash, POINTER_CAST(key)) == NULL) { hash_table_insert(hash, POINTER_CAST(key), POINTER_CAST(1)); keys[keyidx++] = key; } } else if (keyidx > 0) { - delidx = i_rand() % keyidx; + delidx = i_rand_limit(keyidx); hash_table_remove(hash, POINTER_CAST(keys[delidx])); memmove(&keys[delidx], &keys[delidx+1], (keyidx-delidx-1) * sizeof(*keys)); diff --git a/src/lib/test-istream-crlf.c b/src/lib/test-istream-crlf.c index 2db2ea932c..5239e364da 100644 --- a/src/lib/test-istream-crlf.c +++ b/src/lib/test-istream-crlf.c @@ -98,7 +98,7 @@ void test_istream_crlf(void) char buf[100]; size_t len = 0; while (len < sizeof(buf) - 1) { - switch(i_rand()%16) { + switch(i_rand_limit(16)) { case 0: goto outahere; case 1: buf[len] = '\r'; break; case 2: buf[len] = '\n'; break; diff --git a/src/lib/test-istream-multiplex.c b/src/lib/test-istream-multiplex.c index 9d7021c2e7..66186ef2b7 100644 --- a/src/lib/test-istream-multiplex.c +++ b/src/lib/test-istream-multiplex.c @@ -269,10 +269,11 @@ static void test_send_msg(struct ostream *os, uint8_t cid, const char *msg) static void test_istream_multiplex_stream_write(struct ostream *channel) { - size_t rounds = i_rand() % 10; + size_t rounds = i_rand_limit(10); for(size_t i = 0; i < rounds; i++) { - uint8_t cid = i_rand() % 2; - test_send_msg(channel, cid, msgs[1 + i_rand() % (N_ELEMENTS(msgs) - 1)]); + uint8_t cid = i_rand_limit(2); + test_send_msg(channel, cid, + msgs[1 + i_rand_limit(N_ELEMENTS(msgs) - 1)]); } } @@ -287,7 +288,7 @@ static void test_istream_multiplex_stream(void) fd_set_nonblock(fds[0], TRUE); fd_set_nonblock(fds[1], TRUE); struct ostream *os = o_stream_create_fd(fds[1], (size_t)-1); - struct istream *is = i_stream_create_fd(fds[0], 10 + i_rand() % 10); + struct istream *is = i_stream_create_fd(fds[0], 10 + i_rand_limit(10)); struct istream *chan0 = i_stream_create_multiplex(is, (size_t)-1); struct istream *chan1 = i_stream_multiplex_add_channel(chan0, 1); diff --git a/src/lib/test-multiplex.c b/src/lib/test-multiplex.c index 6e3927d2be..5bd9e7a0ba 100644 --- a/src/lib/test-multiplex.c +++ b/src/lib/test-multiplex.c @@ -35,7 +35,7 @@ static struct test_channel test_channel[2]; static void test_multiplex_channel_write(struct test_channel *channel) { unsigned char buf[128]; - size_t len = i_rand() % sizeof(buf); + size_t len = i_rand_limit(sizeof(buf)); random_fill(buf, len); o_stream_nsend(channel->out, buf, len); o_stream_nsend(channel->out_alt, buf, len); @@ -47,7 +47,7 @@ static void test_multiplex_stream_write(struct ostream *channel ATTR_UNUSED) test_channel[1].received->used > 1000) io_loop_stop(current_ioloop); else - test_multiplex_channel_write(&test_channel[i_rand() % 2]); + test_multiplex_channel_write(&test_channel[i_rand_limit(2)]); } static void test_istream_multiplex_stream_read(struct test_channel *channel) diff --git a/src/lib/test-ostream-multiplex.c b/src/lib/test-ostream-multiplex.c index 96d798a97a..8f03fab8a8 100644 --- a/src/lib/test-ostream-multiplex.c +++ b/src/lib/test-ostream-multiplex.c @@ -92,18 +92,19 @@ static void test_ostream_multiplex_stream_read(struct istream *is) static void test_ostream_multiplex_stream_write(struct ostream *channel ATTR_UNUSED) { - size_t rounds = 1 + i_rand() % 10; + size_t rounds = 1 + i_rand_limit(10); for(size_t i = 0; i < rounds; i++) { - if ((i_rand() % 2) != 0) { + if ((i_rand_limit(2)) != 0) { o_stream_cork(chan1); /* send one byte at a time */ - for(const char *p = msgs[i_rand() % N_ELEMENTS(msgs)]; + for(const char *p = msgs[i_rand_limit(N_ELEMENTS(msgs))]; *p != '\0'; p++) { o_stream_nsend(chan1, p, 1); } o_stream_uncork(chan1); } else { - o_stream_nsend_str(chan0, msgs[i_rand() % N_ELEMENTS(msgs)]); + o_stream_nsend_str(chan0, + msgs[i_rand_limit(N_ELEMENTS(msgs))]); } } } diff --git a/src/lib/test-seq-range-array.c b/src/lib/test-seq-range-array.c index f444a5b99c..f187305c2f 100644 --- a/src/lib/test-seq-range-array.c +++ b/src/lib/test-seq-range-array.c @@ -167,9 +167,9 @@ static void test_seq_range_array_random(void) i_array_init(&range, 1); memset(shadowbuf, 0, sizeof(shadowbuf)); for (i = 0; i < SEQ_RANGE_TEST_COUNT; i++) { - seq1 = i_rand() % SEQ_RANGE_TEST_BUFSIZE; - seq2 = seq1 + i_rand() % (SEQ_RANGE_TEST_BUFSIZE - seq1); - test = i_rand() % 4; + seq1 = i_rand_limit(SEQ_RANGE_TEST_BUFSIZE); + seq2 = seq1 + i_rand_limit(SEQ_RANGE_TEST_BUFSIZE - seq1); + test = i_rand_limit(4); switch (test) { case 0: ret = seq_range_array_add(&range, seq1) ? 0 : 1; /* FALSE == added */