Fix ZString::operator[] to throw on out-of-range index instead of crashing

Was an unchecked std::vector index (return list[index];). Any
malformed/short line - a port scanner, a bot probing the port, or any
client sending fewer colon-delimited fields than a given handler
indexes via request[N] - reached this with no length check anywhere
upstream, across every server built on this class. Depending on build
flags that was either undefined behavior or, confirmed live (a real
crash log from Zephora's game server), a hard process abort via
libstdc++'s own vector bounds assertion:

  Assertion '__n < this->size()' failed.

Throwing instead lets this be caught: every caller that dispatches a
parsed request already wraps that dispatch in a try/catch (see
CommandList::processRequest()), so a single malformed line is now
cleanly rejected instead of taking down the entire server process for
every connected player.
This commit is contained in:
Brad Arant committed 2026-09-06 19:36:50 +00:00
1 parent a0cef8077d
commit 694c6b80a4
1 file changed
+16
+16
View File
@@ -370,6 +370,22 @@ namespace coreutils {
} }
ZString &ZString::operator[](int index) { ZString &ZString::operator[](int index) {
// Previously an unchecked std::vector index - a single
// malformed/short line (a port scanner, a bot probing the port,
// or any client sending fewer colon-delimited fields than a
// given handler indexes) reaches every command handler across
// every server built on this class via request[N] with no
// length check first. Depending on build flags that was either
// undefined behavior or - confirmed live, crashing a real game
// server - a hard process abort (libstdc++'s own vector bounds
// assertion). Throwing here instead lets it be caught: every
// caller that dispatches a parsed request already wraps that
// dispatch in a try/catch (see CommandList::processRequest()),
// so this turns "one bad line takes down the whole server" into
// "that one line is cleanly rejected."
if (index < 0 || (size_t)index >= list.size())
throw coreutils::Exception("ZString::operator[]: index " + std::to_string(index) +
" out of range (have " + std::to_string(list.size()) + ")");
return list[index]; return list[index];
} }