Quote:
Originally Posted by Lefloyd
Warum kam eigentlich keiner auf die Idee einfach eine int-Konvertierung zu benutzen? o_o
|
And why not just do this - which will only fail if CHAT_MAX_LEN > MAX_INT - 20.
This is basic math.
Code:
if (resultText.size() >= CHAT_MAX_LEN + 20)
This inequation also shows that the original code is pretty much
bullshit because the
resultText is asserted to be 20 characters
longer than CHAT_MAX_LEN.
In fact,
resultText should be at maximum 20 characters
shorter than CHAT_MAX_LEN.
The number 20 comes from the string to which
resultText is appended which is assumed 14 + 6 = 20 characters in length.
In the code, the inserted numbers are assumed to be at maximum 5 and 1 characters long.
This might not be true for
NPCs with vnums greater than 99.999 or(vnums ares of type WORD) more than 9 results.
Deducing 20 from CHAT_MAX_LEN thus does not safely determine the maximum length for
resultText in some quite realistic cases, especially as
resultCount and with it (on average proportionally)
resultText's length grow.
Code:
ch->ChatPacket(CHAT_TYPE_COMMAND, "cube r_list %d %d %s", npcVNUM, resultCount, resultText.c_str());
// example: "cube r_list 20383 4 ..."
As becomes clear the function is actually broken in several ways, and your proposed change only removes one minor issue of it.
Your turn to fix this.