util.stanza: :maptags(): Fixes to make loop more robust on item removal
authorMatthew Wild <mwild1@gmail.com>
Wed, 3 Apr 2013 12:38:27 +0000 (13:38 +0100)
committerMatthew Wild <mwild1@gmail.com>
Wed, 3 Apr 2013 12:38:27 +0000 (13:38 +0100)
util/stanza.lua

index a0ab2a5aa0ae5d9be003fba34a2682931154ef81..213ed506df9113691521b7e4ac4244b82823c358 100644 (file)
@@ -153,7 +153,7 @@ function stanza_mt:maptags(callback)
        local n_children, n_tags = #self, #tags;
        
        local i = 1;
-       while curr_tag <= n_tags do
+       while curr_tag <= n_tags and n_tags > 0 do
                if self[i] == tags[curr_tag] then
                        local ret = callback(self[i]);
                        if ret == nil then
@@ -161,13 +161,15 @@ function stanza_mt:maptags(callback)
                                t_remove(tags, curr_tag);
                                n_children = n_children - 1;
                                n_tags = n_tags - 1;
+                               i = i - 1;
+                               curr_tag = curr_tag - 1;
                        else
                                self[i] = ret;
                                tags[i] = ret;
                        end
-                       i = i + 1;
                        curr_tag = curr_tag + 1;
                end
+               i = i + 1;
        end
        return self;
 end