changeset 31282:4707df477065

reshape: Show more specific error message if target size is large (bug #62870). * liboctave/array/Array.cc (Array): Catch exception to allow showing a more specific error message if the target size is larger than the maximum possible array size.
author John W. Eaton <jwe@octave.org>
date Sat, 06 Aug 2022 13:05:20 -0500
parents d6817b3308b1
children 5ec49cb45c50
files liboctave/array/Array.cc
diffstat 1 files changed, 19 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Array.cc	Sun Oct 09 15:43:50 2022 +0200
+++ b/liboctave/array/Array.cc	Sat Aug 06 13:05:20 2022 -0500
@@ -52,7 +52,25 @@
   : m_dimensions (dv), m_rep (a.m_rep),
     m_slice_data (a.m_slice_data), m_slice_len (a.m_slice_len)
 {
-  if (m_dimensions.safe_numel () != a.numel ())
+  bool invalid_size = false;
+
+  octave_idx_type new_numel = 0;
+
+  try
+    {
+      // Safe numel may throw a bad_alloc error because the new
+      // dimensions are too large to allocate.  Trap that here so
+      // we can issue a more helpful diagnostic that is consistent
+      // with other size mismatch failures.
+
+      new_numel = m_dimensions.safe_numel ();
+    }
+  catch (const std::bad_alloc&)
+    {
+      invalid_size = true;
+    }
+
+  if (invalid_size || new_numel != a.numel ())
     {
       std::string dimensions_str = a.m_dimensions.str ();
       std::string new_dims_str = m_dimensions.str ();