Refactor the array validation routines into their own file, as they are used by
more than just the residual block evaluation routines now.
Check the step computed by the linear solver for finiteness, and terminate early
if it is not. This will prevent bugs where the solver continues to run after
encountering an nan.
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt
index e6dcbd9..f7caa6c 100644
--- a/internal/ceres/CMakeLists.txt
+++ b/internal/ceres/CMakeLists.txt
@@ -29,6 +29,7 @@
 # Author: keir@google.com (Keir Mierle)
 
 SET(CERES_INTERNAL_SRC
+    array_utils.cc
     block_evaluate_preparer.cc
     block_jacobi_preconditioner.cc
     block_jacobian_writer.cc
@@ -153,6 +154,7 @@
              ${CMAKE_SOURCE_DIR}/data)
   ENDMACRO (CERES_TEST)
 
+  CERES_TEST(array_utils)
   CERES_TEST(autodiff)
   CERES_TEST(autodiff_cost_function)
   CERES_TEST(block_random_access_dense_matrix)
diff --git a/internal/ceres/array_utils.cc b/internal/ceres/array_utils.cc
new file mode 100644
index 0000000..aab0c21
--- /dev/null
+++ b/internal/ceres/array_utils.cc
@@ -0,0 +1,66 @@
+// Ceres Solver - A fast non-linear least squares minimizer
+// Copyright 2012 Google Inc. All rights reserved.
+// http://code.google.com/p/ceres-solver/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright notice,
+//   this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright notice,
+//   this list of conditions and the following disclaimer in the documentation
+//   and/or other materials provided with the distribution.
+// * Neither the name of Google Inc. nor the names of its contributors may be
+//   used to endorse or promote products derived from this software without
+//   specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+//
+// Author: sameeragarwal@google.com (Sameer Agarwal)
+
+#include "ceres/array_utils.h"
+
+#include <cmath>
+#include <cstddef>
+
+namespace ceres {
+namespace internal {
+
+// It is a near impossibility that user code generates this exact
+// value in normal operation, thus we will use it to fill arrays
+// before passing them to user code. If on return an element of the
+// array still contains this value, we will assume that the user code
+// did not write to that memory location.
+const double kImpossibleValue = 1e302;
+
+bool IsArrayValid(const int size, const double* x) {
+  if (x != NULL) {
+    for (int i = 0; i < size; ++i) {
+      if (!isfinite(x[i]) || (x[i] == kImpossibleValue))  {
+        return false;
+      }
+    }
+  }
+  return true;
+}
+
+void InvalidateArray(const int size, double* x) {
+  if (x != NULL) {
+    for (int i = 0; i < size; ++i) {
+      x[i] = kImpossibleValue;
+    }
+  }
+}
+
+}  // namespace internal
+}  // namespace ceres
diff --git a/internal/ceres/array_utils.h b/internal/ceres/array_utils.h
new file mode 100644
index 0000000..99cc8d8
--- /dev/null
+++ b/internal/ceres/array_utils.h
@@ -0,0 +1,65 @@
+// Ceres Solver - A fast non-linear least squares minimizer
+// Copyright 2012 Google Inc. All rights reserved.
+// http://code.google.com/p/ceres-solver/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright notice,
+//   this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright notice,
+//   this list of conditions and the following disclaimer in the documentation
+//   and/or other materials provided with the distribution.
+// * Neither the name of Google Inc. nor the names of its contributors may be
+//   used to endorse or promote products derived from this software without
+//   specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+//
+// Author: sameeragarwal@google.com (Sameer Agarwal)
+//
+// Utility routines for validating arrays. 
+//
+// These are useful for detecting two common class of errors.
+//
+// 1. Uninitialized memory - where the user for some reason did not
+// compute part of an array, but the code expects it.
+//
+// 2. Numerical failure while computing the cost/residual/jacobian,
+// e.g. NaN, infinities etc. This is particularly useful since the
+// automatic differentiation code does computations that are not
+// evident to the user and can silently generate hard to debug errors.
+
+#ifndef CERES_INTERNAL_ARRAY_UTILS_H_
+#define CERES_INTERNAL_ARRAY_UTILS_H_
+
+#include "ceres/internal/port.h"
+
+namespace ceres {
+namespace internal {
+
+// Fill the array x with an impossible value that the user code is
+// never expected to compute.
+void InvalidateArray(int size, double* x);
+
+// Check if all the entries of the array x are valid, i.e. all the
+// values in the array should be finite and none of them should be
+// equal to the "impossible" value used by InvalidateArray.
+bool IsArrayValid(int size, const double* x);
+
+extern const double kImpossibleValue;
+
+}  // namespace internal
+}  // namespace ceres
+
+#endif  // CERES_INTERNAL_ARRAY_UTILS_H_
diff --git a/internal/ceres/array_utils_test.cc b/internal/ceres/array_utils_test.cc
new file mode 100644
index 0000000..c19a44a
--- /dev/null
+++ b/internal/ceres/array_utils_test.cc
@@ -0,0 +1,58 @@
+// Ceres Solver - A fast non-linear least squares minimizer
+// Copyright 2012 Google Inc. All rights reserved.
+// http://code.google.com/p/ceres-solver/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// * Redistributions of source code must retain the above copyright notice,
+//   this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above copyright notice,
+//   this list of conditions and the following disclaimer in the documentation
+//   and/or other materials provided with the distribution.
+// * Neither the name of Google Inc. nor the names of its contributors may be
+//   used to endorse or promote products derived from this software without
+//   specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+//
+// Author: sameeragarwal@google.com (Sameer Agarwal)
+
+#include "ceres/array_utils.h"
+
+#include <limits>
+#include <cmath>
+#include "gtest/gtest.h"
+
+namespace ceres {
+namespace internal {
+
+TEST(ArrayUtils, IsArrayValid) {
+  double x[3];
+  x[0] = 0.0;
+  x[1] = 1.0;
+  x[2] = 2.0;
+  EXPECT_TRUE(IsArrayValid(3, x));
+  x[1] = std::numeric_limits<double>::infinity();
+  EXPECT_FALSE(IsArrayValid(3, x));
+  x[1] = std::numeric_limits<double>::quiet_NaN();
+  EXPECT_FALSE(IsArrayValid(3, x));
+  x[1] = std::numeric_limits<double>::signaling_NaN();
+  EXPECT_FALSE(IsArrayValid(3, x));
+  EXPECT_TRUE(IsArrayValid(1, NULL));
+  InvalidateArray(3, x);
+  EXPECT_FALSE(IsArrayValid(3, x));
+}
+
+}  // namespace internal
+}  // namespace ceres
diff --git a/internal/ceres/levenberg_marquardt.cc b/internal/ceres/levenberg_marquardt.cc
index 4ada794..8b3d9da 100644
--- a/internal/ceres/levenberg_marquardt.cc
+++ b/internal/ceres/levenberg_marquardt.cc
@@ -56,6 +56,7 @@
 
 #include <glog/logging.h>
 #include "Eigen/Core"
