jenkins-bot has submitted this change and it was merged.

Change subject: Improve argument validation in frame:expandTemplate()
......................................................................


Improve argument validation in frame:expandTemplate()

Just like the other methods, e shouldn't be allowing passing of things
that aren't numbers or strings here.

For that matter, we should just abstract out the whole "arg key and
value validation" into a separate function instead of repeating it in
four places.

Bug: T76609
Change-Id: Id7e512a988ef9b7a5c5a110c8992dd5d649dcbf9
---
M engines/LuaCommon/lualib/mw.lua
1 file changed, 24 insertions(+), 38 deletions(-)

Approvals:
  Jackmcbarn: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/engines/LuaCommon/lualib/mw.lua b/engines/LuaCommon/lualib/mw.lua
index 06a670c..f6a2aee 100644
--- a/engines/LuaCommon/lualib/mw.lua
+++ b/engines/LuaCommon/lualib/mw.lua
@@ -231,6 +231,25 @@
                return newFrame( unpack( parentFrameIds ) )
        end
 
+       local function checkArgs( name, args )
+               local ret = {}
+               for k, v in pairs( args ) do
+                       local tp = type( k )
+                       if tp ~= 'string' and tp ~= 'number' then
+                               error( name .. ": arg keys must be strings or 
numbers, " .. tp .. " given", 3 )
+                       end
+                       tp = type( v )
+                       if tp == 'boolean' then
+                               ret[k] = v and '1' or ''
+                       elseif tp == 'string' or tp == 'number' then
+                               ret[k] = tostring( v )
+                       else
+                               error( name .. ": invalid type " .. tp .. " for 
arg '" .. k .. "'", 3 )
+                       end
+               end
+               return ret
+       end
+
        function frame:newChild( opt )
                checkSelf( self, 'newChild' )
 
@@ -249,21 +268,7 @@
                elseif type( opt.args ) ~= 'table' then
                        error( "frame:newChild: args must be a table", 2 )
                else
-                       args = {}
-                       for k, v in pairs( opt.args ) do
-                               local tp = type( k )
-                               if tp ~= 'string' and tp ~= 'number' then
-                                       error( "frame:newChild: arg keys must 
be strings or numbers, " .. tp .. " given", 2 )
-                               end
-                               local tp = type( v )
-                               if tp == 'boolean' then
-                                       args[k] = v and '1' or ''
-                               elseif tp == 'string' or tp == 'number' then
-                                       args[k] = tostring( v )
-                               else
-                                       error( "frame:newChild: invalid type " 
.. tp .. " for arg '" .. k .. "'", 2 )
-                               end
-                       end
+                       args = checkArgs( 'frame:newChild', opt.args )
                end
 
                local newFrameId = php.newChildFrame( frameId, title, args )
@@ -293,7 +298,7 @@
                elseif type( opt.args ) ~= 'table' then
                        error( "frame:expandTemplate: args must be a table" )
                else
-                       args = opt.args
+                       args = checkArgs( 'frame:expandTemplate', opt.args )
                end
 
                return php.expandTemplate( frameId, title, args )
@@ -319,16 +324,7 @@
                        error( "frame:callParserFunction: function name must be 
a string or number", 2 )
                end
 
-               for k, v in pairs( args ) do
-                       if type( k ) ~= 'string' and type( k ) ~= 'number' then
-                               error( "frame:callParserFunction: arg keys must 
be strings or numbers", 2 )
-                       end
-                       if type( v ) == 'number' then
-                               args[k] = tostring( v )
-                       elseif type( v ) ~= 'string' then
-                               error( "frame:callParserFunction: args must be 
strings or numbers", 2 )
-                       end
-               end
+               args = checkArgs( 'frame:callParserFunction', args )
 
                return php.callParserFunction( frameId, name, args )
        end
@@ -361,17 +357,7 @@
                elseif type( args ) == 'string' or type( args ) == 'number' then
                        args = { content, args }
                elseif type( args ) == 'table' then
-                       local tmp = args
-                       args = {}
-                       for k, v in pairs( tmp ) do
-                               if type( k ) ~= 'string' and type( k ) ~= 
'number' then
-                                       error( "frame:extensionTag: arg keys 
must be strings or numbers", 2 )
-                               end
-                               if type( v ) ~= 'string' and type( v ) ~= 
'number' then
-                                       error( "frame:extensionTag: arg values 
must be strings or numbers", 2 )
-                               end
-                               args[k] = v
-                       end
+                       args = checkArgs( 'frame:extensionTag', args )
                        table.insert( args, 1, content )
                else
                        error( "frame:extensionTag: args must be a string, 
number, or table", 2 )
@@ -423,7 +409,7 @@
                        function ()
                                return self:expandTemplate( opt )
                        end
-                       )
+               )
        end
 
        function frame:getTitle()

-- 
To view, visit https://gerrit.wikimedia.org/r/177564
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id7e512a988ef9b7a5c5a110c8992dd5d649dcbf9
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Anomie <bjor...@wikimedia.org>
Gerrit-Reviewer: Jackmcbarn <jackmcb...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to