Skip to content

Commit f44cfa0

Browse files
committed
Adds more safety checks for PLR and CSG reading for the HUD_config structure.
* Adds a #define for the HC_color array size * Doxy's the HUD_config struct
1 parent 18cb07d commit f44cfa0

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

code/hud/hudconfig.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ typedef struct hc_col {
566566
ubyte r, g, b;
567567
} hc_col;
568568

569-
hc_col HC_colors[3] =
569+
hc_col HC_colors[HUD_COLOR_SIZE] =
570570
{
571571
{0, 255, 0}, // Green - get RGB from Adam so it matches palette?-??.pcx
572572
{67, 123, 203}, // Blue - get RGB from Adam so it matches palette?-??.pcx

code/hud/hudconfig.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct ai_info;
2121
#define HUD_COLOR_GREEN 0
2222
#define HUD_COLOR_BLUE 1
2323
#define HUD_COLOR_AMBER 2
24+
#define HUD_COLOR_SIZE 3 // Number of default colors. Keep this up to date.
2425

2526
// specify the max distance that the radar should detect objects
2627
// Index in Radar_ranges[] array to get values
@@ -45,19 +46,22 @@ extern int HUD_default_popup_mask2;
4546
extern int HUD_config_default_flags;
4647
extern int HUD_config_default_flags2;
4748

49+
/**
50+
* @brief Contains core HUD configuration data
51+
* @note Is not default init'd. Assumes new player, PLR, or CSG reads will correctly set data.
52+
*/
4853
typedef struct HUD_CONFIG_TYPE {
49-
int show_flags; // whether to show gauge
50-
int show_flags2; // whether to show gauge
51-
int popup_flags; // whether gauge is popup
52-
int popup_flags2; // whether gauge is popup
53-
int rp_flags; // see RP_ flags above
54-
int rp_dist; // one of RR_ #defines above
55-
int is_observer; // 1 or 0, observer mode or not, respectively
56-
int main_color; // the main color
57-
ubyte num_msg_window_lines;
58-
59-
// colors for all the gauges
60-
color clr[NUM_HUD_GAUGES];
54+
int show_flags; //!< bitfield, whether to show gauge (0 ~ 31)
55+
int show_flags2; //!< bitfield, whether to show gauge (32 ~ 63)
56+
int popup_flags; //!< bitfield, whether gauge is popup (0 ~ 31)
57+
int popup_flags2; //!< bitfield, whether gauge is popup (32 ~ 63)
58+
int rp_flags; //!< one of RP_ #defines in hudconfig.h; Chiefly shows/hides non-ship objects
59+
int rp_dist; //!< one of RR_ #defines above; Is the maxium radar view distance setting
60+
int is_observer; //!< 1 or 0, observer mode or not, respectively
61+
int main_color; //!< the default HUD_COLOR selection for all gauges; each gauge may override this with a custom RGB
62+
ubyte num_msg_window_lines; //!< Number of message lines. (Deprecated by HudGaugeMessages::Max_lines)
63+
64+
color clr[NUM_HUD_GAUGES]; //!< colors for all the gauges
6165
} HUD_CONFIG_TYPE;
6266

6367
extern HUD_CONFIG_TYPE HUD_config;

code/pilotfile/csg.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,7 @@ void pilotfile::csg_write_redalert()
966966
void pilotfile::csg_read_hud()
967967
{
968968
int idx;
969+
int strikes = 0;
969970

970971
// flags
971972
HUD_config.show_flags = cfread_int(cfp);
@@ -986,12 +987,21 @@ void pilotfile::csg_read_hud()
986987

987988
// basic colors
988989
HUD_config.main_color = cfread_int(cfp);
989-
HUD_color_alpha = cfread_int(cfp);
990+
if (HUD_config.main_color < 0 || HUD_config.main_color > HUD_COLOR_SIZE) {
991+
Warning(LOCATION, "Campaign file has invalid main color selection %i, setting to default.\n", HUD_config.main_color);
992+
HUD_config.main_color = HUD_COLOR_GREEN;
993+
}
990994

991-
if (HUD_color_alpha < HUD_COLOR_ALPHA_USER_MIN) {
995+
HUD_color_alpha = cfread_int(cfp);
996+
if (HUD_color_alpha < HUD_COLOR_ALPHA_USER_MIN || HUD_color_alpha > HUD_COLOR_ALPHA_USER_MAX) {
997+
Warning(LOCATION, "Campaign file has invalid alpha color %i, setting to default.\n", HUD_color_alpha);
992998
HUD_color_alpha = HUD_COLOR_ALPHA_DEFAULT;
993999
}
9941000

1001+
if (strikes == 3) {
1002+
Warning(LOCATION, "Campaign file has too many hud config errors, and is likely corrupted. Please verify and save your settings in the hud config menu.");
1003+
}
1004+
9951005
hud_config_record_color(HUD_config.main_color);
9961006

9971007
// gauge-specific colors

code/pilotfile/plr.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ void pilotfile::plr_write_info()
172172

173173
void pilotfile::plr_read_hud()
174174
{
175+
int strikes = 0;
175176
// flags
176177
HUD_config.show_flags = handler->readInt("show_flags");
177178
HUD_config.show_flags2 = handler->readInt("show_flags2");
@@ -187,13 +188,26 @@ void pilotfile::plr_read_hud()
187188
if (HUD_config.rp_dist < 0 || HUD_config.rp_dist >= RR_MAX_RANGES) {
188189
Warning(LOCATION, "Player file has invalid radar range %d, setting to default.\n", HUD_config.rp_dist);
189190
HUD_config.rp_dist = RR_INFINITY;
191+
strikes++;
190192
}
193+
191194
// basic colors
192195
HUD_config.main_color = handler->readInt("main_color");
193-
HUD_color_alpha = handler->readInt("color_alpha");
196+
if (HUD_config.main_color < 0 || HUD_config.main_color > HUD_COLOR_SIZE) {
197+
Warning(LOCATION, "Player file has invalid main color selection %i, setting to default.\n", HUD_config.main_color);
198+
HUD_config.main_color = HUD_COLOR_GREEN;
199+
strikes++;
200+
}
194201

195-
if (HUD_color_alpha < HUD_COLOR_ALPHA_USER_MIN) {
202+
HUD_color_alpha = handler->readInt("color_alpha");
203+
if (HUD_color_alpha < HUD_COLOR_ALPHA_USER_MIN || HUD_color_alpha > HUD_COLOR_ALPHA_USER_MAX) {
204+
Warning(LOCATION, "Player file has invalid alpha color %i, setting to default.\n", HUD_color_alpha);
196205
HUD_color_alpha = HUD_COLOR_ALPHA_DEFAULT;
206+
strikes++;
207+
}
208+
209+
if (strikes == 3) {
210+
Warning(LOCATION, "Player file has too many hud config errors, and is likely corrupted. Please verify and save your settings in the hud config menu.");
197211
}
198212

199213
hud_config_set_color(HUD_config.main_color);

0 commit comments

Comments
 (0)