]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
global: use i_rand_limit() and i_rand_minmax() when possible
authorJosef 'Jeff' Sipek <jeff.sipek@dovecot.fi>
Mon, 18 Sep 2017 11:15:44 +0000 (14:15 +0300)
committerAki Tuomi <aki.tuomi@dovecot.fi>
Tue, 19 Sep 2017 09:37:41 +0000 (12:37 +0300)
34 files changed:
src/auth/auth-request.c
src/director/director-test.c
src/director/test-user-directory.c
src/doveadm/dsync/test-dsync-mailbox-tree-sync.c
src/lib-compression/test-compression.c
src/lib-dict/test-dict-client.c
src/lib-fs/fs-randomfail.c
src/lib-fts/test-fts-tokenizer.c
src/lib-index/test-mail-index-transaction-update.c
src/lib-index/test-mail-transaction-log-file.c
src/lib-mail/test-istream-header-filter.c
src/lib-mail/test-mail-html2text.c
src/lib-mail/test-message-header-decode.c
src/lib-mail/test-message-header-hash.c
src/lib-master/master-login-auth.c
src/lib-master/test-master-service-settings-cache.c
src/lib-test/test-istream.c
src/lib/net.c
src/lib/test-base32.c
src/lib/test-base64.c
src/lib/test-buffer.c
src/lib/test-istream-concat.c
src/lib/test-istream-seekable.c
src/lib/test-istream-tee.c
src/lib/test-ostream-buffer.c
src/lib/test-ostream-file.c
src/lib/test-priorityq.c
src/lib/test-strnum.c
src/lib/test-unichar.c
src/lib/timing.c
src/login-common/login-proxy.c
src/login-common/main.c
src/master/main.c
src/master/service-monitor.c