+#include "ceres/array_utils.h"
 #include "ceres/evaluator.h"
 #include "ceres/file.h"
 #include "ceres/linear_least_squares_problems.h"
@@ -310,6 +311,11 @@
       // Truncated Newton methods.
       solve_options.r_tolerance = -1.0;
 
+      // Invalidate the output array lm_step, so that we can detect if
+      // the linear solver generated numerical garbage.  This is known
+      // to happen for the DENSE_QR and then DENSE_SCHUR solver when
+      // the Jacobin is severly rank deficient mu is too small.
+      InvalidateArray(num_effective_parameters, lm_step.data());
       const time_t linear_solver_start_time = time(NULL);
       LinearSolver::Summary linear_solver_summary =
           linear_solver->Solve(jacobian.get(),
@@ -347,6 +353,14 @@
         break;
       }
 
+      if (!IsArrayValid(num_effective_parameters, lm_step.data())) {
+        LOG(WARNING) << "Linear solver failure. Failed to compute a finite "
+                     << "step. Terminating. Please report this to the Ceres "
+                     << "Solver developers.";
+        summary->termination_type = NUMERICAL_FAILURE;
+        return;
+      }
+
       step_norm = (lm_step.array() * scale.array()).matrix().norm();
 
       // Check step length based convergence. If the step length is
diff --git a/internal/ceres/parameter_block.h b/internal/ceres/parameter_block.h
index 4bac1a8..cedb30c 100644
--- a/internal/ceres/parameter_block.h
+++ b/internal/ceres/parameter_block.h
@@ -32,13 +32,13 @@
 #define CERES_INTERNAL_PARAMETER_BLOCK_H_
 
 #include <cstdlib>
-#include "ceres/integral_types.h"
 #include <glog/logging.h>
+#include "ceres/array_utils.h"
+#include "ceres/integral_types.h"
 #include "ceres/internal/eigen.h"
 #include "ceres/internal/port.h"
 #include "ceres/internal/scoped_ptr.h"
 #include "ceres/local_parameterization.h"
