changeset 19759:5f2c0ca0ef51

Ensure that numbers passed to integer *printf format codes are integers (bug #44245). * num2str.m, validateattributes.m, inputname.m, stemleaf.m, print.m, __ghostscript__.m, __gnuplot_print__.m, __go_draw_axes__.m, __go_draw_figure__.m, bicg.m, qmr.m, mat2str.m, validatestring.m, assert.m: Ensure that numbers passed to integer *printf format codes are integers.
author Rik <rik@octave.org>
date Mon, 16 Feb 2015 16:51:45 -0800
parents abc158e7122e
children 94cc1bdaf884
files scripts/general/num2str.m scripts/general/validateattributes.m scripts/miscellaneous/inputname.m scripts/plot/draw/stemleaf.m scripts/plot/util/print.m scripts/plot/util/private/__ghostscript__.m scripts/plot/util/private/__gnuplot_print__.m scripts/plot/util/private/__go_draw_axes__.m scripts/plot/util/private/__go_draw_figure__.m scripts/sparse/bicg.m scripts/sparse/qmr.m scripts/strings/mat2str.m scripts/strings/validatestring.m scripts/testfun/assert.m
diffstat 14 files changed, 34 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/general/num2str.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/general/num2str.m	Mon Feb 16 16:51:45 2015 -0800
@@ -83,7 +83,7 @@
     if (nargin == 2)
       if (ischar (arg))
         fmt = arg;
-      elseif (isnumeric (arg) && isscalar (arg) && arg >= 0)
+      elseif (isnumeric (arg) && isscalar (arg) && arg >= 0 && arg == fix (arg))
         fmt = sprintf ("%%%d.%dg", arg+7, arg);
       else
         error ("num2str: PRECISION must be a scalar integer >= 0");
@@ -122,7 +122,7 @@
     if (nargin == 2)
       if (ischar (arg))
         fmt = [arg "%-+" arg(2:end) "i"];
-      elseif (isnumeric (arg) && isscalar (arg) && arg >= 0)
+      elseif (isnumeric (arg) && isscalar (arg) && arg >= 0 && arg == fix (arg))
         fmt = sprintf ("%%%d.%dg%%-+%d.%dgi", arg+7, arg, arg+7, arg);
       else
         error ("num2str: PRECISION must be a scalar integer >= 0");
--- a/scripts/general/validateattributes.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/general/validateattributes.m	Mon Feb 16 16:51:45 2015 -0800
@@ -327,7 +327,7 @@
 endfunction
 
 function retval = valid_arg_idx (arg)
-  retval = isnumeric (arg) && isscalar (arg) && arg > 0 && ceil (arg) == arg;
+  retval = isnumeric (arg) && isscalar (arg) && arg > 0 && arg == fix (arg);
 endfunction
 
 function cls = replace_cl_group (cls, name, group)
--- a/scripts/miscellaneous/inputname.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/miscellaneous/inputname.m	Mon Feb 16 16:51:45 2015 -0800
@@ -35,7 +35,7 @@
     print_usage ();
   endif
 
-  s = evalin ("caller", sprintf ("__varval__ (\".argn.\"){%d};", n));
+  s = evalin ("caller", sprintf ("__varval__ (\".argn.\"){%d};", fix (n)));
   ## For compatibility with Matlab,
   ## return empty string if argument name is not a valid identifier.
   if (! isvarname (s))
--- a/scripts/plot/draw/stemleaf.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/plot/draw/stemleaf.m	Mon Feb 16 16:51:45 2015 -0800
@@ -185,7 +185,7 @@
   hl = xs(hlidx);               # lower hinge
   hu = xs(huidx);               # upper hinge
   h_spread = hu - hl;           # h_spread: difference between hinges
-  step = fix(1.5*h_spread);     # step: 1.5 * h_spread
+  step = fix (1.5*h_spread);    # step: 1.5 * h_spread
   i_fence_l = hl - step;        # inner fences: outside hinges + step
   o_fence_l = hl - 2*step;      # outer fences: outside hinges + 2*step
   i_fence_h = hu + step;
--- a/scripts/plot/util/print.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/plot/util/print.m	Mon Feb 16 16:51:45 2015 -0800
@@ -542,7 +542,7 @@
                    opts.preview);
         endswitch
         if (! isempty (opts.ghostscript.resolution))
-          cmd = sprintf ("%s --dpi %d", cmd, opts.ghostscript.resolution);
+          cmd = sprintf ("%s --dpi %d", cmd, fix (opts.ghostscript.resolution));
         endif
       else
         cmd = "";
