Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BufferUtil - Fix off-by-one bug #222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

MrBlackbrow
Copy link

@MrBlackbrow MrBlackbrow commented Jan 17, 2025

I noticed that BufferUtil has a size comparison that is < when it should be <=. The reason this mistake happened is probably because buffers are 0-indexed, unlike the rest of Lua.

The problem can be seen with the following example:

local writer = BufferUtil.writer(3)
print(writer:GetCapacity()) -- 3
writer:WriteUInt8(1)
writer:WriteUInt8(1)
writer:WriteUInt8(1)
print(writer:GetCapacity()) -- 4

In the example you can see that the buffer is created with size 3 and we write 3 bytes to it, but it gets expanded to 4 even though it's not necessary. Also important to note that the issue won't be apparent when writing a number of bytes that's a power of two, because the "expanded" size ends up being same as before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant