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