@@ -661,7 +661,7 @@
       graphicsfile = strcat (opts.name, "-inc.eps");
   endswitch
   papersize = sprintf ("\\usepackage[papersize={%.2fbp,%.2fbp},text={%.2fbp,%.2fbp}]{geometry}",
-                       opts.canvas_size, opts.canvas_size);
+                       fix (opts.canvas_size), fix (opts.canvas_size));
   prepend = {"\\documentclass{minimal}";
              packages;
              papersize;
--- a/scripts/plot/util/private/__ghostscript__.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/plot/util/private/__ghostscript__.m	Mon Feb 16 16:51:45 2015 -0800
@@ -69,15 +69,15 @@
   gs_opts = sprintf ("-dQUIET -dNOPAUSE -dBATCH -dSAFER -sDEVICE=%s", opts.device);
 
   if (! isempty (opts.level) && ismember (opts.level, [1, 2, 3]))
-    gs_opts = sprintf ("%s -dLanguageLevel=%d", gs_opts, round (opts.level));
+    gs_opts = sprintf ("%s -dLanguageLevel=%d", gs_opts, opts.level);
   endif
 
   if (opts.antialiasing && isempty (strfind (opts.device, "write")))
     ## Apply anti-aliasing to all bitmap formats/devices
     gs_opts = sprintf ("%s -dTextAlphaBits=%d -dGraphicsAlphaBits=%d",
-                       gs_opts, opts.antialiasing_textalphabits,
-                       opts.antialiasing_graphicsalphabits);
-    gs_opts = sprintf ("%s -r%dx%d", gs_opts, [1, 1] * opts.resolution);
+                       gs_opts, fix (opts.antialiasing_textalphabits),
+                       fix (opts.antialiasing_graphicsalphabits));
+    gs_opts = sprintf ("%s -r%dx%d", gs_opts, fix ([1, 1] * opts.resolution));
   elseif (any (strcmp (opts.device, {"pswrite", "ps2write", "pdfwrite"})))
     gs_opts = sprintf ("%s -dEmbedAllFonts=true", gs_opts);
     if (strcmp (opts.device, "pdfwrite"))
@@ -95,7 +95,7 @@
       gs_opts = sprintf ("%s -sPAPERSIZE=%s", gs_opts, opts.papersize);
     elseif (isnumeric (opts.papersize) && numel (opts.papersize) == 2)
       gs_opts = sprintf ("%s -dDEVICEWIDTHPOINTS=%d -dDEVICEHEIGHTPOINTS=%d",
-                         gs_opts, opts.papersize);
+                         gs_opts, fix (opts.papersize));
       if (opts.papersize(1) > opts.papersize(2))
         ## Lanscape mode: This option will result in automatic rotation of the
         ##                document page if the requested page size matches one
@@ -108,7 +108,7 @@
     gs_opts = sprintf ("%s -dFIXEDMEDIA", gs_opts);
     ## "pageoffset" is relative to the coordinates, not the BBox LLHC.
     str = sprintf ("%s [%d %d] %s", "<< /Margins [0 0] /.HWMargins [0 0 0 0] /PageOffset",
-                   opts.pageoffset, ">> setpagedevice");
+                   fix (opts.pageoffset), ">> setpagedevice");
     offset_ps = {"%!PS-Adobe-3.0", str, "%%EOF"};
     if (isfield (opts, "offsetfile"))
       offsetfile = opts.offsetfile;
--- a/scripts/plot/util/private/__gnuplot_print__.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/plot/util/private/__gnuplot_print__.m	Mon Feb 16 16:51:45 2015 -0800
@@ -220,6 +220,8 @@
         f = sprintf ("%d", 2 * opts.fontsize);
       endif
     case "svg"
+      ## FIXME: Why does svg format use round on the fontsize while
+      ##        other terminals don't?
       if (! isempty (opts.font) && ! isempty (opts.fontsize))
         fontsize = round (opts.fontsize * 0.75);
         f = sprintf ('fname "%s" fsize %d', opts.font, fontsize);
--- a/scripts/plot/util/private/__go_draw_axes__.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/plot/util/private/__go_draw_axes__.m	Mon Feb 16 16:51:45 2015 -0800
@@ -617,7 +617,7 @@
           usingclause{data_idx} = usingclause{data_idx - 1};
           data{data_idx} = data{data_idx - 1};
           withclause{data_idx} = sprintf ("with %s linestyle %d",
-                                        style{2}, data_idx);
+                                          style{2}, data_idx);
         endif
         if (length (style) > 2)
           data_idx++;
@@ -629,7 +629,7 @@
           usingclause{data_idx} = usingclause{data_idx - 1};
           data{data_idx} = data{data_idx - 1};
           withclause{data_idx} = sprintf ("with %s linestyle %d",
-                                        style{3}, data_idx);
+                                          style{3}, data_idx);
         endif
 
      case "patch"
@@ -1782,6 +1782,8 @@
   #
   # This means that if an alternative size is used, gnuplot will use an
   # inappropriate spacing in the legend by default.
+  ## FIXME: Are fractional spacing specifications allowed?  Or should this
+  ##        number be rounded?
   spc = s / 10 * 1.25;
   spacing_spec = sprintf ("spacing %d", spc);
 
--- a/scripts/plot/util/private/__go_draw_figure__.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/plot/util/private/__go_draw_figure__.m	Mon Feb 16 16:51:45 2015 -0800
@@ -38,7 +38,7 @@
       fputs (plot_stream, "set size 1, 1\n");
       bg = get (h, "color");
       if (isnumeric (bg))
-        fprintf (plot_stream, "set obj 1 rectangle from screen 0,0 to screen 1,1 behind fc rgb \"#%02x%02x%02x\"\n", 255 * bg);
+        fprintf (plot_stream, "set obj 1 rectangle from screen 0,0 to screen 1,1 behind fc rgb \"#%02x%02x%02x\"\n", round (255 * bg));
         bg_is_set = true;
       else
         bg_is_set = false;
@@ -114,7 +114,7 @@
                     endif
                   endfor
                   if (bg_is_set)
-                    fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", 255 * (1 - bg));
+                    fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", round (255 * (1 - bg)));
                   endif
                   __go_draw_axes__ (kids(i), plot_stream, enhanced, mono,
                                     bg_is_set, false, hlgnd);
@@ -140,7 +140,7 @@
                 set (kids(i), "units", "normalized");
                 fg = get (kids(i), "color");
                 if (isnumeric (fg) && strcmp (get (kids(i), "visible"), "on"))
-                  fprintf (plot_stream, "set obj 2 rectangle from graph 0,0 to graph 1,1 behind fc rgb \"#%02x%02x%02x\"\n", 255 * fg);
+                  fprintf (plot_stream, "set obj 2 rectangle from graph 0,0 to graph 1,1 behind fc rgb \"#%02x%02x%02x\"\n", round (255 * fg));
                   fg_is_set = true;
                   fg_was_set = true;
                 elseif (fg_was_set)
@@ -151,7 +151,7 @@
                   fg_is_set = false;
                 endif
                 if (bg_is_set)
-                  fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", 255 * (1 - bg));
+                  fprintf (plot_stream, "set border linecolor rgb \"#%02x%02x%02x\"\n", round (255 * (1 - bg)));
                 endif
                 ## Find if this axes has an associated legend axes and pass it
                 ## to __go_draw_axes__
--- a/scripts/sparse/bicg.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/sparse/bicg.m	Mon Feb 16 16:51:45 2015 -0800
@@ -98,6 +98,8 @@
 
     if (nargin < 4 || isempty (maxit))
       maxit = min (rows (b), 20);
+    else
+      maxit = fix (maxit);
     endif
 
     if (nargin < 5 || isempty (M1))
--- a/scripts/sparse/qmr.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/sparse/qmr.m	Mon Feb 16 16:51:45 2015 -0800
@@ -112,6 +112,8 @@
 
     if (nargin < 4 || isempty (maxit))
       maxit = min (rows (b), 20);
+    else
+      maxit = fix (maxit);
     endif
 
     if (nargin < 5 || isempty (M1))
--- a/scripts/strings/mat2str.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/strings/mat2str.m	Mon Feb 16 16:51:45 2015 -0800
@@ -70,6 +70,10 @@
     n = 15;
   elseif (isempty (n))
     n = 15;   # Default precision
+  elseif (numel (n) > 2)
+    error ("mat2str: N must have only 1 or 2 elements");
+  else
+    n = fix (n);
   endif
 
   x_islogical = islogical (x);
@@ -144,4 +148,5 @@
 %!error mat2str (1,2,3,4)
 %!error mat2str (["Hello"])
 %!error <X must be two dimensional> mat2str (ones (3,3,2))
+%!error <N must have only 1 or 2 elements> mat2str (ones (3,3), [1 2 3])
 
--- a/scripts/strings/validatestring.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/strings/validatestring.m	Mon Feb 16 16:51:45 2015 -0800
@@ -67,7 +67,7 @@
   position = 0;
   ## Process input arguments
   if (! isempty (varargin) && isnumeric (varargin{end}))
-    position = varargin{end};
+    position = fix (varargin{end});
     varargin(end) = [];
   endif
 
--- a/scripts/testfun/assert.m	Mon Feb 16 14:29:43 2015 -0800
+++ b/scripts/testfun/assert.m	Mon Feb 16 16:51:45 2015 -0800
@@ -641,7 +641,7 @@
 
   cout = cell (numel (erridx), 1);
   tmp = cell (1, numel (matsize));
-  [tmp{:}] = ind2sub (matsize, erridx (:));
+  [tmp{:}] = ind2sub (matsize, erridx(:));
   subs = [tmp{:}];
   if (numel (matsize) == 2)
     subs = subs(:, matsize != 1);