annotate src/memfile_test.c @ 36075:f81b0f00ed22 v9.1.0707

patch 9.1.0707: [security]: invalid cursor position may cause a crash Commit: https://github.com/vim/vim/commit/396fd1ec2956307755392a1c61f55d5c1847f308 Author: Christian Brabandt <cb@256bit.org> Date: Sat Aug 31 17:58:16 2024 +0200 patch 9.1.0707: [security]: invalid cursor position may cause a crash Problem: [security]: invalid cursor position may cause a crash (after v9.1.0038) Solution: Set cursor to the last character in a line, if it would otherwise point to beyond the line; no tests added, as it is unclear how to reproduce this. Github Advisory: https://github.com/vim/vim/security/advisories/GHSA-4ghr-c62x-cqfh Co-authored-by: zeertzjq <zeertzjq@outlook.com> Signed-off-by: zeertzjq <zeertzjq@outlook.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sat, 31 Aug 2024 18:15:02 +0200
parents f41b55f9357c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10042
4aead6a9b7a9 commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents: 7883
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 *
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
4 *
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10 /*
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 * memfile_test.c: Unittests for memfile.c
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 * Mostly by Ivan Krasilnikov.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
15 #undef NDEBUG
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
16 #include <assert.h>
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
17
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
18 // Must include main.c because it contains much more than just main()
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19 #define NO_VIM_MAIN
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 #include "main.c"
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
22 // This file has to be included because the tested functions are static
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 #include "memfile.c"
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 #define index_to_key(i) ((i) ^ 15167)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26 #define TEST_COUNT 50000
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 /*
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 * Test mf_hash_*() functions.
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 */
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 static void
7827
41789f16d6b2 commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents: 7803
diff changeset
32 test_mf_hash(void)
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34 mf_hashtab_T ht;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 mf_hashitem_T *item;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 blocknr_T key;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 long_u i;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 long_u num_buckets;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 mf_hash_init(&ht);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
42 // insert some items and check invariants
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45 assert(ht.mht_count == i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
47 // check that number of buckets is a power of 2
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 num_buckets = ht.mht_mask + 1;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
49 assert(num_buckets > 0 && (num_buckets & (num_buckets - 1)) == 0);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
51 // check load factor
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 assert(ht.mht_count <= (num_buckets << MHT_LOG_LOAD_FACTOR));
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 if (i < (MHT_INIT_SIZE << MHT_LOG_LOAD_FACTOR))
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
56 // first expansion shouldn't have occurred yet
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 assert(num_buckets == MHT_INIT_SIZE);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58 assert(ht.mht_buckets == ht.mht_small_buckets);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 else
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 assert(num_buckets > MHT_INIT_SIZE);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63 assert(ht.mht_buckets != ht.mht_small_buckets);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67 assert(mf_hash_find(&ht, key) == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
69 // allocate and add new item
16928
aa9c4703a01b patch 8.1.1465: allocating wrong amount of memory
Bram Moolenaar <Bram@vim.org>
parents: 16825
diff changeset
70 item = LALLOC_CLEAR_ONE(mf_hashitem_T);
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 item->mhi_key = key;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73 mf_hash_add_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75 assert(mf_hash_find(&ht, key) == item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 if (ht.mht_mask + 1 != num_buckets)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
79 // hash table was expanded
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 assert(ht.mht_mask + 1 == num_buckets * MHT_GROWTH_FACTOR);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 assert(i + 1 == (num_buckets << MHT_LOG_LOAD_FACTOR));
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
85 // check presence of inserted items
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 item = mf_hash_find(&ht, key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 assert(item->mhi_key == key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
94 // delete some items
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 if (i % 100 < 70)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 item = mf_hash_find(&ht, key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 assert(item->mhi_key == key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 mf_hash_rem_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 assert(mf_hash_find(&ht, key) == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 mf_hash_add_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 assert(mf_hash_find(&ht, key) == item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 mf_hash_rem_item(&ht, item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 assert(mf_hash_find(&ht, key) == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 vim_free(item);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
117 // check again
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 for (i = 0; i < TEST_COUNT; i++)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
119 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
120 key = index_to_key(i);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
121 item = mf_hash_find(&ht, key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
122
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
123 if (i % 100 < 70)
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
124 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
125 assert(item == NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 else
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 assert(item != NULL);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 assert(item->mhi_key == key);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 16928
diff changeset
134 // free hash table and all remaining items
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135 mf_hash_free_all(&ht);
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 }
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138 int
7827
41789f16d6b2 commit https://github.com/vim/vim/commit/52ea13da0fe86df1abf34de52841e367035170c0
Christian Brabandt <cb@256bit.org>
parents: 7803
diff changeset
139 main(void)
2732
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
140 {
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
141 test_mf_hash();
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
142 return 0;
afb476746692 Add missing files for patch 7.3.143.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
143 }