index 851f7d589bde042dd0d0a113b0a973d1704a2880..33e6a502746338a19cb9b66fb3d0c15007f384e0 100644 (file)
@@ -1924,7 +1924,7 @@ void auth_request_set_field(struct auth_request *request,
                                        "Invalid delay_until randomness number '%s'", p);
                                request->failed = TRUE;
                        } else {
-                               extra_secs = i_rand() % extra_secs;
+                               extra_secs = i_rand_limit(extra_secs);
                        }
                }
                if (str_to_time(value, &timestamp) < 0) {
index af0ba4830be7d2c30c05a5d86839278e7dd6fb31..b43a3138197522656ed3be08ee3782eef0dcf932 100644 (file)
@@ -302,9 +302,9 @@ director_connection_input(struct director_connection *conn,
        o_stream_nsend(output, data, size);
        i_stream_skip(input, size);
 
-       if (i_rand() % 3 == 0 && conn->to_delay == NULL) {
+       if (i_rand_limit(3) == 0 && conn->to_delay == NULL) {
                conn->to_delay =
-                       timeout_add(i_rand() % DIRECTOR_CONN_MAX_DELAY_MSECS,
+                       timeout_add(i_rand_limit(DIRECTOR_CONN_MAX_DELAY_MSECS),
                                    director_connection_timeout, conn);
                io_remove(&conn->in_io);
                io_remove(&conn->out_io);
@@ -435,9 +435,9 @@ static void admin_random_action(struct admin_connection *conn)
                return;
 
        hosts = array_get(&hosts_array, &count);
-       i = i_rand() % count;
+       i = i_rand_limit(count);
 
-       hosts[i]->vhost_count = (i_rand() % 20) * 10;
+       hosts[i]->vhost_count = i_rand_limit(20) * 10;
 
        admin_send(conn, t_strdup_printf("HOST-SET\t%s\t%u\n",
                net_ip2addr(&hosts[i]->ip), hosts[i]->vhost_count));
@@ -546,7 +546,7 @@ static void main_init(const char *admin_path)
        admin_read_hosts(admin);
 
        to_disconnect =
-               timeout_add(1000*(5 + i_rand()%DIRECTOR_DISCONNECT_TIMEOUT_SECS),
+               timeout_add(1000 * i_rand_minmax(5, 5 + DIRECTOR_DISCONNECT_TIMEOUT_SECS - 1),
                            director_connection_disconnect_timeout, (void *)NULL);
 }
 
index 2c62992b201a2b8164fa7069b9dc15f0fb6e03ae..3db64f0e0c26d44682d7e7111f1812724997e511 100644 (file)
@@ -78,15 +78,15 @@ static void test_user_directory_random(void)
        struct user_directory *dir;
        struct mail_host *host = t_new(struct mail_host, 1);
        time_t timestamp;
-       unsigned int i, count = 10000 + i_rand()%10000;
+       unsigned int i, count = i_rand_minmax(10000, 19999);
 
        test_begin("user directory random");
        dir = user_directory_init(USER_DIR_TIMEOUT, NULL);
        for (i = 0; i < count; i++) {
-               if (i_rand() % 10 == 0)
+               if (i_rand_limit(10) == 0)
                        timestamp = ioloop_time;
                else
-                       timestamp = ioloop_time-i_rand()%100;
+                       timestamp = ioloop_time-i_rand_limit(100);
                (void)user_directory_add(dir, i+1, host, timestamp);
        }
        verify_user_directory(dir, count);
index 50f3f70d31e270ceceb48a3b3ab8b962a6eab56a..46c9b4843c9319861bb3996fb243bf38a184a38e 100644 (file)
@@ -48,7 +48,7 @@ static struct dsync_mailbox_node *
 random_node_create(struct dsync_mailbox_tree *tree, unsigned int counter,
                   const char *name)
 {
-       return node_create(tree, counter, name, i_rand() % 10);
+       return node_create(tree, counter, name, i_rand_limit(10));
 }
 
 static void nodes_create(struct dsync_mailbox_tree *tree, unsigned int *counter,
@@ -76,7 +76,7 @@ static void
 create_random_nodes(struct dsync_mailbox_tree *tree, const char *parent_name,
                    unsigned int depth, unsigned int *counter)
 {
-       unsigned int parent_len, i, nodes_count = 1 + i_rand() % 3;
+       unsigned int parent_len, i, nodes_count = i_rand_minmax(1, 3);
        string_t *str;
 
        if (depth == MAX_DEPTH)
index 8f1d7d209a387673f2f1ce3aa0ba78ddf514d74a..79fe441e6fafe93d61b3f05da74c6120b8318ead 100644 (file)
@@ -52,8 +52,8 @@ static void test_compression_handler(const struct compression_handler *handler)
 
        /* 3) write semi-compressible data */
        for (i = 0; i < sizeof(buf); i++) {
-               if (i_rand() % 3 == 0)
-                       buf[i] = i_rand() % 4;
+               if (i_rand_limit(3) == 0)
+                       buf[i] = i_rand_limit(4);
                else
                        buf[i] = i;
        }
index 44561680bfff44b44ed47a8cef4c0f83da34ac62..3cf4dcc7511fb09ceb10b9340994707425261964 100644 (file)
@@ -55,9 +55,10 @@ int main(int argc, char *argv[])
                i_fatal("dict_init(%s) failed: %s", argv[1], error);
 
        for (i = 0;; i++) {
-               i_snprintf(key, sizeof(key), "%s/%02x", prefix, i_rand() % 0xff);
-               i_snprintf(value, sizeof(value), "%04x", i_rand() % 0xffff);
-               switch (i_rand() % 4) {
+               i_snprintf(key, sizeof(key), "%s/%02x", prefix,
+                          i_rand_limit(0xff));
+               i_snprintf(value, sizeof(value), "%04x", i_rand_limit(0xffff));
+               switch (i_rand_limit(4)) {
                case 0:
                        pending++;
                        dict_lookup_async(dict, key, lookup_callback, NULL);
index 37ef24b0cbf442e88469b91d80c49b230bfb3ba3..f222fa1ef58960680a2761f752c5b0fe8e3535ef 100644 (file)
@@ -237,7 +237,7 @@ static bool fs_random_fail(struct fs *_fs, int divider, enum fs_op op)
 
        if (fs->op_probability[op] == 0)
                return FALSE;
-       if ((unsigned int)(i_rand() % (100*divider)) <= fs->op_probability[op]) {
+       if ((unsigned int)i_rand_limit(100 * divider) <= fs->op_probability[op]) {
                errno = EIO;
                fs_set_error(_fs, RANDOMFAIL_ERROR);
                return TRUE;
@@ -275,8 +275,7 @@ fs_random_fail_range(struct fs *_fs, enum fs_op op, uoff_t *offset_r)
 
        if (!fs_random_fail(_fs, 1, op))
                return FALSE;
-       *offset_r = fs->range_start[op] +
-               i_rand() % (fs->range_end[op] - fs->range_start[op] + 1);
+       *offset_r = i_rand_minmax(fs->range_start[op], fs->range_end[op]);
        return TRUE;
 }
 
index 56c77b2d300b813a4c9bed4ad139066130661481..fb43ceba363e1f5a35b288befc6e0baceb30e241 100644 (file)
@@ -113,7 +113,7 @@ test_tokenizer_inputoutput(struct fts_tokenizer *tok, const char *_input,
        /* test input in random chunks */
        outi = first_outi;
        for (i = 0; i < input_len; i += char_len) {
-               max = i_rand() % (input_len - i) + 1;
+               max = i_rand_minmax(1, input_len - i);
                for (char_len = 0; char_len < max; )
                        char_len += uni_utf8_char_bytes(input[i+char_len]);
                while (fts_tokenizer_next(tok, input+i, char_len, &token, &error) > 0) {
index 0b90cc198efefb02d2fe1a0c57b8bdf24ddedbf0..1689bb48f91258908eda25bf12fa7ce05391905e 100644 (file)
@@ -344,12 +344,12 @@ static void test_mail_index_flag_update_random(void)
 
        flags = t_new(enum mail_flags, hdr.messages_count + 1);
        for (r = 0; r < 1000; r++) {
-               change = i_rand() % (MAIL_FLAGS_NONRECENT+1);
-               seq1 = (i_rand() % hdr.messages_count) + 1;
+               change = i_rand_limit(MAIL_FLAGS_NONRECENT + 1);
+               seq1 = i_rand_minmax(1, hdr.messages_count);
                seq2 = seq1 == hdr.messages_count ? seq1 :
-                       (i_rand() % (hdr.messages_count - seq1 + 1)) + seq1;
+                       i_rand_minmax(seq1, hdr.messages_count);
 
-               switch (i_rand() % 3) {
+               switch (i_rand_limit(3)) {
                case 0:
                        modify_type = MODIFY_ADD;
                        for (seq = seq1; seq <= seq2; seq++)
index 85e075958517972a4fc8955785aa8fe0216959ad..89d4f19c87061370586b1a171d890634e14df070 100644 (file)
@@ -329,7 +329,7 @@ static void test_mail_transaction_log_file_modseq_offsets(void)
        test_assert(MODSEQ_MATCH(2, next_offset));
        /* do some random testing with cache */
        for (unsigned int i = 0; i < LOG_FILE_MODSEQ_CACHE_SIZE*10; i++) {
-               modseq = (i_rand() % max_modseq) + 1;
+               modseq = i_rand_minmax(1, max_modseq);
                test_assert(mail_transaction_log_file_get_modseq_next_offset(file, modseq, &next_offset) == 0);
                test_assert(MODSEQ_MATCH(modseq, next_offset));
        }
@@ -351,7 +351,7 @@ static void test_mail_transaction_log_file_modseq_offsets(void)
        test_assert(modseq == max_modseq);
        /* do some random testing with cache */
        for (unsigned int i = 0; i < LOG_FILE_MODSEQ_CACHE_SIZE*10; i++) {
-               modseq = (i_rand() % max_modseq) + 1;
+               modseq = i_rand_minmax(1, max_modseq);
                test_assert(mail_transaction_log_file_get_highest_modseq_at(file, modseq_next_offset[modseq], &modseq_at, &error) == 0);
                test_assert(modseq_at == modseq);
                test_assert(mail_transaction_log_file_get_highest_modseq_at(file, modseq_alt_next_offset[modseq], &modseq_at, &error) == 0);
index 7c196f2b54da079b9dc7622ea9a76ec6d8e13d5e..1b8f6200bec30f73e41c1eae7b814f41769c26f0 100644 (file)
@@ -188,7 +188,7 @@ static void add_random_text(string_t *dest, unsigned int count)
        unsigned int i;
 
        for (i = 0; i < count; i++)
-               str_append_c(dest, i_rand() % ('z'-'a'+1) + 'a');
+               str_append_c(dest, i_rand_minmax('a', 'z'));
 }
 
 static void ATTR_NULL(3)
index 4d051ce966966add6cc34a8969d041f21aeeeb30..021c74e3656de92574040e086398ae2f2e1a032a 100644 (file)
@@ -87,9 +87,9 @@ static void test_mail_html2text_random(void)
 
                ht = mail_html2text_init(0);
                for (unsigned int i = 0; i < 100; i++) {
-                       s[0] = valid_chars[i_rand() % N_ELEMENTS(valid_chars)];
-                       s[1] = valid_chars[i_rand() % N_ELEMENTS(valid_chars)];
-                       mail_html2text_more(ht, s, i_rand()%2+1, str);
+                       s[0] = valid_chars[i_rand_limit(N_ELEMENTS(valid_chars))];
+                       s[1] = valid_chars[i_rand_limit(N_ELEMENTS(valid_chars))];
+                       mail_html2text_more(ht, s, i_rand_minmax(1, 2), str);
                }
                mail_html2text_deinit(&ht);
                str_truncate(str, 0);
index 09d57effbf968e1cafaa28df73f9b40f5493644a..e115ba7aca9b614c4cc802c6b2f222fd5738ef47 100644 (file)
@@ -74,8 +74,8 @@ static void test_message_header_decode_encode_random(void)
                /* fill only with 7bit data so we don't have to worry about
                   the data being valid UTF-8 */
                for (j = 0; j < sizeof(buf); j++)
-                       buf[j] = i_rand() % 128;
-               buflen = i_rand() % sizeof(buf);
+                       buf[j] = i_rand_limit(128);
+               buflen = i_rand_limit(sizeof(buf));
 
                str_truncate(encoded, 0);
                str_truncate(decoded, 0);
index 5036132b2b7eb6eb4019f81e063782a202543fa8..9708ccb709aab58cd01fe10a98d9dd478e458903 100644 (file)
@@ -89,7 +89,7 @@ static void test_message_header_hash_more(void)
                for (unsigned int j = 0; j < input_len; ) {
                        const unsigned char *input_part =
                                (const unsigned char *)tests[i].input + j;
-                       unsigned int len = i_rand() % (input_len - j) + 1;
+                       unsigned int len = i_rand_minmax(1, input_len - j);
                        message_header_hash_more(&ctx, &hash_method_md5,
                                                 &md5_ctx, tests[i].version,
                                                 input_part, len);
index b33dbcc6d29db99ea7a87fc74c0270a0655be079..b6a6d2300a42d0ce1bc08366fe02df3a0c63fda4 100644 (file)
@@ -76,7 +76,7 @@ master_login_auth_init(const char *auth_socket_path, bool request_auth_token)
        auth->refcount = 1;
        auth->fd = -1;
        hash_table_create_direct(&auth->requests, pool, 0);
-       auth->id_counter = (i_rand() % 32767) * 131072U;
+       auth->id_counter = i_rand_limit(32767) * 131072U;
        return auth;
 }
 
index 160eefb1964a831bca060905e3e832fb1cb14397..6b8318296a47dfd166901e1b235289ebbd7e7515 100644 (file)
@@ -64,15 +64,15 @@ static void test_master_service_settings_cache_once(void)
        const struct setting_parser_context *parser;
        const char *error;
 
-       output.used_local = output.service_uses_local && (i_rand() % 2) != 0;
+       output.used_local = output.service_uses_local && i_rand_limit(2) != 0;
        if (output.used_local) {
                input.local_ip.family = AF_INET;
-               input.local_ip.u.ip4.s_addr = 100 + i_rand() % 100;
+               input.local_ip.u.ip4.s_addr = i_rand_minmax(100, 199);
        }
-       output.used_remote = output.service_uses_remote && (i_rand() % 2) != 0;
+       output.used_remote = output.service_uses_remote && i_rand_limit(2) != 0;
        if (output.used_remote) {
                input.remote_ip.family = AF_INET;
-               input.remote_ip.u.ip4.s_addr = 100 + i_rand() % 100;
+               input.remote_ip.u.ip4.s_addr = i_rand_minmax(100, 199);
        }
        test_assert(master_service_settings_cache_read(cache, &input, NULL, &parser, &error) == 0);
 }
index bf5468b9518695d761e33f2e3d571e6c9ac62e43..5a91c244fc18eacb9454c9f7296a6ac0608e0220 100644 (file)
@@ -32,7 +32,7 @@ static ssize_t test_read(struct istream_private *stream)
        } else {
                /* copy data to a buffer in somewhat random place. this could
                   help catch bugs. */
-               new_skip_diff = i_rand() % 128;
+               new_skip_diff = i_rand_limit(128);
                stream->skip = (stream->skip - tstream->skip_diff) +
                        new_skip_diff;
                stream->pos = (stream->pos - tstream->skip_diff) +
index 3ae36f1ef3f29debad8383d4921a0ce3ffca9459..ebef1cdbe88d05a4d53cc9520470b9fcef6ebdb4 100644 (file)
@@ -321,7 +321,7 @@ int net_connect_unix_with_retries(const char *path, unsigned int msecs)
                        break;
 
                /* busy. wait for a while. */
-               usleep(((i_rand() % 10) + 1) * 10000);
+               usleep(i_rand_minmax(1, 10) * 10000);
                if (gettimeofday(&now, NULL) < 0)
                        i_panic("gettimeofday() failed: %m");
        } while (timeval_diff_msecs(&now, &start) < (int)msecs);
index 673429b9bf2d655d9eee3327bcc6d644eef31491..ccdd90d27162528a829862da61a4738b7397fb25 100644 (file)
@@ -151,7 +151,7 @@ static void test_base32_random(void)
 
        test_begin("padded base32 encode/decode with random input");
        for (i = 0; i < 1000; i++) {
-               max = i_rand() % sizeof(buf);
+               max = i_rand_limit(sizeof(buf));
                for (j = 0; j < max; j++)
                        buf[j] = i_rand();
 
@@ -166,7 +166,7 @@ static void test_base32_random(void)
 
        test_begin("padded base32hex encode/decode with random input");
        for (i = 0; i < 1000; i++) {
-               max = i_rand() % sizeof(buf);
+               max = i_rand_limit(sizeof(buf));
                for (j = 0; j < max; j++)
                        buf[j] = i_rand();
 
index 0c00fb9b883761a0753b2447e90a1e6bdc4352a2..83f5c17958dd55e870b9d756f421e602e6eaa554 100644 (file)
@@ -87,7 +87,7 @@ static void test_base64_random(void)
 
        test_begin("base64 encode/decode with random input");
        for (i = 0; i < 1000; i++) {
-               max = i_rand() % sizeof(buf);
+               max = i_rand_limit(sizeof(buf));
                for (j = 0; j < max; j++)
                        buf[j] = i_rand();
 
index 73b59441d5832ec2db9ab88501c79a3ad6514043..5bc22cfc3e1dcff5b0bde24e3a5d4d9b8a6fb99c 100644 (file)
@@ -22,19 +22,19 @@ static void test_buffer_random(void)
        shadowbuf_size = 0;
        for (i = 0; i < BUF_TEST_COUNT; i++) {
                if (buf->used == BUF_TEST_SIZE) {
-                       size = shadowbuf_size = i_rand() % (buf->used - 1);
+                       size = shadowbuf_size = i_rand_limit(buf->used - 1);
                        buffer_set_used_size(buf, size);
                        memset(shadowbuf + shadowbuf_size, 0,
                               BUF_TEST_SIZE - shadowbuf_size);
                        i_assert(buf->used < BUF_TEST_SIZE);
                }
 
-               test = i_rand() % 6;
-               zero = i_rand() % 10 == 0;
+               test = i_rand_limit(6);
+               zero = i_rand_limit(10) == 0;
                switch (test) {
                case 0:
-                       pos = i_rand() % (BUF_TEST_SIZE-1);
-                       size = i_rand() % (BUF_TEST_SIZE - pos);
+                       pos = i_rand_limit(BUF_TEST_SIZE - 1);
+                       size = i_rand_limit(BUF_TEST_SIZE - pos);
                        if (!zero) {
                                buffer_write(buf, pos, testdata, size);
                                memcpy(shadowbuf + pos, testdata, size);
@@ -46,7 +46,7 @@ static void test_buffer_random(void)
                                shadowbuf_size = pos + size;
                        break;
                case 1:
-                       size = i_rand() % (BUF_TEST_SIZE - buf->used);
+                       size = i_rand_limit(BUF_TEST_SIZE - buf->used);
                        if (!zero) {
                                buffer_append(buf, testdata, size);
                                memcpy(shadowbuf + shadowbuf_size,
@@ -58,8 +58,8 @@ static void test_buffer_random(void)
                        shadowbuf_size += size;
                        break;
                case 2:
-                       pos = i_rand() % (BUF_TEST_SIZE-1);
-                       size = i_rand() % (BUF_TEST_SIZE - I_MAX(buf->used, pos));
+                       pos = i_rand_limit(BUF_TEST_SIZE - 1);
+                       size = i_rand_limit(BUF_TEST_SIZE - I_MAX(buf->used, pos));
                        if (!zero) {
                                buffer_insert(buf, pos, testdata, size);
                                memmove(shadowbuf + pos + size,
@@ -79,8 +79,8 @@ static void test_buffer_random(void)
                                shadowbuf_size = pos + size;
                        break;
                case 3:
-                       pos = i_rand() % (BUF_TEST_SIZE-1);
-                       size = i_rand() % (BUF_TEST_SIZE - pos);
+                       pos = i_rand_limit(BUF_TEST_SIZE - 1);
+                       size = i_rand_limit(BUF_TEST_SIZE - pos);
                        buffer_delete(buf, pos, size);
                        if (pos < shadowbuf_size) {
                                if (pos + size > shadowbuf_size)
@@ -97,9 +97,9 @@ static void test_buffer_random(void)
                case 4:
                        if (shadowbuf_size <= 1)
                                break;
-                       pos = i_rand() % (shadowbuf_size-1); /* dest */
-                       pos2 = i_rand() % (shadowbuf_size-1); /* source */
-                       size = i_rand() % (shadowbuf_size - I_MAX(pos, pos2));
+                       pos = i_rand_limit(shadowbuf_size - 1); /* dest */
+                       pos2 = i_rand_limit(shadowbuf_size - 1); /* source */
+                       size = i_rand_limit(shadowbuf_size - I_MAX(pos, pos2));
                        buffer_copy(buf, pos, buf, pos2, size);
                        memmove(shadowbuf + pos,
                                shadowbuf + pos2, size);
@@ -107,8 +107,8 @@ static void test_buffer_random(void)
                                shadowbuf_size = pos + size;
                        break;
                case 5:
-                       pos = i_rand() % (BUF_TEST_SIZE-1);
-                       size = i_rand() % (BUF_TEST_SIZE - pos);
+                       pos = i_rand_limit(BUF_TEST_SIZE - 1);
+                       size = i_rand_limit(BUF_TEST_SIZE - pos);
                        p = buffer_get_space_unsafe(buf, pos, size);
                        memcpy(p, testdata, size);
                        memcpy(shadowbuf + pos, testdata, size);
index 65a894996a86374b290ea943563e149195e43e6b..0248a377bc3b143f6aadcdbd9b2322d682a83345 100644 (file)
@@ -63,10 +63,10 @@ static bool test_istream_concat_random(void)
        size_t size = 0;
        unsigned int i, j, offset, stream_count, data_len, simult;
 
-       stream_count = (i_rand() % TEST_MAX_ISTREAM_COUNT) + 2;
+       stream_count = i_rand_minmax(2, TEST_MAX_ISTREAM_COUNT + 2 - 1);
        streams = t_new(struct istream *, stream_count + 1);
        for (i = 0, offset = 0; i < stream_count; i++) {
-               data_len = i_rand() % TEST_MAX_ISTREAM_SIZE + 1;
+               data_len = i_rand_minmax(1, TEST_MAX_ISTREAM_SIZE);
                w_data = t_malloc_no0(data_len);
                for (j = 0; j < data_len; j++)
                        w_data[j] = offset++;
@@ -79,7 +79,7 @@ static bool test_istream_concat_random(void)
        concat = i_stream_create_concat(streams);
        i_stream_set_max_buffer_size(concat, TEST_MAX_BUFFER_SIZE);
 
-       simult = i_rand() % TEST_MAX_ISTREAM_COUNT;
+       simult = i_rand_limit(TEST_MAX_ISTREAM_COUNT);
        if (simult > 0) {
                limits = t_new(struct istream *, simult);
                for (i = 0; i < simult; i++)
@@ -87,9 +87,9 @@ static bool test_istream_concat_random(void)
        }
 
        for (i = 0; i < 1000; i++) {
-               struct istream *input = (simult == 0) ? concat : limits[i_rand() % simult];
-               if (i_rand() % 3 == 0) {
-                       i_stream_seek(input, i_rand() % offset);
+               struct istream *input = (simult == 0) ? concat : limits[i_rand_limit(simult)];
+               if (i_rand_limit(3) == 0) {
+                       i_stream_seek(input, i_rand_limit(offset));
                } else {
                        ssize_t ret = i_stream_read(input);
                        size = i_stream_get_data_size(input);
@@ -98,7 +98,7 @@ static bool test_istream_concat_random(void)
                        } else if (input->v_offset + size != offset) {
                                test_assert(ret > 0);
                                test_assert(input->v_offset + ret <= offset);
-                               i_stream_skip(input, i_rand() % ret);
+                               i_stream_skip(input, i_rand_limit(ret));
 
                                data = i_stream_get_data(input, &size);
                                for (j = 0; j < size; j++) {
index 052825abba9785d7a98c87740f7c5412ed2e43a5..27632a867cced038d43ee4c6b9f192cbf6e0d885 100644 (file)
@@ -73,10 +73,10 @@ static void test_istream_seekable_random(void)
        size_t size;
        unsigned int i, j, offset, stream_count, data_len, buffer_size;
 
-       stream_count = (i_rand() % 10) + 2;
+       stream_count = i_rand_minmax(2, 10 + 2 - 1);
        streams = t_new(struct istream *, stream_count + 1);
        for (i = 0, offset = 0; i < stream_count; i++) {
-               data_len = i_rand() % 100 + 1;
+               data_len = i_rand_minmax(1, 100);
                w_data = t_malloc_no0(data_len);
                for (j = 0; j < data_len; j++)
                        w_data[j] = offset++;
@@ -87,7 +87,7 @@ static void test_istream_seekable_random(void)
        streams[i] = NULL;
        i_assert(offset > 0);
 
-       buffer_size = (i_rand() % 100) + 1; size = 0;
+       buffer_size = i_rand_minmax(1, 100); size = 0;
        input = i_stream_create_seekable(streams, buffer_size, fd_callback, NULL);
 
        /* first read it through */
@@ -98,8 +98,8 @@ static void test_istream_seekable_random(void)
 
        i_stream_seek(input, 0);
        for (i = 0; i < 100; i++) {
-               if (i_rand() % 3 == 0) {
-                       i_stream_seek(input, i_rand() % offset);
+               if (i_rand_limit(3) == 0) {
+                       i_stream_seek(input, i_rand_limit(offset));
                } else {
                        ssize_t ret = i_stream_read(input);
                        if (input->v_offset + size == offset)
@@ -109,7 +109,7 @@ static void test_istream_seekable_random(void)
                        } else {
                                test_assert(ret > 0);
                                test_assert(input->v_offset + ret <= offset);
-                               i_stream_skip(input, i_rand() % (ret+1));
+                               i_stream_skip(input, i_rand_limit(ret + 1));
 
                                data = i_stream_get_data(input, &size);
                                for (j = 0; j < size; j++) {
index d22438fa677bfb8000d7a4bb700e7d801ca152ec..f4be62c8362f2314b10fbc48d4d0e07fdd4a4cfa 100644 (file)
@@ -34,7 +34,7 @@ static void test_istream_tee_tailing(const char *str)
                        test_assert_idx(i_stream_read(child_input[i]) == 0, len);
                        test_assert_idx(!tee_i_stream_child_is_waiting(child_input[i]), len);
                }
-               delta = i_rand() % 32; /* may stand still */
+               delta = i_rand_limit(32); /* may stand still */
                if(delta > TEST_BUF_SIZE - len)
                        delta = 1;
        }
@@ -48,7 +48,7 @@ static void test_istream_tee_tailing(const char *str)
 
        delta = 1;
        while ((len += delta) <= TEST_STR_LEN) {
-               unsigned int lagger = i_rand() % CHILD_COUNT;
+               unsigned int lagger = i_rand_limit(CHILD_COUNT);
                test_istream_set_size(test_input, len);
                for (i = 0; i < CHILD_COUNT; i++) {
                        test_assert(i_stream_read(child_input[i]) == -2);
@@ -67,7 +67,7 @@ static void test_istream_tee_tailing(const char *str)
                        test_assert(i_stream_read(child_input[i]) == -2);
                        test_assert(!tee_i_stream_child_is_waiting(child_input[i]));
                }
-               delta = i_rand() % 31 + 1; /* mustn't stand still */
+               delta = i_rand_minmax(1, 31); /* mustn't stand still */
                if(delta > TEST_STR_LEN - len)
                        delta = 1;
        }
index c64ab3df454e59fa23a2983406cf98d8b31eccdf..40d5aeb28bea023b3a3cae5b19413e9b7852b966 100644 (file)
@@ -23,18 +23,18 @@ static void test_ostream_buffer_random_once(void)
        output = o_stream_create_buffer(buffer);
        o_stream_cork(output);
 
-       size = (i_rand() % MAX_BUFSIZE) + 1;
+       size = i_rand_minmax(1, MAX_BUFSIZE);
        random_fill(randbuf, size);
        memcpy(buf, randbuf, size);
        test_assert(o_stream_send(output, buf, size) > 0);
 
        for (i = 0; i < 10; i++) {
-               offset = i_rand() % (MAX_BUFSIZE*3);
-               size = (i_rand() % MAX_BUFSIZE) + 1;
+               offset = i_rand_limit(MAX_BUFSIZE * 3);
+               size = i_rand_minmax(1, MAX_BUFSIZE);
                random_fill(randbuf, size);
                memcpy(buf + offset, randbuf, size);
                test_assert(o_stream_pwrite(output, randbuf, size, offset) == 0);
-               if (i_rand() % 10 == 0)
+               if (i_rand_limit(10) == 0)
                        test_assert(o_stream_flush(output) > 0);
        }
 
index 51f468b3938d125a0351e68b0e62ea006aae207e..ef492483502c89c76cdde59ad11c2534361d433b 100644 (file)
@@ -30,18 +30,18 @@ static void test_ostream_file_random_once(void)
        output = o_stream_create_fd(fd, MAX_BUFSIZE);
        o_stream_cork(output);
 
-       size = (i_rand() % MAX_BUFSIZE) + 1;
+       size = i_rand_minmax(1, MAX_BUFSIZE);
        random_fill(randbuf, size);
        memcpy(buf, randbuf, size);
        test_assert(o_stream_send(output, buf, size) > 0);
 
        for (i = 0; i < 10; i++) {
-               offset = i_rand() % (MAX_BUFSIZE*3);
-               size = (i_rand() % MAX_BUFSIZE) + 1;
+               offset = i_rand_limit(MAX_BUFSIZE * 3);
+               size = i_rand_minmax(1, MAX_BUFSIZE);
                random_fill(randbuf, size);
                memcpy(buf + offset, randbuf, size);
                test_assert(o_stream_pwrite(output, randbuf, size, offset) == 0);
-               if (i_rand() % 10 == 0)
+               if (i_rand_limit(10) == 0)
                        test_assert(o_stream_flush(output) > 0);
        }
 
index 4510d89ed6c20ca14dd3f7e594554d076c61d6d1..523440a75c98a1c2b99e38023e5fd85a05342355 100644 (file)
@@ -79,11 +79,11 @@ void test_priorityq(void)
        for (i = 0; i < 100; i++) {
                pq = priorityq_init(cmp_int, 1);
                for (j = 0; j < PQ_MAX_ITEMS; j++) {
-                       items[j].num = i_rand() % INT_MAX;
+                       items[j].num = i_rand_limit(INT_MAX);
                        priorityq_add(pq, &items[j].item);
                }
                for (j = 0; j < PQ_MAX_ITEMS; j++) {
-                       if (i_rand() % 3 == 0) {
+                       if (i_rand_limit(3) == 0) {
                                priorityq_remove(pq, &items[j].item);
                                items[j].num = -1;
                        }
index 997f8ec473fd00d3cd650e09a1171b0606f8b176..b28f3449da46d27c265d959f4734ed70b0018344 100644 (file)
@@ -26,7 +26,7 @@ static int crappy_uintmax_to_str(char *into, uintmax_t val)
 static void test_str_to_uintmax(void)
 {
        unsigned int i=0;
-       int randrange = i_rand()%15+1; /* when 1, will max out on 1s */
+       int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
        uintmax_t value = 0, valbase = i_rand() * 1000ull;
        int len, ret;
        char buff[50]; /* totally assumes < 159 bits */
@@ -38,7 +38,7 @@ static void test_str_to_uintmax(void)
 
                value = (value << 1) + 1;
                if (value >= 64)
-                       value -= i_rand()%randrange; /* don't always test the same numbers */
+                       value -= i_rand_limit(randrange); /* don't always test the same numbers */
                len = crappy_uintmax_to_str(buff, value);
                ret = str_to_uintmax(buff, &value_back);
                test_assert_idx(ret == 0, i);
@@ -99,7 +99,7 @@ static int crappy_uintmax_to_str_hex(char *into, uintmax_t val)
 static void test_str_to_uintmax_hex(void)
 {
        unsigned int i=0;
-       int randrange = i_rand()%15+1; /* when 1, will max out on 1s */
+       int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
        uintmax_t value = 0, valbase = i_rand() * 1000ull;
        int len, ret;
        char buff[52]; /* totally assumes < 200 bits */
@@ -111,7 +111,7 @@ static void test_str_to_uintmax_hex(void)
 
                value = (value << 1) + 1;
                if (value >= 64)
-                       value -= i_rand()%randrange; /* don't always test the same numbers */
+                       value -= i_rand_limit(randrange); /* don't always test the same numbers */
                len = crappy_uintmax_to_str_hex(buff, value);
                ret = str_to_uintmax_hex(buff, &value_back);
                test_assert_idx(ret == 0, i);
@@ -175,7 +175,7 @@ static int crappy_uintmax_to_str_oct(char *into, uintmax_t val)
 static void test_str_to_uintmax_oct(void)
 {
        unsigned int i=0;
-       int randrange = i_rand()%15+1; /* when 1, will max out on 1s */
+       int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
        uintmax_t value = 0, valbase = i_rand() * 1000ull;
        int len, ret;
        char buff[69]; /* totally assumes < 200 bits */
@@ -187,7 +187,7 @@ static void test_str_to_uintmax_oct(void)
 
                value = (value << 1) + 1;
                if (value >= 64)
-                       value -= i_rand()%randrange; /* don't always test the same numbers */
+                       value -= i_rand_limit(randrange); /* don't always test the same numbers */
                len = crappy_uintmax_to_str_oct(buff, value);
                ret = str_to_uintmax_oct(buff, &value_back);
                test_assert_idx(ret == 0, i);
index 42dc85b33638b679d859ab695e6ac7e19865110f..21826ebf97ca19863b30b6e09fa0361cfee8ef9c 100644 (file)
@@ -50,7 +50,7 @@ void test_unichar(void)
                /* The bottom 6 bits should be irrelevant to code coverage,
                   only test 000000, 111111, and something in between. */
                if ((chr & 63) == 1)
-                       chr += i_rand() % 62; /* After 0, somewhere between 1 and 62 */
+                       chr += i_rand_limit(62); /* After 0, somewhere between 1 and 62 */
                else if ((chr & 63) > 0 && (chr & 63) < 63)
                        chr |= 63; /* After random, straight to 63 */
 
index 8b80ab290677d3711cba0bd2eb8b23369f71bfeb..8749ea7846cba1cbcd9609cd264995f6110f84c4 100644 (file)
@@ -39,7 +39,7 @@ void timing_add_usecs(struct timing *timing, uint64_t usecs)
                if (timing->count == 0)
                        timing->min = timing->max = usecs;
        } else {
-               unsigned int idx = i_rand() % timing->count;
+               unsigned int idx = i_rand_limit(timing->count);
                if (idx < TIMING_SUBSAMPLING_BUFFER)
                        timing->samples[idx] = usecs;
        }
index d5ce7461c3feeacfa16164e9ace4f76d90920a05..961274347bfd33807fa549086a8d612ff1fc6d10 100644 (file)
@@ -533,7 +533,7 @@ static unsigned int login_proxy_delay_disconnect(struct login_proxy *proxy)
                   processes will disconnect at slightly different times to
                   spread the load. */
                timeval_add_msecs(&rec->disconnect_timestamp,
-                                 i_rand() % PROXY_DISCONNECT_INTERVAL_MSECS);
+                                 i_rand_limit(PROXY_DISCONNECT_INTERVAL_MSECS));
        }
        rec->num_disconnects_since_ts++;
        if (proxy->to != NULL) {
index ff009fe13e5c582133565ec0551344462f973eea..8d546f8963013db676bbc76881d990ee806e9ddc 100644 (file)
@@ -380,7 +380,7 @@ static void main_preinit(void)
                /* randomize the initial index in case service_count=1
                   (although in that case it's unlikely this setting is
                   even used..) */
-               login_source_ips_idx = i_rand() % login_source_ips_count;
+               login_source_ips_idx = i_rand_limit(login_source_ips_count);
        }
 
        login_load_modules();
index d51c597edfa92805c001ca87c33b78525bcc8ebc..1e29515727964092e725e4b6b7c8652ec9c26e00 100644 (file)
@@ -344,7 +344,7 @@ static void instance_update_now(struct master_instance_list *list)
        
        if (to_instance != NULL)
                timeout_remove(&to_instance);
-       to_instance = timeout_add((3600*12 + i_rand()%(60*30)) * 1000,
+       to_instance = timeout_add((3600 * 12 + i_rand_limit(60 * 30)) * 1000,
                                  instance_update_now, list);
 }
 
index fe0e2d21413649ea31ac6e77cf8384d15dfae513..9eddaff05992b650f0e17ccdafaf35f43ff47ec5 100644 (file)
@@ -109,7 +109,7 @@ static void service_status_less(struct service_process *process,
                           signal to all of them at once */
                        process->to_idle =
                                timeout_add((service->idle_kill * 1000) +
-                                           (i_rand() % 100)*10,
+                                           i_rand_limit(100) * 10,
                                            service_process_kill_idle,
                                            process);
                }