From 9a6998882aed606ff5a7547c0d9a96fa9e0b1612 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Mon, 27 Jan 2014 20:57:20 +0100 Subject: [PATCH] file: Provide EF_WITH_CADEF_MAX_ENTRY_SIZE to clean up xditem() xditem() needs a buffer that can hold entries of any xdumpable table. It's been 2048 bytes and marked FIXME since day one. Clean it up so that if anyone ever goes crazy with entry sizes, we fail an assertion during startup instead of overrunning the buffer during play. Signed-off-by: Markus Armbruster --- include/file.h | 8 +++++++- src/lib/commands/xdump.c | 4 ++-- src/lib/common/filetable.c | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/file.h b/include/file.h index 73be69bd..ac6f39e5 100644 --- a/include/file.h +++ b/include/file.h @@ -27,7 +27,7 @@ * file.h: Describes Empire tables (`files' for historical reasons) * * Known contributors to this file: - * Markus Armbruster, 2005-2012 + * Markus Armbruster, 2005-2014 */ #ifndef FILE_H @@ -130,6 +130,12 @@ struct emptypedstr { /* Create table file, clobbering any existing file */ #define EFF_CREATE bit(16) +/* + * A value larger than any struct empfile member size where member + * cadef is not null. + */ +#define EF_WITH_CADEF_MAX_ENTRY_SIZE 1024 + /* * Empire `file types' * These are really table IDs. Some tables are backed by files, some diff --git a/src/lib/commands/xdump.c b/src/lib/commands/xdump.c index 3b245b66..e7b6853b 100644 --- a/src/lib/commands/xdump.c +++ b/src/lib/commands/xdump.c @@ -27,7 +27,7 @@ * xdump.c: Extended dump * * Known contributors to this file: - * Markus Armbruster, 2004-2011 + * Markus Armbruster, 2004-2014 */ #include @@ -140,7 +140,7 @@ xditem(struct xdstr *xd, int type, char *arg) struct castr *ca; struct nstr_item ni; int n; - char buf[2048]; /* FIXME buffer size? */ + unsigned char buf[EF_WITH_CADEF_MAX_ENTRY_SIZE]; ca = ef_cadef(type); if (!ca) diff --git a/src/lib/common/filetable.c b/src/lib/common/filetable.c index 36f687ff..69b4b7c2 100644 --- a/src/lib/common/filetable.c +++ b/src/lib/common/filetable.c @@ -27,11 +27,12 @@ * filetable.c: Empire game data file descriptions. * * Known contributors to this file: - * Markus Armbruster, 2005-2013 + * Markus Armbruster, 2005-2014 */ #include +#include #include #include "commodity.h" #include "file.h" @@ -394,6 +395,11 @@ empfile_init(void) void empfile_fixup(void) { + struct empfile *ep; + empfile[EF_SECTOR].nent = WORLD_SZ(); empfile[EF_MAP].size = empfile[EF_BMAP].size = WORLD_SZ(); + + for (ep = empfile; ep->uid >= 0; ep++) + assert(!ep->cadef || ep->size <= EF_WITH_CADEF_MAX_ENTRY_SIZE); }