Bug was that Buffer_newDataReader() assumed buf is always writable,
which isn't the case after reading from a static buffer. Added a
separate flag to describe the nature of buf.
This commit is contained in:
Jasmijn Wellner 2020-12-26 13:19:55 +01:00
parent 884aa8bdc6
commit d30f0c7aca

View File

@ -27,6 +27,7 @@ local ffi = require("ffi")
local buf_pos = 0 local buf_pos = 0
local buf_size = -1 local buf_size = -1
local buf = nil local buf = nil
local buf_is_writable = true
local writable_buf = nil local writable_buf = nil
local writable_buf_size = nil local writable_buf_size = nil
local SEEN_LEN = {} local SEEN_LEN = {}
@ -35,22 +36,25 @@ local function Buffer_prereserve(min_size)
if buf_size < min_size then if buf_size < min_size then
buf_size = min_size buf_size = min_size
buf = ffi.new("uint8_t[?]", buf_size) buf = ffi.new("uint8_t[?]", buf_size)
buf_is_writable = true
end end
end end
local function Buffer_clear() local function Buffer_clear()
buf_size = -1 buf_size = -1
buf = nil buf = nil
buf_is_writable = true
writable_buf = nil writable_buf = nil
writable_buf_size = nil writable_buf_size = nil
end end
local function Buffer_makeBuffer(size) local function Buffer_makeBuffer(size)
if writable_buf then if not buf_is_writable then
buf = writable_buf buf = writable_buf
buf_size = writable_buf_size buf_size = writable_buf_size
writable_buf = nil writable_buf = nil
writable_buf_size = nil writable_buf_size = nil
buf_is_writable = true
end end
buf_pos = 0 buf_pos = 0
Buffer_prereserve(size) Buffer_prereserve(size)
@ -62,8 +66,11 @@ local function Buffer_newReader(str)
end end
local function Buffer_newDataReader(data, size) local function Buffer_newDataReader(data, size)
if buf_is_writable then
writable_buf = buf writable_buf = buf
writable_buf_size = buf_size writable_buf_size = buf_size
end
buf_is_writable = false
buf_pos = 0 buf_pos = 0
buf_size = size buf_size = size
buf = ffi.cast("uint8_t*", data) buf = ffi.cast("uint8_t*", data)
@ -74,6 +81,7 @@ local function Buffer_reserve(additional_size)
buf_size = buf_size * 2 buf_size = buf_size * 2
local oldbuf = buf local oldbuf = buf
buf = ffi.new("uint8_t[?]", buf_size) buf = ffi.new("uint8_t[?]", buf_size)
buf_is_writable = true
ffi.copy(buf, oldbuf, buf_pos) ffi.copy(buf, oldbuf, buf_pos)
end end
end end