Commit graph

1 commit

Author SHA1 Message Date
Vadim Peretokin
a140261d68
Fix six command line and console bugs (#9683)
#### Brief overview of PR changes/additions

- Deleting the window that owns a command line no longer leaves a
dangling `TCommandLine*` behind in `mSubCommandLineMap`. A new
`TMainConsole::registerSubCommandLine()` is the single place that map is
written, and it hooks `destroyed()` so the entry goes when the widget
does.
- Seven command line Lua functions located their mandatory string at
`lua_gettop(L)`, which is index `0` when they are called with no
arguments - not a valid Lua stack index, so they silently operated on
whatever an earlier call had left on the stack. The index is now
clamped, and `selectCmdLineText` pushes a real result instead of handing
back a stack leftover.
- `scrollUp`/`scrollDown` report an unknown window instead of raising,
and `prefix()`/`suffix()` only colour the text they add - `suffix()`
also puts it after the last character of the line rather than before it.

#### Motivation for adding to Mudlet

The dangling pointer is the serious one. The command line is a child
widget of the miniconsole, user window or scroll box it lives in, so Qt
frees it along with that parent while the map entry survives.
`TConsole::setFont()` walks the whole map, and that walk is reached from
`Host::setDisplayFont()` - so once any package has created and then
deleted a window carrying a command line, simply changing the display
font, its size or its antialiasing in Settings reads freed memory. No
Lua is involved, and most dereferences land in Qt's text internals, so
crashes from this are likely being filed as unrelated Qt text-layout
bugs.

The rest are Lua API correctness: calls that quietly act on unrelated
data, a return value that is really the C function object, a guard that
never fires, and colour and insert positions that land on the wrong
text.

#### Other info (issues closed, discussion etc)

Closes #9643, closes #9647, closes #9651, closes #9652, closes #9661,
closes #9674

`selectCmdLineText` now returns `true`; its wiki entry needs updating to
match.

**Test case:** with the fix reverted, the new
`SubCommandLineLifetimeTest` fails three assertions and then aborts on
an AddressSanitizer heap-use-after-free in `TCommandLine::console()`
from `TConsole::setFont()`, and 17 of the 20 new and un-pended Lua specs
fail too. With it, busted is 2246/0 twice over and ctest is 72/72
serially.

Assisted-by: Claude:claude-opus-5
2026-08-05 19:55:44 +02:00