]> 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 c32b4403ed2bffb1efc7c9a66c92d88d59617e11..a288cb4fd2dff30248a3a25d6ca93e222ba80b6f 100644 (file)
@@ -28,7 +28,7 @@
  *
  *  Known contributors to this file:
  *     Ron Koenderink, 2005
- *     Markus Armbruster, 2006-2010
+ *     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 *, ...)
@@ -85,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);
@@ -110,6 +157,9 @@ 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_get)
            continue;           /* virtual */
@@ -124,55 +174,17 @@ verify_row(int type, int row)
                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);
     }
@@ -226,6 +238,8 @@ ef_verify(void)
 
     /* 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",