]> git.pond.sub.org Git - empserver/blobdiff - src/lib/common/ef_verify.c
Verify game state and configuration reference sanity
[empserver] / src / lib / common / ef_verify.c
index f6bd6e23e753a79d9cda7abcafed9911b61ef0aa..a288cb4fd2dff30248a3a25d6ca93e222ba80b6f 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
@@ -29,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2006-2008
+ *     Markus Armbruster, 2006-2011
  */
 
 #include <config.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "empobj.h"
 #include "file.h"
 #include "misc.h"
 #include "nsc.h"
-#include "plane.h"
 #include "product.h"
 
 static void verify_fail(int, int, struct castr *, int, char *, ...)
@@ -49,14 +48,11 @@ static void verify_fail(int, int, struct castr *, int, char *, ...)
 static void
 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
 {
-    int i;
+    int base = empfile[type].base < 0 ? type : empfile[type].base;
     va_list ap;
 
-    /* Find base table of view, if any */
-    for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
-
     fprintf(stderr, "%s %s uid %d",
-           EF_IS_GAME_STATE(i) ? "File" : "Config",
+           EF_IS_GAME_STATE(base) ? "File" : "Config",
            ef_nameof(type), row);
     if (ca) {
        fprintf(stderr, " field %s", ca->ca_name);
@@ -89,13 +85,60 @@ verify_ca(int type)
     return 0;
 }
 
+static int
+verify_tabref(int type, int row, struct castr *ca, int idx, long val)
+{
+    int tabno = ca->ca_table;
+    struct castr *ca_sym = ef_cadef(tabno);
+    int i;
+
+    if (ca->ca_flags & NSC_BITS) {
+       /* symbol set */
+       if (CANT_HAPPEN(ca_sym != symbol_ca))
+           return -1;
+       for (i = 0; i < (int)sizeof(long) * 8; i++) {
+           if (val & (1L << i)) {
+               if (!symbol_by_value(1L << i, ef_ptr(tabno, 0))) {
+                   verify_fail(type, row, ca, idx,
+                               "bit %d is not in symbol table %s",
+                               i, ef_nameof(tabno));
+                   return -1;
+               }
+           }
+       }
+    } else if (ca_sym == symbol_ca) {
+       /* symbol */
+       if (!symbol_by_value(val, ef_ptr(tabno, 0))) {
+           verify_fail(type, row, ca, idx,
+                       "value %ld is not in symbol table %s",
+                       val, ef_nameof(tabno));
+           return -1;
+       }
+    } else {
+       /* table index */
+       if (val >= ef_nelem(tabno) || val < -1) {
+           verify_fail(type, row, ca, idx,
+                       "value %ld indexes table %s out of bounds 0..%d",
+                       val, ef_nameof(tabno), ef_nelem(tabno));
+           return -1;
+       }
+       /* laziness: assumes TABNO is EFF_MEM */
+       if (val >= 0 && !empobj_in_use(tabno, ef_ptr(tabno, val))) {
+           verify_fail(type, row, ca, idx,
+                       "value %ld refers to missing element of table %s",
+                       val, ef_nameof(tabno));
+           return -1;
+       }
+    }
+    return 0;
+}
+
 static int
 verify_row(int type, int row)
 {
     struct castr *ca = ef_cadef(type);
-    struct emptypedstr *row_ref;
-    int i, j, k, n;
-    struct castr *ca_sym;
+    struct empobj *row_ref;
+    int i, j, n;
     struct valstr val;
     int ret_val = 0;
     int flags = ef_flags(type);
@@ -114,9 +157,12 @@ verify_row(int type, int row)
        }
     }
 
+    if (!empobj_in_use(type, row_ref))
+       return ret_val;
+
     for (i = 0; ca[i].ca_name; ++i) {
-       if (ca[i].ca_flags & NSC_EXTRA)
-           continue;
+       if (ca[i].ca_get)
+           continue;           /* virtual */
        n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
        j = 0;
        do {
@@ -124,62 +170,26 @@ verify_row(int type, int row)
                continue;
            nstr_mksymval(&val, &ca[i], j);
            nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
-           if (val.val_type != NSC_LONG)
+           if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
+               ret_val = -1;
                continue;
-           ca_sym = ef_cadef(ca[i].ca_table);
-           if (ca[i].ca_flags & NSC_BITS) {
-               /* symbol set */
-               if (CANT_HAPPEN(ca_sym != symbol_ca)) {
-                   ret_val = -1;
-                   continue;
-               }
-               for (k = 0; k < (int)sizeof(long) * 8; k++) {
-                   if (val.val_as.lng & (1L << k))
-                       if (!symbol_by_value(1L << k,
-                                            ef_ptr(ca[i].ca_table, 0))) {
-                           verify_fail(type, row, &ca[i], j,
-                                       "bit %d is not in symbol table %s",
-                                       k, ef_nameof(ca[i].ca_table));
-                           ret_val = -1;
-                       }
-               }
-           } else if (ca[i].ca_table == type && i == 0) {
+           }
+           if (ca[i].ca_table == type && i == 0) {
                /* uid */
-               /* Some files contain zeroed records, cope */
-               /* TODO tighten this check */
-               if (val.val_as.lng == 0)
-                   continue;
                if (val.val_as.lng != row) {
                    verify_fail(type, row, &ca[i], j,
                                "value is %ld instead of %d",
                                val.val_as.lng, row);
                    ret_val = -1;
                }
-
-           } else if (ca_sym == symbol_ca) {
-               /* symbol */
-               if (!symbol_by_value(val.val_as.lng,
-                                    ef_ptr(ca[i].ca_table, 0))) {
-                   verify_fail(type, row, &ca[i], j,
-                               "value %ld is not in symbol table %s",
-                               val.val_as.lng, ef_nameof(ca[i].ca_table));
-                   ret_val = -1;
-               }
            } else {
-               /* table index */
-               if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
-                   || val.val_as.lng < -1) {
-                   verify_fail(type, row, &ca[i], j,
-                       "value %ld indexes table %s out of bounds 0..%d",
-                       val.val_as.lng, ef_nameof(ca[i].ca_table),
-                       ef_nelem(ca[i].ca_table));
+               if (verify_tabref(type, row, &ca[i], j, val.val_as.lng) < 0)
                    ret_val = -1;
-               }
            }
        } while (++j < n);
     }
     if (!(flags & EFF_MEM))
-        free(row_ref);
+       free(row_ref);
     return ret_val;
 }
 
@@ -211,7 +221,7 @@ pln_zap_transient_flags(void)
 }
 
 int
-ef_verify()
+ef_verify(void)
 {
     struct empfile *ep;
     int retval = 0;
@@ -228,6 +238,8 @@ ef_verify()
 
     /* Special checks */
     for (i = 0; pchr[i].p_sname; i++) {
+       if (!pchr[i].p_sname[0])
+           continue;
        if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
            fprintf(stderr,
                "Config %s uid %d field level doesn't match field type\n",