changeset 25366:2dad85fe6b8b

avoid warning from gcc 8 (bug #53872) * oct-sort.cc (roundupsize): Use size_t for computation and check that result fits in octave_idx_type.
author John W. Eaton <jwe@octave.org>
date Fri, 11 May 2018 11:27:01 -0400
parents e875f4719718
children c89fa0989e7b
files liboctave/util/oct-sort.cc
diffstat 1 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/util/oct-sort.cc	Thu May 10 19:08:10 2018 -0400
+++ b/liboctave/util/oct-sort.cc	Fri May 11 11:27:01 2018 -0400
@@ -111,6 +111,7 @@
 #include <cstring>
 #include <stack>
 
+#include "lo-error.h"
 #include "lo-mappers.h"
 #include "quit.h"
 #include "oct-sort.h"
@@ -493,10 +494,10 @@
 }
 
 static inline octave_idx_type
-roundupsize (octave_idx_type n)
+roundupsize (size_t n)
 {
   unsigned int nbits = 3;
-  octave_idx_type n2 = static_cast<octave_idx_type> (n) >> 8;
+  size_t n2 = n >> 8;
 
   /* Round up:
    * If n <       256, to a multiple of        8.
@@ -526,7 +527,13 @@
       nbits += 3;
     }
 
-  return ((n >> nbits) + 1) << nbits;
+  size_t new_size = ((n >> nbits) + 1) << nbits;
+
+  if (new_size == 0 || new_size > std::numeric_limits<octave_idx_type>::max ())
+    (*current_liboctave_error_handler)
+      ("unable to allocate sufficient memory for sort");
+
+  return static_cast<octave_idx_type> (new_size);
 }
 
 /* Ensure enough temp memory for 'need' array slots is available.