Skip to content

Commit

Permalink
ffi/mupdf: fix metadata strings (#1921)
Browse files Browse the repository at this point in the history
Don't create Lua strings with an embedded null terminator.

Cf. koreader/koreader#12468.
  • Loading branch information
benoit-pierre authored Sep 5, 2024
1 parent ecf6bc7 commit 3560f72
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions ffi/mupdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,20 @@ end
local function getMetadataInfo(doc, info)
local bufsize = 255
local buf = ffi.new("char[?]", bufsize)
-- `fz_lookup_metadata` return the number of bytes needed
-- to store the string, **including** the null terminator.
local res = M.fz_lookup_metadata(context(), doc, info, buf, bufsize)
if res > bufsize then
bufsize = res + 1
-- Buffer was too small.
bufsize = res
buf = ffi.new("char[?]", bufsize)
res = M.fz_lookup_metadata(context(), doc, info, buf, bufsize)
end
if res > -1 then
return ffi.string(buf, res)
if res > 1 then
-- Note: strip the null terminator.
return ffi.string(buf, res - 1)
end
-- Empty string or error (-1).
return ""
end

Expand Down

0 comments on commit 3560f72

Please sign in to comment.