# HG changeset patch # User Matthew Wild # Date 1779716660 -3600 # Node ID 67b68147ed1dea9622f26b1f01e75833f9560d54 # Parent 2bcabbeac9b074780527a4b49a8911cf90cd3111 util.ringbuffer: find(): Fix find logic bugs If the buffer was full, find() would return earlier (wpos == rpos). Checking blen is the better way to test if the buffer is empty. Secondly, the loop scanned for blen - l, but if l (length of needle) was larger than the buffer length, this calculation would underflow and cause a lot of spinning. New tests have been added to cover various edge cases of :find(). diff -r 2bcabbeac9b0 -r 67b68147ed1d spec/util_ringbuffer_spec.lua --- a/spec/util_ringbuffer_spec.lua Mon May 25 14:41:06 2026 +0100 +++ b/spec/util_ringbuffer_spec.lua Mon May 25 14:44:20 2026 +0100 @@ -49,6 +49,35 @@ assert.truthy(b:write("hello world")); test_find(b, "world", 7); end); + + it("works across wraps", function () + local b = rb.new(5); + assert.truthy(b:write("12345")); + assert.equal("123", b:read(3)); + assert.truthy(b:write("678")); + test_find(b, "45678", 1); + test_find(b, "567", 2); + test_find(b, "56", 2); + test_find(b, "5", 2); + test_find(b, "6", 3); + test_find(b, "7", 4); + test_find(b, "8", 5); + test_find(b, "9", nil); + end); + + it("no match when the needle is larger than the buffer", function () + for i = 5, 11 do + local b = rb.new(i); + assert.truthy(b:write("hello")); + test_find(b, "hello world", nil); + end + end); + + it("no match when the needle is an empty string", function () + local b = rb.new(5); + assert.truthy(b:write("hello")); + test_find(b, "", nil); + end); end); describe(":sub", function () diff -r 2bcabbeac9b0 -r 67b68147ed1d util-src/ringbuffer.c --- a/util-src/ringbuffer.c Mon May 25 14:41:06 2026 +0100 +++ b/util-src/ringbuffer.c Mon May 25 14:44:20 2026 +0100 @@ -82,7 +82,7 @@ size_t i, j; int m; - if(b->rpos == b->wpos) { /* empty */ + if(l > b->blen || l == 0) { /* needle is too large or too small */ return 0; }