Comparison

util/stanza.lua @ 9217:7df29c5fbb9b

util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981
author Matthew Wild <mwild1@gmail.com>
date Sun, 19 Aug 2018 21:56:33 +0100
parent 8999:a2a4c225a3f8
child 9307:feaef6215bb8
comparison
equal deleted inserted replaced
9216:ba38a947020e 9217:7df29c5fbb9b
215 end 215 end
216 216
217 function stanza_mt:maptags(callback) 217 function stanza_mt:maptags(callback)
218 local tags, curr_tag = self.tags, 1; 218 local tags, curr_tag = self.tags, 1;
219 local n_children, n_tags = #self, #tags; 219 local n_children, n_tags = #self, #tags;
220 local max_iterations = n_children + 1;
220 221
221 local i = 1; 222 local i = 1;
222 while curr_tag <= n_tags and n_tags > 0 do 223 while curr_tag <= n_tags and n_tags > 0 do
223 if self[i] == tags[curr_tag] then 224 if self[i] == tags[curr_tag] then
224 local ret = callback(self[i]); 225 local ret = callback(self[i]);
234 tags[curr_tag] = ret; 235 tags[curr_tag] = ret;
235 end 236 end
236 curr_tag = curr_tag + 1; 237 curr_tag = curr_tag + 1;
237 end 238 end
238 i = i + 1; 239 i = i + 1;
240 if i > max_iterations then
241 -- COMPAT: Hopefully temporary guard against #981 while we
242 -- figure out the root cause
243 error("Invalid stanza state! Please report this error.");
244 end
239 end 245 end
240 return self; 246 return self;
241 end 247 end
242 248
243 function stanza_mt:find(path) 249 function stanza_mt:find(path)