-#include "ceres/residual_block_utils.h"
 
 namespace ceres {
 namespace internal {
diff --git a/internal/ceres/residual_block_utils.cc b/internal/ceres/residual_block_utils.cc
index ed3499b..395f469 100644
--- a/internal/ceres/residual_block_utils.cc
+++ b/internal/ceres/residual_block_utils.cc
@@ -34,6 +34,7 @@
 #include <cstddef>
 #include <limits>
 #include <glog/logging.h>
+#include "ceres/array_utils.h"
 #include "ceres/residual_block.h"
 #include "ceres/parameter_block.h"
 #include "ceres/stringprintf.h"
@@ -43,32 +44,6 @@
 namespace ceres {
 namespace internal {
 
-// It is a near impossibility that user code generates this exact
-// value in normal operation, thus we will use it to fill arrays
-// before passing them to user code. If on return an element of the
-// array still contains this value, we will assume that the user code
-// did not write to that memory location.
-static const double kImpossibleValue = 1e302;
-
-bool IsArrayValid(const int size, const double* x) {
-  if (x != NULL) {
-    for (int i = 0; i < size; ++i) {
-      if (!isfinite(x[i]) || (x[i] == kImpossibleValue))  {
-        return false;
-      }
-    }
-  }
-  return true;
-}
-
-void InvalidateArray(const int size, double* x) {
-  if (x != NULL) {
-    for (int i = 0; i < size; ++i) {
-      x[i] = kImpossibleValue;
-    }
-  }
-}
-
 void InvalidateEvaluation(const ResidualBlock& block,
                           double* cost,
                           double* residuals,
diff --git a/internal/ceres/residual_block_utils.h b/internal/ceres/residual_block_utils.h
index 228867c..7051c21 100644
--- a/internal/ceres/residual_block_utils.h
+++ b/internal/ceres/residual_block_utils.h
@@ -51,15 +51,6 @@
 
 class ResidualBlock;
 
-// Fill the array x with an impossible value that the user code is
-// never expected to compute.
-void InvalidateArray(int size, double* x);
-
-// Check if all the entries of the array x are valid, i.e. all the
-// values in the array should be finite and none of them should be
-// equal to the "impossible" value used by InvalidateArray.
-bool IsArrayValid(int size, const double* x);
-
 // Invalidate cost, resdual and jacobian arrays (if not NULL).
 void InvalidateEvaluation(const ResidualBlock& block,
                           double* cost,
diff --git a/internal/ceres/residual_block_utils_test.cc b/internal/ceres/residual_block_utils_test.cc
index 33a017e..db9ad6d 100644
--- a/internal/ceres/residual_block_utils_test.cc
+++ b/internal/ceres/residual_block_utils_test.cc
@@ -29,7 +29,7 @@
 // Author: sameeragarwal@google.com (Sameer Agarwal)
 
 #include <cmath>
-
+#include <limits>
 #include "gtest/gtest.h"
 #include "ceres/parameter_block.h"
 #include "ceres/residual_block.h"
@@ -117,7 +117,7 @@
   virtual bool Evaluate(double const* const* parameters,
                         double* residuals,
                         double** jacobians) const {
-    residuals[0] = 1.0/0.0;
+    residuals[0] = std::numeric_limits<double>::infinity();
     if (jacobians != NULL && jacobians[0] != NULL) {
       jacobians[0][0] = 0.0;
     }
@@ -132,27 +132,12 @@
                         double** jacobians) const {
     residuals[0] = 1.0;
     if (jacobians != NULL && jacobians[0] != NULL) {
-      jacobians[0][0] = 1.0/0.0;
+      jacobians[0][0] = std::numeric_limits<double>::quiet_NaN();
     }
     return true;
   }
 };
 
-TEST(ResidualBlockUtils, IsArrayValid) {
-  double x[3];
-  x[0] = 0.0;
-  x[1] = 1.0;
-  x[2] = 2.0;
-  EXPECT_TRUE(IsArrayValid(3, x));
-  x[1] = 1.0/0.0;
-  EXPECT_FALSE(IsArrayValid(3, x));
-  x[1] = 0.0/0.0;
-  EXPECT_FALSE(IsArrayValid(3, x));
-  EXPECT_TRUE(IsArrayValid(1, NULL));
-  InvalidateArray(3, x);
-  EXPECT_FALSE(IsArrayValid(3, x));
-}
-
 // Note: It is preferable to write the below test as:
 //
 //  CheckEvaluation(GoodCostFunction(), true);