changeset 26569:0e77df67b522 stable

Add static compile-time checking of printf functions in liboctave. * util/lo-error.h: Use macro OCTAVE_FORMAT_PRINTF for printf functions. * array/Array.h: Use format specifier "z" for type size_t. * array/Array-util.cc, numeric/oct-norm.cc: Supply a format string of "%s" when printing a single string. * array/idx-vector.cc, array/MatrixType.cc, array/Sparse.cc, numeric/DASPK.cc, numeric/DASRT.cc, numeric/DASSL.cc, numeric/LSODE.cc, util/lo-array-errwarn.cc, util/sparse-util.cc: Use preprocessor macro OCTAVE_IDX_TYPE_FORMAT as the format identifier for octave_idx_type. * util/f77-fcn.c: Cast width argument to int. * util/lo-utils.cc: Pass missing argument c0.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 17 Jan 2019 19:25:55 +0100
parents bf05a7c16e9e
children 1934f2bb3cb5
files liboctave/array/Array-util.cc liboctave/array/Array.h liboctave/array/MatrixType.cc liboctave/array/Sparse.cc liboctave/array/idx-vector.cc liboctave/numeric/DASPK.cc liboctave/numeric/DASRT.cc liboctave/numeric/DASSL.cc liboctave/numeric/LSODE.cc liboctave/numeric/oct-norm.cc liboctave/util/f77-fcn.c liboctave/util/lo-array-errwarn.cc liboctave/util/lo-error.h liboctave/util/lo-utils.cc liboctave/util/sparse-util.cc
diffstat 15 files changed, 76 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/array/Array-util.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/array/Array-util.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -565,7 +565,7 @@
           e.set_var ();
           std::string msg = e.message ();
           (*current_liboctave_error_with_id_handler)
-            (e.err_id (), msg.c_str ());
+            (e.err_id (), "%s", msg.c_str ());
         }
     }
   // idxa known to be valid.
--- a/liboctave/array/Array.h	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/array/Array.h	Thu Jan 17 19:25:55 2019 +0100
@@ -872,7 +872,7 @@
       std::string new_dims_str = dimensions.str ();
 
       (*current_liboctave_error_handler)
-        ("reshape: can't reshape %i elements into %s array",
+        ("reshape: can't reshape %zi elements into %s array",
          a.size (), new_dims_str.c_str ());
     }
 
--- a/liboctave/array/MatrixType.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/array/MatrixType.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -26,6 +26,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <vector>
 
 #include "MatrixType.h"
@@ -865,12 +866,14 @@
       else if (typ == MatrixType::Banded)
         (*current_liboctave_warning_with_id_handler)
           ("Octave:matrix-type-info",
-           "banded sparse matrix %d-1-%d (density %f)",
+           "banded sparse matrix %" OCTAVE_IDX_TYPE_FORMAT "-1-"
+           "%" OCTAVE_IDX_TYPE_FORMAT " (density %f)",
            lower_band, upper_band, bandden);
       else if (typ == MatrixType::Banded_Hermitian)
         (*current_liboctave_warning_with_id_handler)
           ("Octave:matrix-type-info",
-           "banded hermitian/symmetric sparse matrix %d-1-%d (density %f)",
+           "banded hermitian/symmetric sparse matrix %" OCTAVE_IDX_TYPE_FORMAT
+           "-1-%" OCTAVE_IDX_TYPE_FORMAT " (density %f)",
            lower_band, upper_band, bandden);
       else if (typ == MatrixType::Hermitian)
         (*current_liboctave_warning_with_id_handler)
--- a/liboctave/array/Sparse.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/array/Sparse.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -27,6 +27,7 @@
 // this file.
 
 #include <cassert>
+#include <cinttypes>
 
 #include <algorithm>
 #include <istream>
@@ -294,14 +295,16 @@
   if (nr < 0)
     nr = r.extent (0);
   else if (r.extent (nr) > nr)
-    (*current_liboctave_error_handler) ("sparse: row index %d out of bound %d",
-                                        r.extent (nr), nr);
+    (*current_liboctave_error_handler)
+      ("sparse: row index %" OCTAVE_IDX_TYPE_FORMAT "out of bound "
+       "%" OCTAVE_IDX_TYPE_FORMAT, r.extent (nr), nr);
 
   if (nc < 0)
     nc = c.extent (0);
   else if (c.extent (nc) > nc)
     (*current_liboctave_error_handler)
