Skip to content

Commit

Permalink
global: Use i_rand_limit(limit) instead of i_rand() % limit
Browse files Browse the repository at this point in the history
spatch with coccinelle/random-misuse.cocci
  • Loading branch information
cmouse committed Sep 11, 2020
1 parent a8705c0 commit 5c7b859
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 28 deletions.
13 changes: 13 additions & 0 deletions coccinelle/random-misuse.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@@
expression E;
@@

-i_rand() % (E)
+i_rand_limit(E)

@@
expression E;
@@

-i_rand() % E
+i_rand_limit(E)
2 changes: 1 addition & 1 deletion src/director/director-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
2 changes: 1 addition & 1 deletion src/lib-fts/test-fts-tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions src/lib/test-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/test-data-stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/lib/test-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/lib/test-istream-crlf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions src/lib/test-istream-multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
}
}

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/test-multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/lib/test-ostream-multiplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))]);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/test-seq-range-array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down

0 comments on commit 5c7b859

Please sign in to comment.