8000 Fix cbor encoding for uint64_t's by Ian-Flury · Pull Request #442 · getml/reflect-cpp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix cbor encoding for uint64_t's #442

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

Merged
merged 1 commit into from
May 31, 2025

Conversation

Ian-Flury
Copy link
Contributor

A fix for cbor encoding; unsigned integers were being emitted as signed ints.

Copy link
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @Ian-Flury, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

Hello! Gemini here, ready to provide a summary of this pull request to help everyone get up to speed quickly. This PR, authored by Ian-Flury, aims to fix an issue in the CBOR encoding logic where unsigned integers, specifically uint64_t, were being incorrectly emitted as signed integers. The changes involve adjusting the Writer class to handle unsigned types separately and adding a new test case to verify the correct encoding of both signed and unsigned 64-bit integers according to the CBOR specification (RFC 8949).

Highlights

  • Fix CBOR Unsigned Integer Encoding: The core issue addressed is the incorrect encoding of unsigned integers (like uint64_t) as signed integers in the CBOR writer. This PR introduces a specific check for unsigned types to ensure they are encoded using the appropriate CBOR major type for unsigned integers.
  • Add Test for Integer Signedness: A new test file and test case have been added to specifically verify that both signed (int64_t) and unsigned (uint64_t) integers are encoded correctly according to the CBOR specification, using the distinct major types for each.

Changelog

  • include/rfl/cbor/Writer.hpp
    • Added a new if constexpr condition to specifically check for std::is_unsigned before the general std::is_integral check.
    • Ensured that unsigned types are cast to std::uint64_t and encoded using encoder_->uint64_value.
  • tests/cbor/test_integers.cpp
    • Added a new test file test_integers.cpp.
    • Added a new test case test_integers_signedness to verify the correct CBOR encoding bytes for large uint64_t and int64_t values.
    • Included expected byte sequences based on RFC 8949 for unsigned (0x1B) and negative (0x20) integer major types.
    • Used memcmp to compare the generated CBOR output against the expected byte sequences.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Unsigned bits, a tricky plight,
Encoded wrong, not shining bright.
A fix applied,
Now verified,
CBOR bytes are finally right.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This PR correctly addresses the CBOR encoding issue for unsigned 64-bit integers by adding a specific handling path for std::is_unsigned types. The change in Writer.hpp is clear and effective. The new test case in test_integers.cpp is a valuable addition and thoroughly checks the signedness behavior with a well-chosen boundary value (0xffffffffffffffff). The comments explaining the CBOR byte structure in the test are particularly helpful.

I have a couple of suggestions regarding the byte buffer comparison method in the tests to improve its robustness.

Summary of Findings

  • Test comparison robustness: The memcmp usage in the new tests (test_integers.cpp lines 27 and 36) could lead to undefined behavior (if the actual buffer is shorter than expected) or false positives (if the actual buffer is longer but has a matching prefix). It's recommended to use direct std::vector comparison with EXPECT_EQ after appropriate type conversion (e.g., to std::vector<unsigned char>) for safer and more idiomatic GTest assertions. This ensures both size and content are fully verified.

Merge Readiness

The core fix for CBOR encoding in Writer.hpp is well-implemented and directly addresses the described issue. The accompanying tests are a great addition. However, there's a high-severity issue identified in the new test's comparison logic that could affect test reliability by potentially causing crashes or missing discrepancies. I recommend addressing this test issue to ensure the validation is robust before merging. As a reviewer, I am not authorized to approve pull requests, so please ensure further necessary reviews and approvals are obtained.

Signed-off-by: David Corbeil <8996584+dcorbeil@users.noreply.github.com>
Signed-off-by: Ian Flury <ianflury123@gmail.com>
@Ian-Flury Ian-Flury force-pushed the encode_cbor_uint64 branch from 58537e0 to 1fe1700 Compare May 30, 2025 23:15
@liuzicheng1987
Copy link
Contributor

@Ian-Flury , thanks for your contribution!

@liuzicheng1987 liuzicheng1987 merged commit dd257a8 into getml:main May 31, 2025
13 checks passed
@Ian-Flury Ian-Flury deleted the encode_cbor_uint64 branch June 2, 2025 22:55
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.

3 participants
0