Skip to content

Commit

Permalink
Pad shorter writes with spaces to preserve null terminated buffer siz…
Browse files Browse the repository at this point in the history
…e information
  • Loading branch information
Dasaav-dsv committed Aug 25, 2024
1 parent b6bff3e commit 4ca0582
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ ThingName.__tostring = ThingName.read
function ThingName:write(name)
name = name and tostring(name)
if name == nil then return end
if string.len(name) > self.length then
error(string.format("ThingName:write length error: length of %s (%d) is greater than %d", name, string.len(name), self:len()))
local len = self.length - string.len(name)
if len > 0 then
-- pad with spaces (or we lose null terminated length information)
name = table.concat{name, string.rep(" ", len)}
end
-- already does boundary checking
tga.writeWCStr(self.ptr, name, self.length)
end

Expand Down

0 comments on commit 4ca0582

Please sign in to comment.