about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2016-10-25 12:12:02 -0700
committerKartik K. Agaram <vc@akkartik.com>2016-10-25 12:12:02 -0700
commit2ab491c23f044b620d4b5a75ed2f3c00c1de72e1 (patch)
tree11ebeb893677d5d829a8503b2871646a5be59d0f
parentc3d39e3797a5ea1d0a2a53e9e41c64a5411fcf27 (diff)
downloadmu-2ab491c23f044b620d4b5a75ed2f3c00c1de72e1.tar.gz
3592 - warn on *any* lookup of address 0
Thanks Caleb Couch for running into this with $print.
-rw-r--r--035lookup.cc20
-rw-r--r--036refcount.cc2
2 files changed, 19 insertions, 3 deletions
diff --git a/035lookup.cc b/035lookup.cc
index d6dd86d2..6008338e 100644
--- a/035lookup.cc
+++ b/035lookup.cc
@@ -67,6 +67,15 @@ def main [
 -mem: storing 34 in location 0
 +error: can't write to location 0 in '1:address:num/lookup <- copy 34'
 
+//: attempts to /lookup address 0 always loudly fail
+:(scenario lookup_0_fails)
+% Hide_errors = true;
+def main [
+  1:address:num <- copy 0
+  2:num <- copy 1:address:num/lookup
+]
++error: tried to /lookup 0 in '2:num <- copy 1:address:num/lookup'
+
 :(code)
 void canonize(reagent& x) {
   if (is_literal(x)) return;
@@ -85,10 +94,10 @@ void lookup_memory(reagent& x) {
     raise << maybe(current_recipe_name()) << "tried to /lookup 0\n" << end();
     return;
   }
-  lookup_memory_core(x);
+  lookup_memory_core(x, /*check_for_null*/true);
 }
 
-void lookup_memory_core(reagent& x) {
+void lookup_memory_core(reagent& x, bool check_for_null) {
   if (x.value == 0) return;
   trace(9999, "mem") << "location " << x.value << " is " << no_scientific(get_or_insert(Memory, x.value)) << end();
   x.set_value(get_or_insert(Memory, x.value));
@@ -97,6 +106,12 @@ void lookup_memory_core(reagent& x) {
     trace(9999, "mem") << "skipping refcount at " << x.value << end();
     x.set_value(x.value+1);  // skip refcount
   }
+  else if (check_for_null) {
+    if (Current_routine)
+      raise << "tried to /lookup 0 in '" << to_original_string(current_instruction()) << "'\n" << end();
+    else
+      raise << "tried to /lookup 0\n" << end();
+  }
   drop_one_lookup(x);
 }
 
@@ -110,6 +125,7 @@ void test_lookup_address_skips_refcount() {
 }
 
 void test_lookup_zero_address_does_not_skip_refcount() {
+  Hide_errors = true;
   reagent x("*x:address:num");
   x.set_value(34);  // unsafe
   put(Memory, 34, 0);
diff --git a/036refcount.cc b/036refcount.cc
index 688a4682..009defa1 100644
--- a/036refcount.cc
+++ b/036refcount.cc
@@ -81,7 +81,7 @@ void decrement_refcount(int old_address, const type_tree* payload_type, int payl
 
 int payload_size(reagent/*copy*/ x) {
   x.properties.push_back(pair<string, string_tree*>("lookup", NULL));
-  lookup_memory_core(x);
+  lookup_memory_core(x, /*check for nulls*/false);
   return size_of(x) + /*refcount*/1;
 }