-      ("sparse: column index %d out of bound %d", r.extent (nc), nc);
+      ("sparse: column index %" OCTAVE_IDX_TYPE_FORMAT " out of bound "
+       "%" OCTAVE_IDX_TYPE_FORMAT, r.extent (nc), nc);
 
   dimensions = dim_vector (nr, nc);
 
@@ -735,14 +738,16 @@
 T
 Sparse<T>::range_error (const char *fcn, octave_idx_type n) const
 {
-  (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n);
+  (*current_liboctave_error_handler) ("%s (%" OCTAVE_IDX_TYPE_FORMAT "): "
+                                      "range error", fcn, n);
 }
 
 template <typename T>
 T&
 Sparse<T>::range_error (const char *fcn, octave_idx_type n)
 {
-  (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n);
+  (*current_liboctave_error_handler) ("%s (%" OCTAVE_IDX_TYPE_FORMAT "): "
+                                      "range error", fcn, n);
 }
 
 template <typename T>
@@ -750,14 +755,18 @@
 Sparse<T>::range_error (const char *fcn, octave_idx_type i,
                         octave_idx_type j) const
 {
-  (*current_liboctave_error_handler) ("%s (%d, %d): range error", fcn, i, j);
+  (*current_liboctave_error_handler)
+    ("%s (%" OCTAVE_IDX_TYPE_FORMAT ", %" OCTAVE_IDX_TYPE_FORMAT "): "
+     "range error", fcn, i, j);
 }
 
 template <typename T>
 T&
 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j)
 {
-  (*current_liboctave_error_handler) ("%s (%d, %d): range error", fcn, i, j);
+  (*current_liboctave_error_handler)
+    ("%s (%" OCTAVE_IDX_TYPE_FORMAT ", %" OCTAVE_IDX_TYPE_FORMAT "): "
+     "range error", fcn, i, j);
 }
 
 template <typename T>
@@ -781,7 +790,7 @@
 
   std::string buf_str = buf.str ();
 
-  (*current_liboctave_error_handler) (buf_str.c_str ());
+  (*current_liboctave_error_handler) ("%s", buf_str.c_str ());
 }
 
 template <typename T>
@@ -804,7 +813,7 @@
 
   std::string buf_str = buf.str ();
 
-  (*current_liboctave_error_handler) (buf_str.c_str ());
+  (*current_liboctave_error_handler) ("%s", buf_str.c_str ());
 }
 
 template <typename T>
@@ -2728,7 +2737,7 @@
               std::string err_field;
               is >> err_field;
               (*current_liboctave_error_handler)
-                ("invalid sparse matrix: element %d: "
+                ("invalid sparse matrix: element %" OCTAVE_IDX_TYPE_FORMAT ": "
                  "Symbols '%s' is not an integer format",
                  i+1, err_field.c_str ());
             }
@@ -2738,8 +2747,8 @@
               is.setstate (std::ios::failbit);
 
               (*current_liboctave_error_handler)
-                ("invalid sparse matrix: element %d: "
-                 "row index = %d out of range",
+                ("invalid sparse matrix: element %" OCTAVE_IDX_TYPE_FORMAT ": "
+                 "row index = %" OCTAVE_IDX_TYPE_FORMAT " out of range",
                  i+1, itmp + 1);
             }
 
@@ -2748,8 +2757,8 @@
               is.setstate (std::ios::failbit);
 
               (*current_liboctave_error_handler)
-                ("invalid sparse matrix: element %d: "
-                 "column index = %d out of range",
+                ("invalid sparse matrix: element %" OCTAVE_IDX_TYPE_FORMAT ": "
+                 "column index = %" OCTAVE_IDX_TYPE_FORMAT " out of range",
                  i+1, jtmp + 1);
             }
 
@@ -2758,8 +2767,9 @@
               is.setstate (std::ios::failbit);
 
               (*current_liboctave_error_handler)
-                ("invalid sparse matrix: element %d:"
-                 "column indices must appear in ascending order (%d < %d)",
+                ("invalid sparse matrix: element %" OCTAVE_IDX_TYPE_FORMAT ":"
+                 "column indices must appear in ascending order "
+                 "(%" OCTAVE_IDX_TYPE_FORMAT " < %" OCTAVE_IDX_TYPE_FORMAT ")",
                  i+1, jtmp, jold);
             }
           else if (jtmp > jold)
@@ -2772,9 +2782,9 @@
               is.setstate (std::ios::failbit);
 
               (*current_liboctave_error_handler)
-                ("invalid sparse matrix: element %d: "
+                ("invalid sparse matrix: element %" OCTAVE_IDX_TYPE_FORMAT ": "
                  "row indices must appear in ascending order in each column "
-                 "(%d < %d)",
+                 "(%" OCTAVE_IDX_TYPE_FORMAT " < %" OCTAVE_IDX_TYPE_FORMAT ")",
                  i+1, iold, itmp);
             }
 
