-
Notifications
You must be signed in to change notification settings - Fork 28
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
Fix blob pad #51
Fix blob pad #51
Conversation
14d9314
to
a2d766d
Compare
Sorry that it took me almost a year to check your PR, and thanks for the contribution! I force-pushed the branch to rebase onto the latest state of |
Okay, I now understand what was wrong with commit 64072a489e0e8aac8dd818fb9bf5e39728f36191
Author: Andreas Linz <klingt.net@gmail.com>
Date: 2025-03-06 11:47:28 +0100
fixup! Fix blob pad
diff --git a/src/decoder.rs b/src/decoder.rs
index afc5d3b..425ea8a 100644
--- a/src/decoder.rs
+++ b/src/decoder.rs
@@ -152,7 +152,7 @@ fn read_osc_string<'a>(
map_res(
terminated(
take_till(|c| c == 0u8),
- nullbit_then_pad_to_32_bit_boundary(original_input),
+ pad_to_32_bit_boundary(original_input),
),
|str_buf: &'a [u8]| {
String::from_utf8(str_buf.into())
@@ -282,17 +282,10 @@ fn pad_to_32_bit_boundary<'a>(
original_input: &'a [u8],
) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], (), OscError> {
move |input| {
- let offset = (4 - original_input.offset(input) % 4) % 4;
- let (input, _) = take(offset)(input)?;
- Ok((input, ()))
- }
-}
-
-fn nullbit_then_pad_to_32_bit_boundary<'a>(
- original_input: &'a [u8],
-) -> impl Fn(&'a [u8]) -> IResult<&'a [u8], (), OscError> {
- move |input| {
- let offset = 4 - original_input.offset(input) % 4;
+ let offset = match original_input.offset(input) % 4 {
+ 0 => 0,
+ r => 4 - r % 4,
+ };
let (input, _) = take(offset)(input)?;
Ok((input, ()))
}
But, this breaks two existing tests, |
Yess, I have to duplicate the function because the tests broke, and it doesn't seems the assertions are wrong. For information, in the spec, string messages must end by the null byte, then completed with the padding, but blob don't end by this null byte. Feel free to modify this code if you found a more convenient way to do that, ask me if you want me to make some changes. |
In |
This fixes a bug that occurred when the content-length of a string, excluding the null-byte, was four-byte aligned. In this case we added 4 additional bytes of padding, instead of 3. We now consume the string including the zero-byte in the parser, and then applying the correct null-byte padding. Thanks to @edjin for providing the initial fix this patch is based on.
a2d766d
to
8b6c8be
Compare
I think with 8b6c8be I found a concise solution. There's still some potential for improvement, though. |
Fix decoding of four-byte aligned strings This fixes a bug that occurred when the content-length of a string, excluding the null-byte, was four-byte aligned. In this case we added 4 additional bytes of padding, instead of 3. We now consume the string including the zero-byte in the parser, and then apply the correct null-byte padding. Thanks to @edjin for providing the initial fix this patch is based on.
This looks good/better to me, Thanks 👍 |
Awesome, thanks for merging |
Hi!
I found the bug while trying to decode messages containing blobs. It happens that when a blob's size is divisible by 4, 4 more bytes than required are skipped.