Commit graph

1 commit

Author SHA1 Message Date
Vadim Peretokin
b5b082c375
fix: cap the length of inserted text like echoed text (#9497)
#### Brief overview of PR changes/additions

`TBuffer::insertInLine()` - the code path behind the Lua
`insertText`/`cinsertText` API - now enforces the same
`MAX_CHARACTERS_PER_ECHO` (1,000,000) per-echo character cap that the
normal echo/print path (`appendLine()`) already applies. It also now
splices the inserted run into the line in a single operation instead of
one character at a time.

#### Motivation for adding to Mudlet

Two problems with the old code:

1. **Uncapped insert.** The echo/append path truncates a single echo to
`MAX_CHARACTERS_PER_ECHO`, but `insertInLine()` did not. A single
`insertText()` with a very large string was therefore unbounded, growing
a line without limit (unnecessary memory pressure / potential DoS). The
fix caps the inserted text to the same limit with the same
truncate-in-place semantics, so oversized inserts are now bounded
consistently across both paths.
2. **Quadratic insert.** The old code inserted characters one at a time
into the middle of a `QString` and a `std::deque<TChar>`; each
mid-container insert is O(n), making a large insert O(n^2). The run is
now inserted in one operation (`QString::insert` + `std::deque::insert`
with a count), producing byte-for-byte identical buffer contents.

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

Adds a functional test `InsertTextCapTest` (ephemeral stub port) that
inserts an oversized string mid-line via `insertText()` and asserts the
inserted run is capped to the limit, plus a control that a normal-sized
insert is spliced in unchanged at the cursor (verifying placement and
that the character/styling containers stay in sync). Verified fail-first
against baseline. The broader TBuffer/display functional suite
(`TelnetTextDisplayedTest`, `MainConsoleSelectionTest`, `TOscTest`,
`TriggerEditorTest`, `TFeedTriggersRecursionTest`) passes.

Assisted-by: Claude:claude-opus-4-8
2026-07-30 15:52:22 +02:00