--- a/liboctave/array/idx-vector.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/array/idx-vector.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -26,6 +26,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <cstdlib>
 
 #include <ostream>
@@ -1285,7 +1286,7 @@
   if (! resize_ok && extent (z_len) > z_len)
     {
       (*current_liboctave_error_handler)
-        ("invalid matrix index = %d", extent (z_len));
+        ("invalid matrix index = %" OCTAVE_IDX_TYPE_FORMAT, extent (z_len));
       // FIXME: Should we call this before calling error_handler?
       rep->err = true;
       chkerr ();
--- a/liboctave/numeric/DASPK.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/numeric/DASPK.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <sstream>
 
 #include "DASPK.h"
@@ -359,7 +360,8 @@
                     lid = 40 + n;
                   else
                     (*current_liboctave_error_handler)
-                      ("daspk: invalid value for eiq: %d", eiq);
+                      ("daspk: invalid value for eiq: "
+                       "%" OCTAVE_IDX_TYPE_FORMAT, eiq);
 
                   for (F77_INT i = 0; i < n; i++)
                     iwork(lid+i) = (av(i) ? -1 : 1);
@@ -404,7 +406,8 @@
                 lid = 40 + n;
               else
                 (*current_liboctave_error_handler)
-                  ("daspk: invalid value for eiq: %d", eiq);
+                  ("daspk: invalid value for eiq: %" OCTAVE_IDX_TYPE_FORMAT,
+                   eiq);
 
               for (F77_INT i = 0; i < n; i++)
                 iwork(lid+i) = (av(i) ? -1 : 1);
@@ -533,7 +536,8 @@
     default:
       integration_error = true;
       (*current_liboctave_error_handler)
-        ("unrecognized value of istate (= %d) returned from ddaspk", istate);
+        ("unrecognized value of istate (= %" OCTAVE_IDX_TYPE_FORMAT ") "
+         "returned from ddaspk", istate);
       break;
     }
 
--- a/liboctave/numeric/DASRT.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/numeric/DASRT.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <sstream>
 
 #include "DASRT.h"
@@ -374,8 +375,8 @@
     default:
       integration_error = true;
       (*current_liboctave_error_handler)
-        ("unrecognized value of istate (= %d) returned from ddasrt",
-         istate);
+        ("unrecognized value of istate (= %" OCTAVE_IDX_TYPE_FORMAT ") "
+         "returned from ddasrt", istate);
       break;
     }
 }
--- a/liboctave/numeric/DASSL.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/numeric/DASSL.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <sstream>
 
 #include "DASSL.h"
@@ -337,8 +338,8 @@
     default:
       integration_error = true;
       (*current_liboctave_error_handler)
-        ("unrecognized value of istate (= %d) returned from ddassl",
-         istate);
+        ("unrecognized value of istate (= %" OCTAVE_IDX_TYPE_FORMAT ") "
+         "returned from ddassl", istate);
       break;
     }
 
--- a/liboctave/numeric/LSODE.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/numeric/LSODE.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <sstream>
 
 #include "LSODE.h"
@@ -302,7 +303,8 @@
     default:
       integration_error = true;
       (*current_liboctave_error_handler)
-        ("unrecognized value of istate (= %d) returned from lsode", istate);
+        ("unrecognized value of istate (= %" OCTAVE_IDX_TYPE_FORMAT ") "
+         "returned from lsode", istate);
       break;
     }
 
--- a/liboctave/numeric/oct-norm.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/numeric/oct-norm.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -501,7 +501,7 @@
       res = higham (m, p, sqrteps, max_norm_iter, x);
     }
   else
-    (*current_liboctave_error_handler) (p_less1_gripe);
+    (*current_liboctave_error_handler) ("%s", p_less1_gripe);
 
   return res;
 }
@@ -522,7 +522,7 @@
       res = higham (m, p, sqrteps, max_norm_iter, x);
     }
   else
-    (*current_liboctave_error_handler) (p_less1_gripe);
+    (*current_liboctave_error_handler) ("%s", p_less1_gripe);
 
   return res;
 }
--- a/liboctave/util/f77-fcn.c	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/util/f77-fcn.c	Thu Jan 17 19:25:55 2019 +0100
@@ -53,7 +53,7 @@
       slen = strlen (s);
     }
 
-  (*current_liboctave_error_handler) ("%.*s", slen, s);
+  (*current_liboctave_error_handler) ("%.*s", (int) slen, s);
 
   F77_NORETURN (0)
 }
