Skip to content
Snippets Groups Projects
Commit 8d9652d9 authored by Oleg Babin's avatar Oleg Babin Committed by Kirill Yukhin
Browse files

lua: handle uri.format empty input properly

After 7fd6c809
(buffer: port static allocator to Lua) uri started to use
static_allocator - cyclic buffer that also is used in
several modules.

However situation when uri.format output is zero-length
string was not handled properly and ffi.string could
return data that was previously written in static buffer
because use as string terminator the first zero byte.

To prevent such situation let's pass result length explicitly.

Closes #4779

(cherry picked from commit 57f6fc93)
parent 9e403e42
No related branches found
No related tags found
No related merge requests found
......@@ -77,8 +77,8 @@ local function format(uri, write_password)
uribuf.fragment = uri.fragment
uribuf.fragment_len = string.len(uri.fragment or '')
local str = static_alloc('char', 1024)
builtin.uri_format(str, 1024, uribuf, write_password and 1 or 0)
return ffi.string(str)
local len = builtin.uri_format(str, 1024, uribuf, write_password and 1 or 0)
return ffi.string(str, len)
end
return {
......
......@@ -2,6 +2,8 @@
local tap = require('tap')
local uri = require('uri')
local ffi = require('ffi')
local static_alloc = require('buffer').static_alloc
local function test_parse(test)
-- Tests for uri.parse() Lua bindings.
......@@ -64,8 +66,28 @@ local function test_format(test)
test:is(uri.format(u, true), "user:password@localhost", "password kept")
end
local function test_static_alloc(test)
-- gh-4779 uri.format returns junk output.
-- As static allocator is also used in several places
-- we should properly handle situation when output
-- is zero-length string.
-- Here we allocate the whole buffer,
-- fill it with some "junk" bytes and
-- check that result doesn't contain any of them.
local buffer_size = 12288
local buf = static_alloc('char', buffer_size)
ffi.fill(buf, 512, string.byte('x'))
local iterations = 10
test:plan(iterations)
for _ = 1, iterations do
test:is(uri.format({}), '')
end
end
tap.test("uri", function(test)
test:plan(2)
test:plan(3)
test:test("parse", test_parse)
test:test("format", test_format)
test:test("static_alloc", test_static_alloc)
end)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment