about summary refs log tree commit diff stats
path: root/032array.cc
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-07-29 18:40:36 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-07-29 18:40:36 -0700
commit89b87bc7c493670ecb598784e1073a09f691d43e (patch)
treeb645853bb524a9a57a897a729269786aad8c4ce6 /032array.cc
parent9570363aec35e187e2395b1760a4b94e71580ac9 (diff)
downloadmu-89b87bc7c493670ecb598784e1073a09f691d43e.tar.gz
1886 - gracefully handle malformed ingredients
For example:
  x:number <- index y:address:array:number, 3
(forgetting to do a lookup)

Thanks Caleb Couch.
Diffstat (limited to '032array.cc')
-rw-r--r--032array.cc7
1 files changed, 4 insertions, 3 deletions
diff --git a/032array.cc b/032array.cc
index 9f816ceb..5911f1c3 100644
--- a/032array.cc
+++ b/032array.cc
@@ -81,12 +81,14 @@ Recipe_ordinal["index"] = INDEX;
 case INDEX: {
   if (SIZE(ingredients) != 2) {
     raise << current_recipe_name() << ": 'index' expects exactly 2 ingredients in '" << current_instruction().to_string() << "'\n" << end();
+    products.resize(1);
     break;
   }
   reagent base = canonize(current_instruction().ingredients.at(0));
   long long int base_address = base.value;
   if (base.types.at(0) != Type_ordinal["array"]) {
     raise << current_recipe_name () << ": 'index' on a non-array " << base.original_string << '\n' << end();
+    products.resize(1);
     break;
   }
   reagent offset = canonize(current_instruction().ingredients.at(1));
@@ -171,6 +173,7 @@ INDEX_ADDRESS,
 Recipe_ordinal["index-address"] = INDEX_ADDRESS;
 :(before "End Primitive Recipe Implementations")
 case INDEX_ADDRESS: {
+  products.resize(1);
   if (SIZE(ingredients) != 2) {
     raise << current_recipe_name() << ": 'index-address' expects exactly 2 ingredients in '" << current_instruction().to_string() << "'\n" << end();
     break;
@@ -186,11 +189,9 @@ case INDEX_ADDRESS: {
   vector<type_ordinal> element_type = array_element(base.types);
   if (offset_val.at(0) < 0 || offset_val.at(0) >= Memory[base_address]) {
     raise << current_recipe_name() << ": invalid index " << offset_val.at(0) << '\n' << end();
-    products.resize(1);
     break;
   }
   long long int result = base_address + 1 + offset_val.at(0)*size_of(element_type);
-  products.resize(1);
   products.at(0).push_back(result);
   break;
 }
@@ -243,6 +244,7 @@ LENGTH,
 Recipe_ordinal["length"] = LENGTH;
 :(before "End Primitive Recipe Implementations")
 case LENGTH: {
+  products.resize(1);
   if (SIZE(ingredients) != 1) {
     raise << current_recipe_name() << ": 'length' expects exactly 2 ingredients in '" << current_instruction().to_string() << "'\n" << end();
     break;
@@ -252,7 +254,6 @@ case LENGTH: {
     raise << "tried to calculate length of non-array " << x.original_string << '\n' << end();
     break;
   }
-  products.resize(1);
   products.at(0).push_back(Memory[x.value]);
   break;
 }