Skip to content

[netdata] use bit manipulation helpers for NetworkDataTlv - #11810

Merged
jwhui merged 2 commits into
openthread:mainfrom
abtink:netdata/tlv-use-bit-helper
Aug 20, 2025
Merged

[netdata] use bit manipulation helpers for NetworkDataTlv#11810
jwhui merged 2 commits into
openthread:mainfrom
abtink:netdata/tlv-use-bit-helper

Conversation

@abtink

@abtink abtink commented Aug 12, 2025

Copy link
Copy Markdown
Member

This change updates the NetworkDataTlv class to use common bit manipulation helper functions (ReadBits, WriteBits, SetBit, GetBit, ClearBit).

This change replaces the manual bit masking and shifting for accessing the Type value and the Stable flag with calls to the new helpers. The member variable mType is also renamed to mTypeAndStableFlag to more accurately reflect its contents.


Added a second commit on this PR which addresses the false-positive gcc warning from #11810 (comment).

[toolchain] suppress gcc string operation warning (#11810)

Introduces macros to suppress a known false-positive GCC warning "-Wstringop-overflow=0" which can be triggered when manipulating network data.

The AddHasRoute(), AddBorderRouter(), and AddServer() methods shift and update the network data bytes, which may involve inserting or updating a sub-TLV within an existing TLV. This can trigger a "writing x byte into a region of size 0" error on some GCC toolchains.

This change adds the OT_SUPPRESS_GCC_STRING_OP_BEGIN and OT_SUPPRESS_GCC_STRING_OP_END macros to silence this specific warning within these code blocks.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of Changes

Hello @abtink, 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!

This pull request refactors the NetworkDataTlv class to enhance code readability and maintainability by replacing manual bit manipulation with dedicated helper functions. The change centralizes bitwise operations for accessing the Type value and Stable flag, and improves clarity by renaming a key member variable to accurately reflect its contents.

Highlights

  • Refactoring: Replaced manual bit masking and shifting in NetworkDataTlv with common bit manipulation helper functions (ReadBits, WriteBits, SetBit, GetBit, ClearBit).
  • Code Clarity: Renamed the mType member variable to mTypeAndStableFlag to better reflect its combined storage of type and stable flag information.
  • Dependency: Introduced a dependency on common/num_utils.hpp for the new bit manipulation helpers.
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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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.

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.

@abtink abtink changed the title [netdata] to use bit manipulation helpers for NetworkDataTlv [netdata] use bit manipulation helpers for NetworkDataTlv Aug 12, 2025

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request is a nice cleanup that replaces manual bit-shifting and masking operations with common bit manipulation helper functions from num_utils.hpp. This improves code readability and maintainability. The member variable mType is also aptly renamed to mTypeAndStableFlag. The changes are correct and well-implemented. I have one minor suggestion to further simplify the code by removing a constant that has become redundant after the refactoring.

Comment thread src/core/thread/network_data_tlvs.hpp
@github-actions

github-actions Bot commented Aug 12, 2025

Copy link
Copy Markdown

Merging #11810 into main

name branch text data bss total
ot-cli-ftd f3efd14 479144 860 66556 546560
1d12ff8 479160 860 66556 546576
+/- +16 +0 +0 +16
ot-ncp-ftd f3efd14 444860 764 61800 507424
1d12ff8 444860 764 61800 507424
+/- +0 +0 +0 +0
ot-cli-mtd f3efd14 372000 764 51036 423800
1d12ff8 372000 764 51036 423800
+/- +0 +0 +0 +0
ot-ncp-mtd f3efd14 353100 764 46304 400168
1d12ff8 353100 764 46304 400168
+/- +0 +0 +0 +0
ot-cli-ftd-br f3efd14 579120 868 135724 715712
1d12ff8 579136 868 135724 715728
+/- +16 +0 +0 +16
ot-rcp f3efd14 63136 568 20804 84508
1d12ff8 63136 568 20804 84508
+/- +0 +0 +0 +0
Library files
name branch text data bss total
libopenthread-ftd.a f3efd14 246330 95 40334 286759
1d12ff8 246332 95 40334 286761
+/- +2 +0 +0 +2
libopenthread-cli-ftd.a f3efd14 60843 0 8083 68926
1d12ff8 60843 0 8083 68926
+/- +0 +0 +0 +0
libopenthread-ncp-ftd.a f3efd14 33257 0 5948 39205
1d12ff8 33257 0 5948 39205
+/- +0 +0 +0 +0
libopenthread-mtd.a f3efd14 164827 0 24838 189665
1d12ff8 164827 0 24838 189665
+/- +0 +0 +0 +0
libopenthread-cli-mtd.a f3efd14 41134 0 8059 49193
1d12ff8 41134 0 8059 49193
+/- +0 +0 +0 +0
libopenthread-ncp-mtd.a f3efd14 25785 0 5948 31733
1d12ff8 25785 0 5948 31733
+/- +0 +0 +0 +0
libopenthread-ftd-br.a f3efd14 349199 100 109470 458769
1d12ff8 349205 100 109470 458775
+/- +6 +0 +0 +6
libopenthread-cli-ftd-br.a f3efd14 77278 0 8115 85393
1d12ff8 77278 0 8115 85393
+/- +0 +0 +0 +0
libopenthread-rcp.a f3efd14 9932 0 5060 14992
1d12ff8 9932 0 5060 14992
+/- +0 +0 +0 +0
libopenthread-radio.a f3efd14 19423 0 238 19661
1d12ff8 19423 0 238 19661
+/- +0 +0 +0 +0

@abtink
abtink force-pushed the netdata/tlv-use-bit-helper branch 2 times, most recently from 769f11a to 49680af Compare August 12, 2025 19:09
@codecov

codecov Bot commented Aug 12, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.40%. Comparing base (50edbc8) to head (e5d9b48).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11810      +/-   ##
==========================================
- Coverage   75.03%   71.40%   -3.64%     
==========================================
  Files         631      644      +13     
  Lines       89675   100205   +10530     
==========================================
+ Hits        67286    71549    +4263     
- Misses      22389    28656    +6267     
Files with missing lines Coverage Δ
src/core/common/num_utils.hpp 96.66% <100.00%> (-1.67%) ⬇️
src/core/thread/network_data_leader_ftd.cpp 92.39% <ø> (+7.88%) ⬆️
src/core/thread/network_data_tlvs.hpp 79.71% <100.00%> (-18.02%) ⬇️

... and 404 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abtink

abtink commented Aug 12, 2025

Copy link
Copy Markdown
Member Author

We get this strange build error (only on specific gcc version) for this change:

In file included from /home/runner/work/openthread/openthread/src/core/common/data.hpp:47,
                 from /home/runner/work/openthread/openthread/src/core/common/message.hpp:49,
                 from /home/runner/work/openthread/openthread/src/core/coap/coap_message.hpp:46,
                 from /home/runner/work/openthread/openthread/src/core/coap/coap.hpp:36,
                 from /home/runner/work/openthread/openthread/src/core/thread/network_data_leader.hpp:41,
                 from /home/runner/work/openthread/openthread/src/core/thread/network_data_leader_ftd.cpp:34:
In function ‘void ot::SetBit(UintType&, uint8_t) [with UintType = unsigned char]’,
    inlined from ‘void ot::NetworkData::NetworkDataTlv::SetStable()’ at /home/runner/work/openthread/openthread/src/core/thread/network_data_tlvs.hpp:238:43,
    inlined from ‘ot::Error ot::NetworkData::Leader::AddHasRoute(const ot::NetworkData::HasRouteTlv&, ot::NetworkData::PrefixTlv&, ChangedFlags&)’ at /home/runner/work/openthread/openthread/src/core/thread/network_data_leader_ftd.cpp:856:35:
/home/runner/work/openthread/openthread/src/core/common/num_utils.hpp:275:11: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
  275 |     aBits |= static_cast<UintType>(static_cast<UintType>(1) << aBitOffset);
      |     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/runner/work/openthread/openthread/src/core/thread/network_data.hpp:52,
                 from /home/runner/work/openthread/openthread/src/core/thread/network_data_leader.hpp:48:
/home/runner/work/openthread/openthread/src/core/thread/network_data_tlvs.hpp: In member function ‘ot::Error ot::NetworkData::Leader::AddHasRoute(const ot::NetworkData::HasRouteTlv&, ot::NetworkData::PrefixTlv&, ChangedFlags&)’:
/home/runner/work/openthread/openthread/src/core/thread/network_data_tlvs.hpp:579:7: note: at offset 2 into destination object ‘ot::NetworkData::PrefixTlv::<anonymous>’ of size 2

@abtink
abtink force-pushed the netdata/tlv-use-bit-helper branch from 49680af to 303c7b3 Compare August 18, 2025 18:32
…ad#11810)

This change updates the `NetworkDataTlv` class to use common bit
manipulation helper functions (`ReadBits`, `WriteBits`, `SetBit`,
`GetBit`, `ClearBit`).

This change replaces the manual bit masking and shifting for accessing
the `Type` value and the `Stable` flag with calls to the new helpers.
The member variable `mType` is also renamed to `mTypeAndStableFlag` to
more accurately reflect its contents.
abtink added a commit to abtink/openthread that referenced this pull request Aug 18, 2025
Introduces macros to suppress a known false-positive GCC warning
"-Wstringop-overflow=0" which can be triggered when manipulating
network data.

The `AddHasRoute()`, `AddBorderRouter()`, and `AddServer()` methods
shift and update the network data bytes, which may involve inserting
or updating a sub-TLV within an existing TLV. This can trigger a
"writing x byte into a region of size 0" error on some GCC
toolchains.

This change adds the `OT_SUPPRESS_GCC_STRING_OP_BEGIN` and
`OT_SUPPRESS_GCC_STRING_OP_END` macros to silence this specific
warning within these code blocks.
@abtink
abtink force-pushed the netdata/tlv-use-bit-helper branch from 303c7b3 to b19b992 Compare August 18, 2025 19:31
@abtink

abtink commented Aug 18, 2025

Copy link
Copy Markdown
Member Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces two main changes. First, it refactors the NetworkDataTlv class to use common bit manipulation helper functions, which improves code clarity and maintainability. The member variable mType is also aptly renamed to mTypeAndStableFlag. Second, it adds macros to suppress a specific false-positive GCC warning related to string operations, which is a good practice for handling known toolchain issues.

My review focuses on the correctness of these changes. The refactoring of NetworkDataTlv is well-implemented. However, I've found a potential issue in the implementation of the GCC warning suppression macro, which I've detailed in a specific comment. The keyword used to suppress the warning seems incorrect.

Comment thread include/openthread/platform/toolchain.h
Introduces macros to suppress a known false-positive GCC warning
"-Wstringop-overflow=0" which can be triggered when manipulating
network data.

The `AddHasRoute()`, `AddBorderRouter()`, and `AddServer()` methods
shift and update the network data bytes, which may involve inserting
or updating a sub-TLV within an existing TLV. This can trigger a
"writing x byte into a region of size 0" error on some GCC
toolchains.

This change adds the `OT_SUPPRESS_GCC_STRING_OP_BEGIN` and
`OT_SUPPRESS_GCC_STRING_OP_END` macros to silence this specific
warning within these code blocks.
@abtink
abtink force-pushed the netdata/tlv-use-bit-helper branch from b19b992 to e5d9b48 Compare August 18, 2025 19:37
@abtink
abtink marked this pull request as ready for review August 18, 2025 23:06
@jwhui
jwhui requested a review from Copilot August 19, 2025 18:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR modernizes the NetworkDataTlv class by replacing manual bit manipulation with common helper functions and addresses a false-positive GCC warning in network data manipulation methods.

  • Replaces manual bit masking/shifting with standardized ReadBits, WriteBits, SetBit, GetBit, and ClearBit helpers
  • Renames mType to mTypeAndStableFlag for clarity
  • Adds GCC pragma macros to suppress false-positive string operation warnings

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/core/thread/network_data_tlvs.hpp Refactors NetworkDataTlv to use bit manipulation helpers and renames member variable
src/core/thread/network_data_leader_ftd.cpp Applies GCC warning suppression macros around network data manipulation methods
src/core/common/num_utils.hpp Simplifies SetBit and ClearBit implementations using compound assignment operators
include/openthread/platform/toolchain.h Adds GCC pragma macros for suppressing string operation warnings
include/openthread/instance.h Increments API version number
Comments suppressed due to low confidence (1)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@jwhui
jwhui merged commit 8b1f217 into openthread:main Aug 20, 2025
102 of 103 checks passed
jwhui pushed a commit that referenced this pull request Aug 20, 2025
This change updates the `NetworkDataTlv` class to use common bit
manipulation helper functions (`ReadBits`, `WriteBits`, `SetBit`,
`GetBit`, `ClearBit`).

This change replaces the manual bit masking and shifting for accessing
the `Type` value and the `Stable` flag with calls to the new helpers.
The member variable `mType` is also renamed to `mTypeAndStableFlag` to
more accurately reflect its contents.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants