From 65bccb7c97e8078de033812bf83a7307b9045c25 Mon Sep 17 00:00:00 2001 From: Darkvater Date: Sat, 15 Apr 2006 16:03:45 +0000 Subject: [PATCH] (svn r4429) - CodeChange[newgrf]: Action0, prop 0x0D. use calloc to allocate memory and zero it out and change the loop a little --- newgrf.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/newgrf.c b/newgrf.c index c9e0eae426..60d94731b3 100644 --- a/newgrf.c +++ b/newgrf.c @@ -1014,29 +1014,27 @@ static bool BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int Bridge *bridge = &_bridge[brid + i]; byte tableid = grf_load_byte(&buf); byte numtables = grf_load_byte(&buf); - byte table, sprite; if (bridge->sprite_table == NULL) { - /* Allocate memory for sprite table pointers and set to NULL */ - bridge->sprite_table = malloc(7 * sizeof(*bridge->sprite_table)); - for (table = 0; table < 7; table++) - bridge->sprite_table[table] = NULL; + /* Allocate memory for sprite table pointers and zero out */ + bridge->sprite_table = calloc(7, sizeof(*bridge->sprite_table)); } - for (table = tableid; table < tableid + numtables; table++) { - if (table < 7) { - if (bridge->sprite_table[table] == NULL) { - bridge->sprite_table[table] = malloc(32 * sizeof(**bridge->sprite_table)); - } + for (; numtables-- != 0; tableid++) { + byte sprite; - for (sprite = 0; sprite < 32; sprite++) - bridge->sprite_table[table][sprite] = grf_load_dword(&buf); - } else { - grfmsg(GMS_WARN, "BridgeChangeInfo: Table %d >= 7, skipping.", table); - // Skip past invalid data. - for (sprite = 0; sprite < 32; sprite++) - grf_load_dword(&buf); + if (tableid >= 7) { // skip invalid data + grfmsg(GMS_WARN, "BridgeChangeInfo: Table %d >= 7, skipping.", tableid); + for (sprite = 0; sprite < 32; sprite++) grf_load_dword(&buf); + continue; } + + if (bridge->sprite_table[tableid] == NULL) { + bridge->sprite_table[tableid] = malloc(32 * sizeof(**bridge->sprite_table)); + } + + for (sprite = 0; sprite < 32; sprite++) + bridge->sprite_table[tableid][sprite] = grf_load_dword(&buf); } } break;