Skip to content

Does not work in one example #1

Description

@gusevfe

Hello.

I've found an example of data, when encode-decode round trip does not give the identical result. It consists of four bytes: 0x00, 0x00, 0x80, 0x40.

The patch to add this test case is:

diff --git a/tests/test.cpp b/tests/test.cpp
index 078a6d4..bc35c4f 100644
--- a/tests/test.cpp
+++ b/tests/test.cpp
@@ -52,6 +52,7 @@ static void encode_decode_uint8()
     const uint8_t literalszeros[] = { 0x55, 0x00 };
     const uint8_t literalsones[]  = { 0xAA, 0xFF };
     const uint8_t mixed[]         = { 0xAA, 0xAA, 0xAA, 0xAA, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x
FF, 0xAA, 0x00 };
+    const uint8_t weird[]         = { 0x00, 0x00, 0x80, 0x40 };

     assert_true( roundtrip( zeros, 0xFFu ) );
     assert_true( roundtrip( ones ) );
@@ -63,6 +64,7 @@ static void encode_decode_uint8()
     assert_true( roundtrip( literalszeros ) );
     assert_true( roundtrip( literalsones ) );
     assert_true( roundtrip( mixed ) );
+    assert_true( roundtrip( weird ) );
 }

 static void encode_decode_uint16()

The test program then produces this result:

> ./obj/test
check failed! (file tests/test.cpp, line 67): roundtrip( weird )
Total tests: 45, Tests failed: 1

It was possible to fix this with the following change:

diff --git a/src/brle.h b/src/brle.h
index 70f7b83..e4391ba 100644
--- a/src/brle.h
+++ b/src/brle.h
@@ -409,6 +409,10 @@ constexpr auto decode( InputIt input, InputIt last, OutputIt output ) -> OutputI
             bit_count = bit_count - data_bits;
             bits      = in >> ( detail::literal_size - bit_count );
         }
+
+        if( bit_count == 0) {
+            bits = 0;
+        }
     }

     return output;

But I am not completely sure if this is a correct approach.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions