Functions: check number of input argmuents in macros 87/13187/4
Paul Bignier [Wed, 20 Nov 2013 16:14:13 +0000 (17:14 +0100)]
Many functions' macros did not check their input arguments number.

Change-Id: I993855019ec50d116cc75e7562738e2beb0a802b

14 files changed:
scilab/modules/functions/macros/add_profiling.sci
scilab/modules/functions/macros/bytecode.sci
scilab/modules/functions/macros/bytecodewalk.sci
scilab/modules/functions/macros/createfun.sci
scilab/modules/functions/macros/fun2string.sci
scilab/modules/functions/macros/genlib.sci
scilab/modules/functions/macros/head_comments.sci
scilab/modules/functions/macros/macrovar.sci
scilab/modules/functions/macros/plotprofile.sci
scilab/modules/functions/macros/profile.sci
scilab/modules/functions/macros/recompilefunction.sci
scilab/modules/functions/macros/remove_profiling.sci
scilab/modules/functions/macros/reset_profiling.sci
scilab/modules/functions/macros/showprofile.sci

index dcac0e4..328fedc 100644 (file)
@@ -8,6 +8,11 @@
 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 
 function add_profiling(funname)
+
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "addprofiling", 1));
+    end
+
     //add profiling instruction bytecode after each function line
     nsiz=6
     execstr("code=bytecode("+funname+")")
@@ -20,7 +25,9 @@ function add_profiling(funname)
     c1=bytecodewalk(c,15,addprof)
     code=[code(1:lc-2) int32(size(c1,"*")) c1]
     execstr(funname+" = resume(bytecode(code))")
+
 endfunction
+
 function [c,l]=addprof(l)
     //add profiling instruction bytecode
     c=int32([15 25 0 0]);l=l+1;
index 2ed382f..f919892 100644 (file)
 function x=bytecode(f)
     //returns the function f bytecode array in x
     //should be hard-coded
+
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "bytecode", 1));
+    end
+
     nsiz=6
     if type(f)==8 then //bytecode to function
         u=mopen(TMPDIR+"/%fun.bin","wb");
index 3b40afc..2a4da8b 100644 (file)
@@ -10,6 +10,9 @@
 function c1=bytecodewalk(code,query,job)
     //walk along bytecode looking for a particular opcode (query) and
     //applying a function (job) at this point
+    if argn(2) < 3 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "bytecodewalk", 3));
+    end
     c1=[];
     lc0=1;
     lc=lc0;
@@ -43,13 +46,13 @@ function c1=bytecodewalk(code,query,job)
                 lc=lc+1
                 long=double(code(lc))
                 lc=lc+1
-                cv=bytecodewalk(code(lc:lc+long-1))
+                cv=bytecodewalk(code(lc:lc+long-1),query,job)
                 lc=lc+long
                 long=double(code(lc))
                 lc=lc+1
                 id=code(lc:lc-1+nsiz)
                 lc=lc+nsiz
-                cc=bytecodewalk(code(lc:lc+long-1))
+                cc=bytecodewalk(code(lc:lc+long-1),query,job)
                 c1=[c1 int32(7) int32(size(cv,"*")) cv int32(size(cc,"*")) id cc]
                 lc=lc+long
                 lc0=lc
@@ -67,19 +70,19 @@ function c1=bytecodewalk(code,query,job)
 
                 long_case_expr=double(code(lc))
                 lc=lc+1
-                c_case_expr=bytecodewalk(code(lc:lc+long_case_expr-1))
+                c_case_expr=bytecodewalk(code(lc:lc+long_case_expr-1),query,job)
                 lc=lc+long_case_expr
                 long_case_expr=size(c_case_expr,"*")
                 ctemp=[ctemp int32(long_case_expr) c_case_expr]
                 for k=1:ncase+1
                     long_case_expr=double(code(lc));
                     lc=lc+1;
-                    c_case_expr=bytecodewalk(code(lc:lc+long_case_expr-1))
+                    c_case_expr=bytecodewalk(code(lc:lc+long_case_expr-1),query,job)
                     lc=lc+long_case_expr;
                     long_case_expr=size(c_case_expr,"*");
                     long_case_then=double(code(lc))
                     lc=lc+1
-                    c_case_then=bytecodewalk(code(lc:lc+long_case_then-1))
+                    c_case_then=bytecodewalk(code(lc:lc+long_case_then-1),query,job)
                     lc=lc+long_case_then
                     long_case_then=size(c_case_then,"*")
                     ctemp=[ctemp int32(long_case_expr) c_case_expr int32(long_case_then) ...
@@ -97,11 +100,11 @@ function c1=bytecodewalk(code,query,job)
                 long_try=double(code(lc+1))
                 long_catch=double(code(lc+2))
                 lc=lc+3
-                c_try=bytecodewalk(code(lc:lc+long_try-1))
+                c_try=bytecodewalk(code(lc:lc+long_try-1),query,job)
                 lc=lc+long_try
                 long_try=size(c_try,"*")
 
-                c_catch=bytecodewalk(code(lc:lc+long_catch-1))
+                c_catch=bytecodewalk(code(lc:lc+long_catch-1),query,job)
                 lc=lc+long_catch
                 long_catch=size(c_catch,"*")
                 c1=[c1 int32([11 long_try long_catch]) c_try c_catch]
@@ -174,12 +177,12 @@ function walkclause
     for k=1:ncase+1
         long_if_expr=double(code(lc))
         lc=lc+1
-        c_if_expr=bytecodewalk(code(lc:lc+long_if_expr-1))
+        c_if_expr=bytecodewalk(code(lc:lc+long_if_expr-1),query,job)
         lc=lc+long_if_expr
         long_if_expr=size(c_if_expr,"*")
         long_if_then=double(code(lc))
         lc=lc+1
-        c_if_then=bytecodewalk(code(lc:lc+long_if_then-1))
+        c_if_then=bytecodewalk(code(lc:lc+long_if_then-1),query,job)
         lc=lc+long_if_then
         long_if_then=size(c_if_then,"*")
         ctemp=[ctemp int32(long_if_expr) c_if_expr int32(long_if_then) ...
index 7f47d8b..8c7428d 100644 (file)
@@ -12,6 +12,9 @@ function %fun=createfun(t)
     //sequence and the following the function instructions, it returns et
     //Scilab function.
     // This is  mainly used by sci2exp
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "createfun", 1));
+    end
     h=t(1)
     k=strindex(h,"=")
     if k==[] then k1=1,else k1=k(1),end
index c66f93b..413c748 100644 (file)
@@ -16,7 +16,9 @@ function txt = fun2string(fun,nam)
     // nam   : nam of the scilab function to generate
     // txt   : character string column vector: the text of resulting scilab function
     //!
-    if argn(2)<2 then nam="ans",end
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"), "fun2string", 1, 2));
+    elseif argn(2)<2 then nam="ans",end
     if type(fun)==11 then comp(fun),end
     if type(fun)<>15 then
         lst=macr2lst(fun);
