]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
Oops in verify_row() when non-integral selector references a table
[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-2010
32  */
33
34 #include <config.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "file.h"
40 #include "misc.h"
41 #include "nsc.h"
42 #include "plane.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 i;
52     va_list ap;
53
54     /* Find base table of view, if any */
55     for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
56
57     fprintf(stderr, "%s %s uid %d",
58             EF_IS_GAME_STATE(i) ? "File" : "Config",
59             ef_nameof(type), row);
60     if (ca) {
61         fprintf(stderr, " field %s", ca->ca_name);
62         if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
63             fprintf(stderr, "(%d)", idx);
64     }
65     fprintf(stderr, ": ");
66     va_start(ap, fmt);
67     vfprintf(stderr, fmt, ap);
68     va_end(ap);
69     putc('\n', stderr);
70 }
71
72 static int
73 verify_ca(int type)
74 {
75     struct castr *ca = ef_cadef(type);
76     int i;
77
78     for (i = 0; ca[i].ca_name; i++) {
79         /*
80          * Virtual selectors must be NSC_EXTRA, because xundump can't
81          * cope with them without setter methods.  Exception: if
82          * EFF_MEM is not set, xundump doesn't touch the table.
83          */
84         if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
85                         && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
86             ca[i].ca_flags |= NSC_EXTRA;
87     }
88     return 0;
89 }
90
91 static int
92 verify_row(int type, int row)
93 {
94     struct castr *ca = ef_cadef(type);
95     struct emptypedstr *row_ref;
96     int i, j, k, n;
97     struct castr *ca_sym;
98     struct valstr val;
99     int ret_val = 0;
100     int flags = ef_flags(type);
101
102     if (flags & EFF_MEM)
103         row_ref = ef_ptr(type, row);
104     else {
105         row_ref = malloc(empfile[type].size);
106         ef_read(type, row, row_ref);
107     }
108
109     if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
110         if (row_ref->ef_type != type || row_ref->uid != row) {
111             verify_fail(type, row, NULL, 0, "header corrupt");
112             ret_val = -1;
113         }
114     }
115
116     for (i = 0; ca[i].ca_name; ++i) {
117         if (ca[i].ca_get)
118             continue;           /* virtual */
119         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
120         j = 0;
121         do {
122             if (ca[i].ca_table == EF_BAD)
123                 continue;
124             nstr_mksymval(&val, &ca[i], j);
125             nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
126             if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
127                 ret_val = -1;
128                 continue;
129             }
130             ca_sym = ef_cadef(ca[i].ca_table);
131             if (ca[i].ca_flags & NSC_BITS) {
132                 /* symbol set */
133                 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
134                     ret_val = -1;
135                     continue;
136                 }
137                 for (k = 0; k < (int)sizeof(long) * 8; k++) {
138                     if (val.val_as.lng & (1L << k))
139                         if (!symbol_by_value(1L << k,
140                                              ef_ptr(ca[i].ca_table, 0))) {
141                             verify_fail(type, row, &ca[i], j,
142                                         "bit %d is not in symbol table %s",
143                                         k, ef_nameof(ca[i].ca_table));
144                             ret_val = -1;
145                         }
146                 }
147             } else if (ca[i].ca_table == type && i == 0) {
148                 /* uid */
149                 /* Some files contain zeroed records, cope */
150                 /* TODO tighten this check */
151                 if (val.val_as.lng == 0)
152                     continue;
153                 if (val.val_as.lng != row) {
154                     verify_fail(type, row, &ca[i], j,
155                                 "value is %ld instead of %d",
156                                 val.val_as.lng, row);
157                     ret_val = -1;
158                 }
159
160             } else if (ca_sym == symbol_ca) {
161                 /* symbol */
162                 if (!symbol_by_value(val.val_as.lng,
163                                      ef_ptr(ca[i].ca_table, 0))) {
164                     verify_fail(type, row, &ca[i], j,
165                                 "value %ld is not in symbol table %s",
166                                 val.val_as.lng, ef_nameof(ca[i].ca_table));
167                     ret_val = -1;
168                 }
169             } else {
170                 /* table index */
171                 if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
172                     || val.val_as.lng < -1) {
173                     verify_fail(type, row, &ca[i], j,
174                         "value %ld indexes table %s out of bounds 0..%d",
175                         val.val_as.lng, ef_nameof(ca[i].ca_table),
176                         ef_nelem(ca[i].ca_table));
177                     ret_val = -1;
178                 }
179             }
180         } while (++j < n);
181     }
182     if (!(flags & EFF_MEM))
183         free(row_ref);
184     return ret_val;
185 }
186
187 static void
188 pln_zap_transient_flags(void)
189 {
190     int i;
191     struct plnstr *pp;
192     int dirty = 0;
193
194     /* laziness: assumes plane file is EFF_MEM */
195     for (i = 0; (pp = getplanep(i)) != NULL; i++) {
196         if (!pp->pln_own)
197             continue;
198         if (pp->pln_flags & PLN_LAUNCHED
199             && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
200             pp->pln_flags &= ~PLN_LAUNCHED;
201             /* FIXME missile should be destroyed instead */
202             dirty = 1;
203             verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
204             /*
205              * Can't putplane() here, because pln_prewrite() crashes
206              * without a valid player.
207              */
208         }
209     }
210     if (dirty)
211         ef_flush(EF_PLANE);     /* pretty wasteful */
212 }
213
214 int
215 ef_verify(void)
216 {
217     struct empfile *ep;
218     int retval = 0;
219     int i;
220
221     for (ep = empfile; ep->name; ep++) {
222         if (!ef_cadef(ep->uid))
223             continue;
224         verify_ca(ep->uid);
225         for (i = 0; i < ef_nelem(ep->uid); i++) {
226             retval += verify_row(ep->uid, i);
227         }
228     }
229
230     /* Special checks */
231     for (i = 0; pchr[i].p_sname; i++) {
232         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
233             fprintf(stderr,
234                 "Config %s uid %d field level doesn't match field type\n",
235                 ef_nameof(EF_PRODUCT), i);
236             retval = -1;
237         }
238     }
239
240     pln_zap_transient_flags();
241     return retval;
242 }