comparison xrogue/state.c @ 166:9b5f1e6aa35a

arogue7, xrogue: fix uninitialized variables when restoring. The save and restore code assumed sizeof(long) == 4, which is not the case on x64. Reading only 4 bytes from the savefile left the others uninitialized, which led to problems like billions of experience points or gold pieces.
author John "Elwin" Edwards
date Fri, 26 Jun 2015 11:42:02 -0400
parents 1af259ac4ed2
children 6e6fb0955095
comparison
equal deleted inserted replaced
165:2d94c32a709e 166:9b5f1e6aa35a
199 return(WRITESTAT); 199 return(WRITESTAT);
200 } 200 }
201 201
202 rs_write_ulong(FILE *savef, unsigned long c) 202 rs_write_ulong(FILE *savef, unsigned long c)
203 { 203 {
204 unsigned int c2;
204 char bytes[4]; 205 char bytes[4];
205 char *buf = (char *)&c; 206 char *buf = (char *)&c;
206 207
208 if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
209 {
210 c2 = c;
211 buf = (char *) &c2;
212 }
213
207 if (big_endian) 214 if (big_endian)
208 { 215 {
209 bytes[3] = buf[0]; 216 bytes[3] = buf[0];
210 bytes[2] = buf[1]; 217 bytes[2] = buf[1];
211 bytes[1] = buf[2]; 218 bytes[1] = buf[2];
218 return(WRITESTAT); 225 return(WRITESTAT);
219 } 226 }
220 227
221 rs_write_long(FILE *savef, long c) 228 rs_write_long(FILE *savef, long c)
222 { 229 {
230 int c2;
223 char bytes[4]; 231 char bytes[4];
224 char *buf = (char *)&c; 232 char *buf = (char *)&c;
225 233
234 if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
235 {
236 c2 = c;
237 buf = (char *) &c2;
238 }
239
226 if (big_endian) 240 if (big_endian)
227 { 241 {
228 bytes[3] = buf[0]; 242 bytes[3] = buf[0];
229 bytes[2] = buf[1]; 243 bytes[2] = buf[1];
230 bytes[1] = buf[2]; 244 bytes[1] = buf[2];
310 bytes[1] = buf[2]; 324 bytes[1] = buf[2];
311 bytes[0] = buf[3]; 325 bytes[0] = buf[3];
312 buf = bytes; 326 buf = bytes;
313 } 327 }
314 328
315 *i = *((unsigned long *) buf); 329 if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
330 *i = *((unsigned int *) buf);
331 else
332 *i = *((unsigned long *) buf);
316 return(READSTAT); 333 return(READSTAT);
317 } 334 }
318 335
319 rs_read_long(int inf, long *i) 336 rs_read_long(int inf, long *i)
320 { 337 {
331 bytes[1] = buf[2]; 348 bytes[1] = buf[2];
332 bytes[0] = buf[3]; 349 bytes[0] = buf[3];
333 buf = bytes; 350 buf = bytes;
334 } 351 }
335 352
336 *i = *((long *) buf); 353 if ( (sizeof(long) == 8) && (sizeof(int) == 4) )
354 *i = *((int *) buf);
355 else
356 *i = *((long *) buf);
337 return(READSTAT); 357 return(READSTAT);
338 } 358 }
339 359
340 rs_read_boolean(int inf, bool *i) 360 rs_read_boolean(int inf, bool *i)
341 { 361 {