Fix: Protect against a few out of bounds or uninitialised usage errors

This commit is contained in:
Charles Pigott 2018-10-14 23:36:14 +01:00 committed by frosch
parent 52ed3bcbaa
commit b5028efc1f
9 changed files with 19 additions and 5 deletions

View File

@ -154,6 +154,7 @@ static uint32 RotateRight(uint32 val, uint32 rot)
{ {
/* Do not rotate more than necessary */ /* Do not rotate more than necessary */
rot %= 32; rot %= 32;
assert(rot > 0);
return (val >> rot) | (val << (32 - rot)); return (val >> rot) | (val << (32 - rot));
} }

View File

@ -145,6 +145,7 @@ static uint FindShipTrack(const Ship *v, TileIndex tile, DiagDirection dir, Trac
Track best_track = INVALID_TRACK; Track best_track = INVALID_TRACK;
assert(bits != TRACK_BIT_NONE);
do { do {
Track i = RemoveFirstTrack(&bits); Track i = RemoveFirstTrack(&bits);
@ -176,7 +177,7 @@ good:;
best_length = pfs.best_length; best_length = pfs.best_length;
bad:; bad:;
} while (bits != 0); } while (bits != TRACK_BIT_NONE);
*track = best_track; *track = best_track;
return best_bird_dist; return best_bird_dist;

View File

@ -28,6 +28,8 @@ template <typename Tpf> void DumpState(Tpf &pf1, Tpf &pf2)
pf2.DumpBase(dmp2); pf2.DumpBase(dmp2);
FILE *f1 = fopen("yapf1.txt", "wt"); FILE *f1 = fopen("yapf1.txt", "wt");
FILE *f2 = fopen("yapf2.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(dmp1.m_out.Data(), 1, dmp1.m_out.Size(), f1);
fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2); fwrite(dmp2.m_out.Data(), 1, dmp2.m_out.Size(), f2);
fclose(f1); fclose(f1);

View File

@ -449,6 +449,7 @@ enum Roadside {
ROADSIDE_GRASS = 1, ///< Road on grass ROADSIDE_GRASS = 1, ///< Road on grass
ROADSIDE_PAVED = 2, ///< Road with paved sidewalks ROADSIDE_PAVED = 2, ///< Road with paved sidewalks
ROADSIDE_STREET_LIGHTS = 3, ///< Road with street lights on 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_TREES = 5, ///< Road with trees on paved sidewalks
ROADSIDE_GRASS_ROAD_WORKS = 6, ///< Road on grass with road works ROADSIDE_GRASS_ROAD_WORKS = 6, ///< Road on grass with road works
ROADSIDE_PAVED_ROAD_WORKS = 7, ///< Road with sidewalks and road works ROADSIDE_PAVED_ROAD_WORKS = 7, ///< Road with sidewalks and road works

View File

@ -371,7 +371,10 @@ static bool CompareFiles(const char *n1, const char *n2)
if (f2 == NULL) return false; if (f2 == NULL) return false;
FILE *f1 = fopen(n1, "rb"); 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; size_t l1, l2;
do { do {

View File

@ -215,7 +215,10 @@ bool CompareFiles(const char *n1, const char *n2)
if (f2 == NULL) return false; if (f2 == NULL) return false;
FILE *f1 = fopen(n1, "rb"); 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; size_t l1, l2;
do { do {

View File

@ -719,7 +719,7 @@ void UpdateTownCargoTotal(Town *t)
static void UpdateTownCargoes(Town *t, TileIndex start, bool update_total = true) static void UpdateTownCargoes(Town *t, TileIndex start, bool update_total = true)
{ {
CargoArray accepted, produced; CargoArray accepted, produced;
CargoTypes dummy; CargoTypes dummy = 0;
/* Gather acceptance for all houses in an area around the start tile. /* 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 * The area is composed of the square the tile is in, extended one square in all

View File

@ -61,7 +61,7 @@ static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
*/ */
static inline bool IsValidTrackdir(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);
} }
/** /**

View File

@ -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) void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
{ {
Window *w; Window *w;
DrawPixelInfo *old_dpi = _cur_dpi;
DrawPixelInfo bk; DrawPixelInfo bk;
_cur_dpi = &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)); 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;
} }
/** /**