Skip to content

Commit 6503b87

Browse files
authored
Merge pull request scp-fs2open#6723 from Goober5000/coverity_things
Various more Coverity issues
2 parents 8102feb + 17a9127 commit 6503b87

15 files changed

Lines changed: 55 additions & 31 deletions

File tree

code/controlconfig/controlsconfig.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,7 @@ bool control_config_create_new_preset(const SCP_string& newName, bool overwrite)
17871787
// Pack the current bindings into a preset, then save the file
17881788
CC_preset preset;
17891789
preset.name = newName;
1790+
preset.type = Preset_t::pst;
17901791
std::copy(Control_config.begin(), Control_config.end(), std::back_inserter(preset.bindings));
17911792

17921793
// Done with the file

code/controlconfig/controlsconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ class CC_preset {
561561
public:
562562
SCP_vector<CCB> bindings;
563563
SCP_string name;
564-
Preset_t type;
564+
Preset_t type = Preset_t::hardcode;
565565

566566
public:
567567
CC_preset() = default;

code/fireball/fireballs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,7 @@ void fireball_render(object* obj, model_draw_list *scene)
12321232
float flare_rad = obj->radius * fi->flare_size_ratio;
12331233

12341234
matrix* warp_orientation;
1235+
matrix dest = ZERO_MATRIX;
12351236

12361237
// Flare animation selection
12371238
if (fi->warp_flare_style == warp_style::ENHANCED) {
@@ -1244,7 +1245,6 @@ void fireball_render(object* obj, model_draw_list *scene)
12441245

12451246
if (fi->warp_model_style == warp_style::CINEMATIC) {
12461247
matrix m = ZERO_MATRIX;
1247-
matrix dest = ZERO_MATRIX;
12481248

12491249
float duration_ratio = 2.0f / fb->warp_open_duration;
12501250

code/globalincs/utility.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ typename T::size_type stringcost(const T& op, const T& input, typename T::size_t
162162
}
163163

164164
template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
165-
int count_items_with_string(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const char* str)
165+
int count_items_with_string(const VECTOR_T& item_vector, FIELD_T* ITEM_T::* field, const char* str)
166166
{
167167
int count = 0;
168168
for (const ITEM_T& item : item_vector)
@@ -231,7 +231,7 @@ int count_items_with_field(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field,
231231
}
232232

233233
template <typename ITEM_T, typename FIELD_T>
234-
int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
234+
int count_items_with_string(const ITEM_T* item_array, int num_items, FIELD_T* ITEM_T::* field, const char* str)
235235
{
236236
if (item_array == nullptr)
237237
return 0;
@@ -308,7 +308,7 @@ int count_items_with_field(const ITEM_T* item_array, int num_items, FIELD_T ITEM
308308
}
309309

310310
template <typename VECTOR_T, typename ITEM_T, typename FIELD_T>
311-
int find_item_with_string(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, const char* str)
311+
int find_item_with_string(const VECTOR_T& item_vector, FIELD_T* ITEM_T::* field, const char* str)
312312
{
313313
int index = 0;
314314
for (const ITEM_T& item : item_vector)
@@ -385,7 +385,7 @@ int find_item_with_field(const VECTOR_T& item_vector, FIELD_T ITEM_T::* field, c
385385
}
386386

387387
template <typename ITEM_T, typename FIELD_T>
388-
int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T ITEM_T::* field, const char* str)
388+
int find_item_with_string(const ITEM_T* item_array, int num_items, FIELD_T* ITEM_T::* field, const char* str)
389389
{
390390
if (item_array == nullptr)
391391
return -1;

code/hud/hudconfig.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ typedef struct HUD_CONFIG_TYPE {
192192
const HC_gauge_mappings& gauge_map = HC_gauge_mappings::get_instance();
193193

194194
it = gauge_colors.find(gauge_map.get_string_id_from_numeric_id(type_it->second));
195-
196-
return it->second;
195+
if (it != gauge_colors.end()) {
196+
return it->second;
197+
}
197198
}
198199

199200
// Still nothing.. return white

code/hud/hudtargetbox.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ void HudGaugeTargetBox::renderTargetIntegrity(int disabled,int force_obj_num, bo
547547
case TS_CRT:
548548
strcpy_s(buf,XSTR( "crt", 347));
549549
break;
550+
default:
551+
UNREACHABLE("Unhandled target integrity status %d in HudGaugeTargetBox::renderTargetIntegrity!", status);
552+
buf[0] = '\0';
553+
break;
550554
}
551555

552556
if (!config) {

code/lab/renderer/lab_renderer.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,33 @@ class LabRenderer {
127127
void useNextTeamColorPreset() {
128128
if (!Team_Colors.empty()) {
129129
auto color_itr = Team_Colors.find(currentTeamColor);
130+
auto penultimate = Team_Colors.end();
131+
penultimate--;
130132

131-
if (color_itr == Team_Colors.begin()) {
132-
color_itr = --Team_Colors.end();
133-
currentTeamColor = color_itr->first;
133+
if (color_itr == Team_Colors.end()) { // not found
134+
color_itr = Team_Colors.begin();
135+
} else if (color_itr == penultimate) {
136+
color_itr = Team_Colors.begin();
134137
} else {
135-
--color_itr;
136-
currentTeamColor = color_itr->first;
138+
color_itr++;
137139
}
140+
currentTeamColor = color_itr->first;
138141
}
139142
}
140143

141144
void usePreviousTeamColorPreset() {
142145
if (!Team_Colors.empty()) {
143146
auto color_itr = Team_Colors.find(currentTeamColor);
147+
auto penultimate = Team_Colors.end();
148+
penultimate--;
144149

145-
++color_itr;
146-
if (color_itr == Team_Colors.end())
150+
if (color_itr == Team_Colors.end()) { // not found
147151
color_itr = Team_Colors.begin();
152+
} else if (color_itr == Team_Colors.begin()) {
153+
color_itr = penultimate;
154+
} else {
155+
color_itr--;
156+
}
148157
currentTeamColor = color_itr->first;
149158
}
150159
}

code/missionui/missiondebrief.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ void debrief_draw_award_text()
22032203
curr_y = start_y;
22042204

22052205
// draw the strings
2206-
for (i=0; i<Debrief_award_text_num_lines; i++) {
2206+
for (i=0; i<Debrief_award_text_num_lines && i < AWARD_TEXT_MAX_LINES; i++) {
22072207
gr_get_string_size(&sw, NULL, Debrief_award_text[i]);
22082208
x = (Medal_bitmap < 0) ? (Debrief_award_text_coords[gr_screen.res][0] + (field_width - sw) / 2) : Debrief_award_text_coords[gr_screen.res][0];
22092209
if (i==AWARD_TEXT_MAX_LINES-1) x += 7; // hack because of the shape of the box

code/model/modelread.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5890,7 +5890,7 @@ void parse_glowpoint_table(const char *filename)
58905890
}
58915891

58925892
if (optional_string("$Texture:")) {
5893-
char glow_texture_name[32];
5893+
char glow_texture_name[NAME_LENGTH];
58945894
stuff_string(glow_texture_name, F_NAME, NAME_LENGTH);
58955895

58965896
gpo.glow_bitmap_override = true;
@@ -5907,10 +5907,18 @@ void parse_glowpoint_table(const char *filename)
59075907
nprintf(("Model", "Glowpoint preset %s texture num is %d\n", gpo.name, gpo.glow_bitmap));
59085908
}
59095909

5910-
char glow_texture_neb_name[256];
5911-
strncpy(glow_texture_neb_name, glow_texture_name, 256);
5912-
strcat(glow_texture_neb_name, "-neb");
5913-
gpo.glow_neb_bitmap = bm_load(glow_texture_neb_name);
5910+
if (strlen(glow_texture_name) > MAX_FILENAME_LEN - 4 - 1)
5911+
{
5912+
nprintf(("Model", "Texture '%s' referenced by glowpoint preset '%s' is too long for the -neb suffix!\n", glow_texture_name, gpo.name));
5913+
gpo.glow_neb_bitmap = -1;
5914+
}
5915+
else
5916+
{
5917+
char glow_texture_neb_name[MAX_FILENAME_LEN];
5918+
strcpy_s(glow_texture_neb_name, glow_texture_name);
5919+
strcat_s(glow_texture_neb_name, "-neb");
5920+
gpo.glow_neb_bitmap = bm_load(glow_texture_neb_name);
5921+
}
59145922

59155923
if (gpo.glow_neb_bitmap < 0)
59165924
{

code/parse/parselo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4004,7 +4004,7 @@ SCP_vector<std::pair<size_t, size_t>> str_wrap_to_width(const SCP_string& source
40044004
lines.emplace_back(pos_start, 0);
40054005
} else {
40064006
auto sublines = str_wrap_to_width(source_string, max_pixel_width, strip_leading_whitespace, pos_start, (newline_at - pos_start));
4007-
lines.reserve(lines.size() + sublines.size());
4007+
lines.reserve(lines.size() + sublines.size()); // coverity[inefficient_reserve:FALSE]
40084008
std::move(sublines.begin(), sublines.end(), std::back_inserter(lines));
40094009
}
40104010

@@ -4032,7 +4032,7 @@ SCP_vector<std::pair<size_t, size_t>> str_wrap_to_width(const SCP_string& source
40324032
size_t search_min = 0;
40334033
size_t search_max = stringlen;
40344034
size_t center = 0;
4035-
while ((search_max - search_min) > 0) {
4035+
while (search_max > search_min) {
40364036
center = search_min + ((search_max - search_min) / 2);
40374037
gr_get_string_size(&line_width, nullptr, source_string.c_str() + pos_start, 1.0f, center);
40384038
if (line_width == max_pixel_width) {
@@ -4107,7 +4107,7 @@ SCP_vector<std::pair<size_t, size_t>> str_wrap_to_width(const char* source_strin
41074107
for (auto& subline : sublines)
41084108
subline.first += (ch_start - source_string);
41094109

4110-
lines.reserve(lines.size() + sublines.size());
4110+
lines.reserve(lines.size() + sublines.size()); // coverity[inefficient_reserve:FALSE]
41114111
std::move(sublines.begin(), sublines.end(), std::back_inserter(lines));
41124112
}
41134113

@@ -4135,7 +4135,7 @@ SCP_vector<std::pair<size_t, size_t>> str_wrap_to_width(const char* source_strin
41354135
size_t search_min = 0;
41364136
size_t search_max = stringlen;
41374137
size_t center = 0;
4138-
while ((search_max - search_min) > 0) {
4138+
while (search_max > search_min) {
41394139
center = search_min + ((search_max - search_min) / 2);
41404140
gr_get_string_size(&line_width, nullptr, ch_start, 1.0f, center);
41414141
if (line_width == max_pixel_width) {

0 commit comments

Comments
 (0)