Change: Don't handle 'missing' string parameters as 0. (#11673)

If not enough parameters are supplied for a string, then a value of 0 was used, which could result in incorrect information being displayed.

Instead, throw an exception and include an error in the string.
wip-string-2
Peter Nelson 5 months ago committed by GitHub
parent c44faf4eea
commit 7482f71692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,15 +81,13 @@ StringParameter *StringParameters::GetNextParameterPointer()
{
assert(this->next_type == 0 || (SCC_CONTROL_START <= this->next_type && this->next_type <= SCC_CONTROL_END));
if (this->offset >= this->parameters.size()) {
Debug(misc, 0, "Trying to read invalid string parameter");
return nullptr;
throw std::out_of_range("Trying to read invalid string parameter");
}
auto &param = this->parameters[this->offset++];
if (param.type != 0 && param.type != this->next_type) {
Debug(misc, 0, "Trying to read string parameter with wrong type");
this->next_type = 0;
return nullptr;
throw std::out_of_range("Trying to read string parameter with wrong type");
}
param.type = this->next_type;
this->next_type = 0;
@ -903,6 +901,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
str_stack.push(str_arg);
for (;;) {
try {
while (!str_stack.empty() && (b = Utf8Consume(&str_stack.top())) == '\0') {
str_stack.pop();
}
@ -1105,7 +1104,7 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
case SCC_RAW_STRING_POINTER: { // {RAW_STRING}
const char *raw_string = args.GetNextParameterString();
/* raw_string can be(come) nullptr when the parameter is out of range and 0 is returned instead. */
/* raw_string can be nullptr. */
if (raw_string == nullptr) {
builder += "(invalid RAW_STRING parameter)";
break;
@ -1631,6 +1630,10 @@ static void FormatString(StringBuilder &builder, const char *str_arg, StringPara
builder.Utf8Encode(b);
break;
}
} catch (std::out_of_range &e) {
Debug(misc, 0, "FormatString: {}", e.what());
builder += "(invalid parameter)";
}
}
}

@ -95,7 +95,7 @@ public:
T GetNextParameter()
{
auto ptr = GetNextParameterPointer();
return static_cast<T>(ptr == nullptr ? 0 : ptr->data);
return static_cast<T>(ptr->data);
}
/**
@ -107,7 +107,6 @@ public:
const char *GetNextParameterString()
{
auto ptr = GetNextParameterPointer();
if (ptr == nullptr) return nullptr;
return ptr->string != nullptr ? ptr->string->c_str() : ptr->string_view;
}

Loading…
Cancel
Save