open
https://gitlab.synchro.net/main/sbbs/-/work_items/1347
## Summary
When a message header outgrows its allocated blocks, `smb_putmsghdr()` moves it by calling `smb_new_msghdr()` with `SMB_SELFPACK` (or `SMB_HYPERALLOC` for a hyper-allocated base), regardless of how the base is configured:
```c
int result = smb_new_msghdr(smb, msg, (smb->status.attr & SMB_HYPERALLOC) ? SMB_HYPERALLOC : SMB_SELFPACK, false);
```
On a fast-allocation base this is the one place smblib reuses blocks in the middle of `.shd` before a pack: the moved header is placed in the first hole left by a deleted message. Everywhere else, a fast-allocation base only appends (plus reuse of a truncated tail).
The growth path dates from b18b0f6590c (2023-05-07).
## Why it matters
- Mid-file reuse is what makes a stale reference dangerous. Anything still holding the old `.shd` offset of the deleted message then reads a different message's header; `smb_getmsghdr()` checks only the header ID at that offset, not that the message number matches the index record.
- Appending (`smb_fallochdr()`) is O(1) and has no scan. The header scan is cheap (one byte per block), so speed is a minor gain; consistency with the base's configured mode is the main one.
- Cost: the old header's blocks stay a hole until the next pack, the same as for any deleted message on a fast-allocation base.
Always appending is not the fix: a self-packing base is not expected to be packed, so its growth holes would never be reclaimed.
## Why it isn't a one-line change
`smb_putmsghdr()` cannot tell fast from self-pack. Only hyper-allocation is persisted in the base status (`SMB_HYPERALLOC`). Fast vs. self-pack lives in the BBS configuration (`SUB_FAST`, `SM_FASTMAIL`) and reaches smblib only as the `storage` argument of the add/update functions (see `smb_storage_mode()` in `load_cfg.c`).
## Proposed change
1. Add a storage-mode field to `smb_t`.
2. Set it where a base is opened with the configuration in hand: `smb_open_sub()` (sub-boards and mail) and `smb_open_dir()` (file bases).
3. Have the growth path in `smb_putmsghdr()` pass that field instead of the hardcoded `SMB_SELFPACK`.
4. A base opened without configuration (`smbutil`, `fixsmb`, `chksmb`) leaves the field zero, which is `SMB_SELFPACK`, so the utilities behave as they do now.
5. Add a test beside `Test_HeaderGrowthInHyperAllocatedBase` in `src/smblib/tests/smballoctest.c`: on a fast-allocation base with a free hole, a grown header is appended rather than placed in the hole.
Alternative: a `storage` parameter on `smb_putmsghdr()` / `smb_putmsg()`. More explicit, but it touches every caller, including `smb_updatemsg()`.
## Which bases are affected
- **Sub-boards:** new sub-boards have defaulted to fast allocation since ef6afc0cd2 (2018-07-28) (`new_subboard->misc` includes `SUB_FAST` in `scfg/scfgsub.c`).
- **Mail:** self-packing by default (`SM_FASTMAIL` is not set in the stock `main.ini`); fast allocation when the sysop selects it.
- **File bases:** always self-packing (`SMB_SELFPACK` is hardcoded in `filedat.c` and `js_filebase.cpp`), so unaffected.
Header growth itself is uncommon: it needs an update that adds enough header-field data to cross a 256-byte block boundary.
## Status
Deferred. Related hardening of the same path (read errors during the scan, and a caller-held header lock being dropped) went in with eb01f9eed5 (2026-11-14).
— *Authored by Claude (Claude Code), on behalf of @rswindell*
--- SBBSecho 3.38-Linux
* Origin: Vertrauen - [vert/cvs/bbs].synchro.net (1:103/705)