From b5028efc1fa862073c908c435f84098ccd246fdc Mon Sep 17 00:00:00 2001 From: Charles Pigott Date: Sun, 14 Oct 2018 23:36:14 +0100 Subject: Fix: Protect against a few out of bounds or uninitialised usage errors --- src/newgrf_spritegroup.cpp | 1 + src/pathfinder/opf/opf_ship.cpp | 3 ++- src/pathfinder/yapf/yapf_rail.cpp | 2 ++ src/road_map.h | 1 + src/settingsgen/settingsgen.cpp | 5 ++++- src/strgen/strgen.cpp | 5 ++++- src/town_cmd.cpp | 2 +- src/track_func.h | 2 +- src/window.cpp | 3 +++ 9 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/newgrf_spritegroup.cpp b/src/newgrf_spritegroup.cpp index 086cbf346..59b845f10 100644 --- a/src/newgrf_spritegroup.cpp +++ b/src/newgrf_spritegroup.cpp @@ -154,6 +154,7 @@ static uint32 RotateRight(uint32 val, uint32 rot) { /* Do not rotate more than necessary */ rot %= 32; + assert(rot > 0); return (val >> rot) | (val << (32 - rot)); } diff --git a/src/pathfinder/opf/opf_ship.cpp b/src/pathfinder/opf/opf_ship.cpp index 1a866337c..c993f8203 100644 --- a/src/pathfinder/opf/opf_ship.cpp +++ b/src/pathfinder/opf/opf_ship.cpp @@ -145,6 +145,7 @@ static uint FindShipTrack(const Ship *v, TileIndex tile, DiagDirection dir, Trac Track best_track = INVALID_TRACK; + assert(bits != TRACK_BIT_NONE); do { Track i = RemoveFirstTrack(&bits); @@ -176,7 +177,7 @@ good:; best_length = pfs.best_length; bad:; - } while (bits != 0); + } while (bits != TRACK_BIT_NONE); *track = best_track; return best_bird_dist; diff --git a/src/pathfinder/yapf/yapf_rail.cpp b/src/pathfinder/yapf/yapf_rail.cpp index dccee3686..d3f8e8aee 100644 --- a/src/pathfinder/yapf/yapf_rail.cpp +++ b/src/pathfinder/yapf/yapf_rail.cpp @@ -28,6 +28,8 @@ template void DumpState(Tpf &pf1, Tpf &pf2) pf2.DumpBase(dmp2); FILE *f1 = fopen("yapf1.txt", "wt"); FILE *f2 = fopen("yapf2.txt", "wt"); + assert(f1 != NULL); + assert(f2 != NULL); fwrite(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1); fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2); fclose(f1); diff --git a/src/road_map.h b/src/road_map.h index 5b3e6b090..49526d37f 100644 --- a/src/road_map.h +++ b/src/road_map.h @@ -449,6 +449,7 @@ enum Roadside { ROADSIDE_GRASS = 1, ///< Road on grass ROADSIDE_PAVED = 2, ///< Road with paved sidewalks ROADSIDE_STREET_LIGHTS = 3, ///< Road with street lights on paved sidewalks + // 4 is unused for historical reasons ROADSIDE_TREES = 5, ///< Road with trees on paved sidewalks ROADSIDE_GRASS_ROAD_WORKS = 6, ///< Road on grass with road works ROADSIDE_PAVED_ROAD_WORKS = 7, ///< Road with sidewalks and road works diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp index 44a3f6f26..ac6db6581 100644 --- a/src/settingsgen/settingsgen.cpp +++ b/src/settingsgen/settingsgen.cpp @@ -371,7 +371,10 @@ static bool CompareFiles(const char *n1, const char *n2) if (f2 == NULL) return false; FILE *f1 = fopen(n1, "rb"); - if (f1 == NULL) error("can't open %s", n1); + if (f1 == NULL) { + fclose(f2); + error("can't open %s", n1); + } size_t l1, l2; do { diff --git a/src/strgen/strgen.cpp b/src/strgen/strgen.cpp index fa2942465..c231717da 100644 --- a/src/strgen/strgen.cpp +++ b/src/strgen/strgen.cpp @@ -215,7 +215,10 @@ bool CompareFiles(const char *n1, const char *n2) if (f2 == NULL) return false; FILE *f1 = fopen(n1, "rb"); - if (f1 == NULL) error("can't open %s", n1); + if (f1 == NULL) { + fclose(f2); + error("can't open %s", n1); + } size_t l1, l2; do { diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index b8c102ef3..927651864 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -719,7 +719,7 @@ void UpdateTownCargoTotal(Town *t) static void UpdateTownCargoes(Town *t, TileIndex start, bool update_total = true) { CargoArray accepted, produced; - CargoTypes dummy; + CargoTypes dummy = 0; /* Gather acceptance for all houses in an area around the start tile. * The area is composed of the square the tile is in, extended one square in all diff --git a/src/track_func.h b/src/track_func.h index 8e2056265..a55d6d8f2 100644 --- a/src/track_func.h +++ b/src/track_func.h @@ -61,7 +61,7 @@ static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir) */ static inline bool IsValidTrackdir(Trackdir trackdir) { - return (1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE; + return trackdir != INVALID_TRACKDIR && ((1 << trackdir & TRACKDIR_BIT_MASK) != TRACKDIR_BIT_NONE); } /** diff --git a/src/window.cpp b/src/window.cpp index d7911bfa4..cd8769e27 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -938,6 +938,8 @@ static void DrawOverlappedWindow(Window *w, int left, int top, int right, int bo void DrawOverlappedWindowForAll(int left, int top, int right, int bottom) { Window *w; + + DrawPixelInfo *old_dpi = _cur_dpi; DrawPixelInfo bk; _cur_dpi = &bk; @@ -951,6 +953,7 @@ void DrawOverlappedWindowForAll(int left, int top, int right, int bottom) DrawOverlappedWindow(w, max(left, w->left), max(top, w->top), min(right, w->left + w->width), min(bottom, w->top + w->height)); } } + _cur_dpi = old_dpi; } /** -- cgit v1.2.3-54-g00ecf