]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
Catch selectors that make xundump oops early
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  ef_verify.c: Verify game configuration
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  *     Markus Armbruster, 2006-2008
33  */
34
35 #include <config.h>
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "file.h"
41 #include "misc.h"
42 #include "nsc.h"
43 #include "plane.h"
44 #include "product.h"
45
46 static void verify_fail(int, int, struct castr *, int, char *, ...)
47     ATTRIBUTE((format (printf, 5, 6)));
48
49 static void
50 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
51 {
52     int i;
53     va_list ap;
54
55     /* Find base table of view, if any */
56     for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
57
58     fprintf(stderr, "%s %s uid %d",
59             EF_IS_GAME_STATE(i) ? "File" : "Config",
60             ef_nameof(type), row);
61     if (ca) {
62         fprintf(stderr, " field %s", ca->ca_name);
63         if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
64             fprintf(stderr, "(%d)", idx);
65     }
66     fprintf(stderr, ": ");
67     va_start(ap, fmt);
68     vfprintf(stderr, fmt, ap);
69     va_end(ap);
70     putc('\n', stderr);
71 }
72
73 static int
74 verify_ca(int type)
75 {
76     struct castr *ca = ef_cadef(type);
77     int i;
78
79     for (i = 0; ca[i].ca_name; i++) {
80         /*
81          * Virtual selectors must be NSC_EXTRA, because xundump can't
82          * cope with them without setter methods.  Exception: if
83          * EFF_MEM is not set, xundump doesn't touch the table.
84          */
85         if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
86                         && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
87             ca[i].ca_flags |= NSC_EXTRA;
88     }
89     return 0;
90 }
91
92 static int
93 verify_row(int type, int row)
94 {
95     struct castr *ca = ef_cadef(type);
96     struct emptypedstr *row_ref;
97     int i, j, k, n;
98     struct castr *ca_sym;
99     struct valstr val;
100     int ret_val = 0;
101     int flags = ef_flags(type);
102  
103     if (flags & EFF_MEM)
104         row_ref = ef_ptr(type, row); 
105     else {
106         row_ref = malloc(empfile[type].size); 
107         ef_read(type, row, row_ref);
108     }
109
110     if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
111         if (row_ref->ef_type != type || row_ref->uid != row) {
112             verify_fail(type, row, NULL, 0, "header corrupt");
113             ret_val = -1;
114         }
115     }
116
117     for (i = 0; ca[i].ca_name; ++i) {
118         if (ca[i].ca_flags & NSC_EXTRA)
119             continue;
120         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
121         j = 0;
122         do {
123             if (ca[i].ca_table == EF_BAD)
124                 continue;
125             nstr_mksymval(&val, &ca[i], j);
126             nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
127             if (val.val_type != NSC_LONG)
128                 continue;
129             ca_sym = ef_cadef(ca[i].ca_table);
130             if (ca[i].ca_flags & NSC_BITS) {
131                 /* symbol set */
132                 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
133                     ret_val = -1;
134                     continue;
135                 }
136                 for (k = 0; k < (int)sizeof(long) * 8; k++) {
137                     if (val.val_as.lng & (1L << k))
138                         if (!symbol_by_value(1L << k,
139                                              ef_ptr(ca[i].ca_table, 0))) {
140                             verify_fail(type, row, &ca[i], j,
141                                         "bit %d is not in symbol table %s",
142                                         k, ef_nameof(ca[i].ca_table));
143                             ret_val = -1;
144                         }
145                 }
146             } else if (ca[i].ca_table == type && i == 0) {
147                 /* uid */
148                 /* Some files contain zeroed records, cope */
149                 /* TODO tighten this check */
150                 if (val.val_as.lng == 0)
151                     continue;
152                 if (val.val_as.lng != row) {
153                     verify_fail(type, row, &ca[i], j,
154                                 "value is %ld instead of %d",
155                                 val.val_as.lng, row);
156                     ret_val = -1;
157                 }
158
159             } else if (ca_sym == symbol_ca) {
160                 /* symbol */
161                 if (!symbol_by_value(val.val_as.lng,
162                                      ef_ptr(ca[i].ca_table, 0))) {
163                     verify_fail(type, row, &ca[i], j,
164                                 "value %ld is not in symbol table %s",
165                                 val.val_as.lng, ef_nameof(ca[i].ca_table));
166                     ret_val = -1;
167                 }
168             } else {
169                 /* table index */
170                 if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
171                     || val.val_as.lng < -1) {
172                     verify_fail(type, row, &ca[i], j,
173                         "value %ld indexes table %s out of bounds 0..%d",
174                         val.val_as.lng, ef_nameof(ca[i].ca_table),
175                         ef_nelem(ca[i].ca_table));
176                     ret_val = -1;
177                 }
178             }
179         } while (++j < n);
180     } 
181     if (!(flags & EFF_MEM))
182         free(row_ref);
183     return ret_val;
184 }
185
186 static void
187 pln_zap_transient_flags(void)
188 {
189     int i;
190     struct plnstr *pp;
191     int dirty = 0;
192
193     /* laziness: assumes plane file is EFF_MEM */
194     for (i = 0; (pp = getplanep(i)) != NULL; i++) {
195         if (pp->pln_flags & PLN_LAUNCHED
196             && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
197             pp->pln_flags &= ~PLN_LAUNCHED;
198             /* FIXME missile should be destroyed instead */
199             dirty = 1;
200             verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
201             /*
202              * Can't putplane() here, because pln_prewrite() crashes
203              * without a valid player.
204              */
205         }
206     }
207     if (dirty)
208         ef_flush(EF_PLANE);     /* pretty wasteful */
209 }
210
211 int
212 ef_verify()
213 {
214     struct empfile *ep;
215     int retval = 0;
216     int i;
217
218     for (ep = empfile; ep->name; ep++) {
219         if (!ef_cadef(ep->uid))
220             continue;
221         verify_ca(ep->uid);
222         for (i = 0; i < ef_nelem(ep->uid); i++) {
223             retval += verify_row(ep->uid, i);
224         }
225     }
226
227     /* Special checks */
228     for (i = 0; pchr[i].p_sname; i++) {
229         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
230             fprintf(stderr,
231                 "Config %s uid %d field level doesn't match field type\n",
232                 ef_nameof(EF_PRODUCT), i);
233             retval = -1;
234         }
235     }
236
237     pln_zap_transient_flags();
238     return retval;
239 }