New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change: Don't handle 'missing' string parameters as 0. #11673
Conversation
src/strings.cpp
Outdated
case VEH_AIRCRAFT: string_id = STR_SV_AIRCRAFT_NAME; break; | ||
case SCC_WAYPOINT_NAME: { // {WAYPOINT} | ||
Waypoint *wp = Waypoint::GetIfValid(args.GetNextParameter<StationID>()); | ||
if (wp == nullptr) break; | ||
|
||
if (!wp->name.empty()) { | ||
auto tmp_params = MakeParameters(wp->name); | ||
GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params); | ||
} else { | ||
auto tmp_params = MakeParameters(wp->town->index, wp->town_cn + 1); | ||
StringID string_id = ((wp->string_id == STR_SV_STNAME_BUOY) ? STR_FORMAT_BUOY_NAME : STR_FORMAT_WAYPOINT_NAME); | ||
if (wp->town_cn != 0) string_id++; | ||
GetStringWithArgs(builder, string_id, tmp_params); | ||
} | ||
break; | ||
} | ||
|
||
GetStringWithArgs(builder, string_id, tmp_params); | ||
case SCC_VEHICLE_NAME: { // {VEHICLE} | ||
const Vehicle *v = Vehicle::GetIfValid(args.GetNextParameter<VehicleID>()); | ||
if (v == nullptr) break; | ||
|
||
if (!v->name.empty()) { | ||
auto tmp_params = MakeParameters(v->name); | ||
GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params); | ||
} else if (v->group_id != DEFAULT_GROUP) { | ||
/* The vehicle has no name, but is member of a group, so print group name */ | ||
auto tmp_params = MakeParameters(v->group_id, v->unitnumber); | ||
GetStringWithArgs(builder, STR_FORMAT_GROUP_VEHICLE_NAME, tmp_params); | ||
} else { | ||
auto tmp_params = MakeParameters(v->unitnumber); | ||
|
||
StringID string_id; | ||
switch (v->type) { | ||
default: string_id = STR_INVALID_VEHICLE; break; | ||
case VEH_TRAIN: string_id = STR_SV_TRAIN_NAME; break; | ||
case VEH_ROAD: string_id = STR_SV_ROAD_VEHICLE_NAME; break; | ||
case VEH_SHIP: string_id = STR_SV_SHIP_NAME; break; | ||
case VEH_AIRCRAFT: string_id = STR_SV_AIRCRAFT_NAME; break; | ||
} | ||
|
||
GetStringWithArgs(builder, string_id, tmp_params); | ||
} | ||
break; | ||
} | ||
break; | ||
} | ||
|
||
case SCC_SIGN_NAME: { // {SIGN} | ||
const Sign *si = Sign::GetIfValid(args.GetNextParameter<SignID>()); | ||
if (si == nullptr) break; | ||
case SCC_SIGN_NAME: { // {SIGN} | ||
const Sign *si = Sign::GetIfValid(args.GetNextParameter<SignID>()); | ||
if (si == nullptr) break; | ||
|
||
if (!si->name.empty()) { | ||
auto tmp_params = MakeParameters(si->name); | ||
GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params); | ||
} else { | ||
auto tmp_params = ArrayStringParameters<0>(); | ||
GetStringWithArgs(builder, STR_DEFAULT_SIGN_NAME, tmp_params); | ||
if (!si->name.empty()) { | ||
auto tmp_params = MakeParameters(si->name); | ||
GetStringWithArgs(builder, STR_JUST_RAW_STRING, tmp_params); | ||
} else { | ||
auto tmp_params = ArrayStringParameters<0>(); | ||
GetStringWithArgs(builder, STR_DEFAULT_SIGN_NAME, tmp_params); | ||
} | ||
break; | ||
} | ||
break; | ||
} | ||
|
||
case SCC_STATION_FEATURES: { // {STATIONFEATURES} | ||
StationGetSpecialString(builder, args.GetNextParameter<StationFacility>()); | ||
break; | ||
} | ||
case SCC_STATION_FEATURES: { // {STATIONFEATURES} | ||
StationGetSpecialString(builder, args.GetNextParameter<StationFacility>()); | ||
break; | ||
} | ||
|
||
case SCC_COLOUR: { // {COLOUR} | ||
StringControlCode scc = (StringControlCode)(SCC_BLUE + args.GetNextParameter<Colours>()); | ||
if (IsInsideMM(scc, SCC_BLUE, SCC_COLOUR)) builder.Utf8Encode(scc); | ||
break; | ||
} | ||
case SCC_COLOUR: { // {COLOUR} | ||
StringControlCode scc = (StringControlCode)(SCC_BLUE + args.GetNextParameter<Colours>()); | ||
if (IsInsideMM(scc, SCC_BLUE, SCC_COLOUR)) builder.Utf8Encode(scc); | ||
break; | ||
} | ||
|
||
default: | ||
builder.Utf8Encode(b); | ||
break; | ||
default: | ||
builder.Utf8Encode(b); | ||
break; | ||
} |
Check notice
Code scanning / CodeQL
Long switch case Note
SCC_ENCODED (81 lines)
Switch has at least one case that is too long:
SCC_GENDER_LIST (32 lines)
Switch has at least one case that is too long:
SCC_CARGO_SHORT (33 lines)
Switch has at least one case that is too long:
SCC_ENGINE_NAME (31 lines)
Switch has at least one case that is too long:
SCC_STATION_NAME (39 lines)
4fc2aaf
to
438d233
Compare
f9d844b
to
f5abdb5
Compare
PeterN/OpenTTD@invalid-parameters...glx22:OpenTTD:pr/11673 tested with
and
seems to show what I (and ScriptText validation) expect
|
f5abdb5
to
10dfed1
Compare
I've removed the second commit now because it was obviously wrong, and I think it can be raised in a separate PR. |
10dfed1
to
2ce3c3c
Compare
src/strings.cpp
Outdated
/* raw_string can be(come) nullptr when the parameter is out of range and 0 is returned instead. */ | ||
if (raw_string == nullptr) { | ||
builder += "(invalid RAW_STRING parameter)"; | ||
case SCC_RAW_STRING_POINTER: { // {RAW_STRING} | ||
const char *raw_string = args.GetNextParameterString(); | ||
FormatString(builder, raw_string, args); | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could also be nullptr
when you, for whatever reason, pass nullptr
to SetDParamStr
. You can currently do that, and it will also trigger this code.
The question is whether we want to game to crash here, or should we maybe add an assert
in SetDParamStr
where applicable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's still possible then I'll put it back to how it was I was misinformed :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe replace the comment with /* raw_string can be nullptr. */
, i.e. remove the bit about the parameter being out of range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, the comment was misleading :)
2ce3c3c
to
b2ba696
Compare
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.
b2ba696
to
def4fcd
Compare
Motivation / Problem
If not enough parameters are supplied for a string, then a value of 0 was used, which could result in incorrect information being displayed.
Players may not notice there is an issue.
Description
Instead, throw an exception and include an error in the string. This is non-translatable, like existing string error messages.
Limitations
This is not intended as a fix for GSText compatibility, that is another issue :)
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.