--- a/liboctave/util/lo-array-errwarn.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/util/lo-array-errwarn.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -24,6 +24,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <cmath>
 
 #include <sstream>
@@ -66,7 +67,8 @@
     const char *err_id = error_id_nonconformant_args;
 
     (*current_liboctave_error_with_id_handler)
-      (err_id, "%s: nonconformant arguments (op1 len: %d, op2 len: %d)",
+      (err_id, "%s: nonconformant arguments (op1 len: %" OCTAVE_IDX_TYPE_FORMAT
+       ", op2 len: % " OCTAVE_IDX_TYPE_FORMAT ")",
        op, op1_len, op2_len);
   }
 
@@ -78,7 +80,9 @@
     const char *err_id = error_id_nonconformant_args;
 
     (*current_liboctave_error_with_id_handler)
-      (err_id, "%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
+      (err_id, "%s: nonconformant arguments "
+       "(op1 is %" OCTAVE_IDX_TYPE_FORMAT "x%" OCTAVE_IDX_TYPE_FORMAT ", "
+       "op2 is %" OCTAVE_IDX_TYPE_FORMAT"x%" OCTAVE_IDX_TYPE_FORMAT ")",
        op, op1_nr, op1_nc, op2_nr, op2_nc);
   }
 
@@ -103,7 +107,8 @@
     const char *err_id = error_id_index_out_of_bounds;
 
     (*current_liboctave_error_with_id_handler)
-      (err_id, "A(%s) = []: index out of bounds: value %d out of bound %d",
+      (err_id, "A(%s) = []: index out of bounds: value %" OCTAVE_IDX_TYPE_FORMAT
+       " out of bound %" OCTAVE_IDX_TYPE_FORMAT,
        is1d ? "I" : "..,I,..", idx, ext);
   }
 
--- a/liboctave/util/lo-error.h	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/util/lo-error.h	Thu Jan 17 19:25:55 2019 +0100
@@ -53,14 +53,18 @@
 
 /* Would be nice to make these pointers private, but we want to share
    them among all the liboctave classes. */
+OCTAVE_FORMAT_PRINTF (1, 2)
 OCTAVE_NORETURN OCTAVE_API extern liboctave_error_handler
   current_liboctave_error_handler;
 
+OCTAVE_FORMAT_PRINTF (2, 3)
 OCTAVE_NORETURN OCTAVE_API extern liboctave_error_with_id_handler
   current_liboctave_error_with_id_handler;
 
+OCTAVE_FORMAT_PRINTF (1, 2)
 OCTAVE_API extern liboctave_warning_handler current_liboctave_warning_handler;
 
+OCTAVE_FORMAT_PRINTF (2, 3)
 OCTAVE_API extern liboctave_warning_with_id_handler
   current_liboctave_warning_with_id_handler;
 
--- a/liboctave/util/lo-utils.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/util/lo-utils.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -233,7 +233,8 @@
       break;
 
     default:
-      (*current_liboctave_error_handler) ("read_inf_nan_na: invalid character '%c'");
+      (*current_liboctave_error_handler)
+        ("read_inf_nan_na: invalid character '%c'", c0);
     }
 
   return val;
--- a/liboctave/util/sparse-util.cc	Thu Jan 17 09:25:35 2019 +0100
+++ b/liboctave/util/sparse-util.cc	Thu Jan 17 19:25:55 2019 +0100
@@ -25,6 +25,7 @@
 #  include "config.h"
 #endif
 
+#include <cinttypes>
 #include <cstdarg>
 #include <cstdio>
 
@@ -101,8 +102,9 @@
 
           if (c[j] > nnz)
             (*current_liboctave_error_handler)
-              ("invalid sparse matrix: cidx[%d] = %d "
-               "exceeds number of nonzero elements", j, c[j]+1);
+              ("invalid sparse matrix: cidx[%" OCTAVE_IDX_TYPE_FORMAT "] = "
+               "%" OCTAVE_IDX_TYPE_FORMAT "exceeds number of nonzero elements",
+               j, c[j]+1);
 
           if (c[j] != jold)
             {
@@ -115,7 +117,8 @@
 
                   if (r[i] >= nrows)
                     (*current_liboctave_error_handler)
-                      ("invalid sparse matrix: ridx[%d] = %d out of range",
+                      ("invalid sparse matrix: ridx[%" OCTAVE_IDX_TYPE_FORMAT
+                       "] = %" OCTAVE_IDX_TYPE_FORMAT " out of range",
                        i, r[i]+1);
                 }