From bf3e4126b28d1327f047e6047cd4638d7a129730 Mon Sep 17 00:00:00 2001
From: ridiculousfish <corydoras@ridiculousfish.com>
Date: Sun, 6 Jan 2013 14:48:46 -0800
Subject: [PATCH] Fix for builtin_test crash
 https://github.com/fish-shell/fish-shell/issues/497

---
 builtin_set.cpp  | 9 ---------
 builtin_test.cpp | 9 ++++++++-
 fish_tests.cpp   | 3 +++
 3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/builtin_set.cpp b/builtin_set.cpp
index 1b20ee913..c916d6864 100644
--- a/builtin_set.cpp
+++ b/builtin_set.cpp
@@ -772,7 +772,6 @@ static int builtin_set(parser_t &parser, wchar_t **argv)
             else
             {
                 wcstring_list_t value;
-//        al_init(&value);
 
                 while (woptind < argc)
                 {
@@ -790,17 +789,9 @@ static int builtin_set(parser_t &parser, wchar_t **argv)
 
                 my_env_set(dest, result, scope);
 
-//        al_destroy( &value );
 
             }
         }
-
-//    al_foreach( &result, &free );
-//    al_destroy( &result );
-
-//    al_destroy(&indexes);
-//    al_destroy(&values);
-
     }
     else
     {
diff --git a/builtin_test.cpp b/builtin_test.cpp
index 6f7e5acf3..79251252d 100644
--- a/builtin_test.cpp
+++ b/builtin_test.cpp
@@ -334,11 +334,12 @@ expression *test_parser::parse_combining_expression(unsigned int start, unsigned
     std::vector<expression *> subjects;
     std::vector<token_t> combiners;
     unsigned int idx = start;
+    bool first = true;
 
     while (idx < end)
     {
 
-        if (! subjects.empty())
+        if (! first)
         {
             /* This is not the first expression, so we expect a combiner. */
             token_t combiner = token_for_string(arg(idx))->tok;
@@ -357,12 +358,18 @@ expression *test_parser::parse_combining_expression(unsigned int start, unsigned
         if (! expr)
         {
             add_error(L"Missing argument at index %u", idx);
+            if (! first)
+            {
+                /* Clean up the dangling combiner, since it never got its right hand expression */
+                combiners.pop_back();
+            }
             break;
         }
 
         /* Go to the end of this expression */
         idx = expr->range.end;
         subjects.push_back(expr);
+        first = false;
     }
 
     if (! subjects.empty())
diff --git a/fish_tests.cpp b/fish_tests.cpp
index 8bbd23f74..89e77a4f2 100644
--- a/fish_tests.cpp
+++ b/fish_tests.cpp
@@ -922,6 +922,9 @@ static void test_test()
     /* These should be errors */
     assert(run_test_test(1, L"foo bar"));
     assert(run_test_test(1, L"foo bar baz"));
+    
+    /* This crashed */
+    assert(run_test_test(1, L"1 = 1 -a = 1"));
 }
 
 /** Testing colors */