![]() |
Kernel pitfalls | ![]() |
The items marked (checked) were reproduced on 2026-10-06 with a small program compiled against the kernel headers with -DNO_FAST_ALLOC and AddressSanitizer.
References into containers do not survive growth (checked). H(x) returns a reference into a list cell of the hash map; any later insertion or removal which resizes the table rebuilds the cells and frees the old ones. Writing through the old reference is a use-after-free, and with the fast allocator the value silently lands in a recycled block:
hashmap<int,int> H (0);
int& r= H(1);
for (int i=2; i<100; i++) H(i)= i; // the table grows
r= 42; // use after free
cout << H[1]; // still 0 |
The same holds for a[i] and t[i] after the array or tree grows (<<), and for references obtained through subtree after the tree is modified. Keep handles, not references, across such operations. A single assignment H(x)= y is safe, even H(x)= H(z) when both insert (checked): TeXmacs is compiled as C++17, where the right-hand side of an assignment is evaluated before the left-hand side.
Reading with H(x) inserts (checked). Use H[x] or H->contains (x) to read; H(x) creates the entry with the default value, which grows the table and makes contains true afterwards.
Modifying a table while iterating (checked). An iterator walks the buckets of the live table. Inserting during the iteration can make it see keys twice or miss some: inserting 40 keys while iterating over 8 gave 53 visits for 48 keys. Collect the keys first (for instance in an array) and then modify.
clear keeps the count of entries (issue #306 of mgubi/texmacs). hashmap_rep::clear (
hashmap.cpp:127
) empties the buckets but does not reset size, so after H->clear (), N (H) still gives the old number of entries, empty is false, and the table grows earlier than needed. Assign a new table (H= hashmap<T,U> (init)) instead.
Shared representations. a= b shares; an in-place change through one handle (<<, b[i]= …, H(x)= …) is visible through all. This is the most common source of “impossible” changes, for instance a hash map stored in two structures, or a tree of the document modified through a copy which was not made with copy.
Lists are recursive. N, last_item, appending and copy recurse over the whole list; they are slow and use stack in proportion to the length. Build long sequences in an array, or by prepending and then reverse.
Unstable iteration order. The order of iterate depends on the hash values and on the size of the table, and for pointer keys on the addresses, which vary from run to run. Output which must be reproducible (files, tests) has to sort the keys.
Extension labels are not stable. The number of an extension tree label depends on the order in which names were first seen; never store it, store the name (as_string (L (t))).
Memory is not given back. Small blocks return to their free list and the chunks to nobody, so the process does not shrink after closing a large document. Memory checkers need NO_FAST_ALLOC; with the fast allocator, use-after-free errors corrupt other objects of the same size instead of crashing.
Cycles leak. Two handles which point to each other keep each other alive forever; use a plain pointer for the back link and make sure it does not outlive its target.
Not thread-safe. Reference counts and free lists are not atomic. Code running in another thread (a Qt worker, a callback of a network library) must not touch kernel objects; it has to hand its results to the main thread as plain C++ data.