index 6e03d94..c291172 100644 (file)
@@ -13,6 +13,10 @@ function [success, funcs, success_files, failed_files] = genlib(nam, path, force
 
     // get all .sci files in the specified directory
 
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"), "genlib", 1, 5));
+    end
+
     if type(nam) <> 10 then
         error(999, msprintf(_("%s: Wrong type for input argument #%d: A string expected.\n"), "genlib", 1));
     end
index 4161919..e257b9f 100644 (file)
@@ -8,33 +8,37 @@
 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 
 function head_comments(name, %paths)
-  // Displays the first comments of a function
+    // Displays the first comments of a function
 
-  if type(name) <> 10 then
-     error(msprintf(gettext("%s: Wrong type for input argument #%d: A character string expected.\n"),"head_comments",1))
-  end
-  name = stripblanks(name)
-  if execstr('var='+name,'errcatch')<>0 then
-    error(msprintf(gettext("%s: Undefined variable %s.\n"),"head_comments",name )),
-  end
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"), "head_comments", 1, 2));
+    end
 
-  if type(var) == 11 then comp(var); end
-  if type(var) <> 13 then
-    error(msprintf(gettext("%s: Wrong value for input argument #%d: Name of a Scilab function expected.\n"),"head_comments",1))
-  end
-  ops = macr2lst(var);
-  syntax = 'function ['+strcat(ops(2),',')+'] = '+name+'('+strcat(ops(3),',')+')';
+    if type(name) <> 10 then
+        error(msprintf(gettext("%s: Wrong type for input argument #%d: A character string expected.\n"),"head_comments",1))
+    end
+    name = stripblanks(name)
+    if execstr("var="+name,"errcatch")<>0 then
+        error(msprintf(gettext("%s: Undefined variable %s.\n"),"head_comments",name )),
+    end
 
-  comm = [];
-  k = 4; if ops(4) == '15' then k = k+1; end
-  while ops(k)(1)=='31' then
-    comm = [comm; ops(k)(2)];
-    k = k+2;
-  end
-  if comm == [] then
-    mprintf("%s\n",[syntax;gettext("No comment available.")])
-  else
-    mprintf("%s\n",[syntax;comm])
-  end
+    if type(var) == 11 then comp(var); end
+    if type(var) <> 13 then
+        error(msprintf(gettext("%s: Wrong value for input argument #%d: Name of a Scilab function expected.\n"),"head_comments",1))
+    end
+    ops = macr2lst(var);
+    syntax = "function ["+strcat(ops(2),",")+"] = "+name+"("+strcat(ops(3),",")+")";
+
+    comm = [];
+    k = 4; if ops(4) == "15" then k = k+1; end
+    while ops(k)(1)=="31" then
+        comm = [comm; ops(k)(2)];
+        k = k+2;
+    end
+    if comm == [] then
+        mprintf("%s\n",[syntax;gettext("No comment available.")])
+    else
+        mprintf("%s\n",[syntax;comm])
+    end
 
 endfunction
index c11a3cd..f943bf5 100644 (file)
@@ -17,6 +17,9 @@ function vars=macrovar(macro)
     //           called : macros called
     //           locals : local variables
     //!
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "macrovar", 1));
+    end
     if type(macro)==11 then comp(macro),end
     if type(macro)<>13 then
         error(msprintf(gettext("%s: Wrong type for input argument #%d: Scilab function expected.\n"),"macrovar",1)),
index 541d0ff..c6cd3d0 100644 (file)
@@ -9,6 +9,10 @@
 
 function plotprofile(fun)
 
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "plotprofile", 1));
+    end
+
     sep="="
     sep=part(sep,ones(1,72))
 
index 3c9377e..1848ed7 100644 (file)
@@ -8,6 +8,9 @@
 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 
 function count=profile(fun)
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "profile", 1));
+    end
     if type(fun)==13 then
         lst=macr2lst(fun)
     else
index d2392b1..cb1ac40 100644 (file)
@@ -8,6 +8,9 @@
 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 
 function recompilefunction(funname,kind,force)
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d to %d expected.\n"), "recompilefunction", 1, 3));
+    end
     if ~exists("force","local") then force=%f; end
     if ~exists("kind","local") then kind="c"; end
     if ~exists(funname)
index c12cfb0..7a4b3f8 100644 (file)
@@ -9,6 +9,9 @@
 
 function remove_profiling(funname)
     //remove all profiling instruction bytecode
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "remove_profiling", 1));
+    end
     nsiz=6
     execstr("code=bytecode("+funname+")")
     lc=1
index 43d8e4a..98e4fd8 100644 (file)
@@ -9,6 +9,9 @@
 
 function reset_profiling(funname)
     //reset profiling data in bytecode
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "reset_profiling", 1));
+    end
     nsiz=6
     execstr("code=bytecode("+funname+")")
     lc=1
index 7d08cbb..69faa30 100644 (file)
@@ -9,6 +9,9 @@
 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 
 function showprofile(fun)
+    if argn(2) < 1 then
+        error(sprintf(_("%s: Wrong number of input argument(s): %d expected.\n"), "showprofile", 1));
+    end
     lst = macr2lst(fun)
     count = profile(lst)
     count(:,2) = round(100*count(:,2))/100