# HG changeset patch # User Matthew Wild # Date 1534712193 -3600 # Node ID 7df29c5fbb9bf7445e408c3ec4e7f79615075819 # Parent ba38a947020e153e547b133f00f22a211223ae06 util.stanza + tests: Bail out of loop if we are iterating too far, fixes #981 diff -r ba38a947020e -r 7df29c5fbb9b spec/util_stanza_spec.lua --- a/spec/util_stanza_spec.lua Sun Aug 19 21:29:52 2018 +0100 +++ b/spec/util_stanza_spec.lua Sun Aug 19 21:56:33 2018 +0100 @@ -327,5 +327,12 @@ end); assert.equal(3, #s.tags); end); + it("errors on invalid data - #981", function () + local s = st.message({}, "Hello"); + s.tags[1] = st.clone(s.tags[1]); + assert.has_error_match(function () + s:maptags(function () end); + end, "Invalid stanza"); + end); end); end); diff -r ba38a947020e -r 7df29c5fbb9b util/stanza.lua --- a/util/stanza.lua Sun Aug 19 21:29:52 2018 +0100 +++ b/util/stanza.lua Sun Aug 19 21:56:33 2018 +0100 @@ -217,6 +217,7 @@ function stanza_mt:maptags(callback) local tags, curr_tag = self.tags, 1; local n_children, n_tags = #self, #tags; + local max_iterations = n_children + 1; local i = 1; while curr_tag <= n_tags and n_tags > 0 do @@ -236,6 +237,11 @@ curr_tag = curr_tag + 1; end i = i + 1; + if i > max_iterations then + -- COMPAT: Hopefully temporary guard against #981 while we + -- figure out the root cause + error("Invalid stanza state! Please report this error."); + end end return self; end