10000 Fixing the generation for Attribute Min Max used in endpoint_config.h by brdandu · Pull Request #1617 · project-chip/zap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fixing the generation for Attribute Min Max used in endpoint_config.h #1617

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

Conversation

brdandu
Copy link
Collaborator
@brdandu brdandu commented Jul 3, 2025
  • Fixing the generation of min and max values to be auto assumed and generated correctly when either min or max are mentioned instead of both min and max
  • Also making sure we do not always typecase everything to (uint16_t) but instead typecast the values based on size and sign
  • Github: ZAP Incorrect min/max defaults for temperature #1615

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.

Summary of Changes

Hello @brdandu, 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 refines the generation of attribute minimum and maximum values for endpoint_config.h. It introduces intelligent inference for missing min/max boundaries based on the attribute's data type and implements dynamic typecasting to ensure that generated values are correctly represented according to their size and signedness, improving the robustness and accuracy of the generated code.

Highlights

  • Dynamic Typecasting for Attribute Values: The generator now dynamically determines the appropriate C type (e.g., uint8_t, int32_t) for min, max, and default attribute values. This replaces the previous hardcoded (uint16_t) cast, ensuring type correctness and preventing potential overflows or sign issues in the generated endpoint_config.h.
  • Automatic Min/Max Inference: Improved the generation logic to automatically infer missing minimum or maximum attribute values. If only one boundary (min or max) is specified, the other is now calculated based on the attribute's data type (signed/unsigned and its size), providing more robust and complete attribute definitions.
  • Refactored Attribute Collection: The collectAttributes function was updated to accept zclPackageIds. This enhancement allows the function to accurately retrieve the sign and size information for ZCL types, which is critical for the new dynamic typecasting and min/max inference logic.
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.

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 pull request introduces valuable improvements for generating attribute configurations. It correctly handles cases where only a min or max is specified by inferring the other bound based on the data type. Additionally, it replaces a hardcoded uint16_t cast with a dynamic type cast that respects the attribute's size and sign, which is a great enhancement for robustness.

I've identified one high-severity issue regarding potential precision loss when calculating default boundaries for large integer types and have provided a suggestion to use BigInt to resolve it. I've also suggested caching the result of types.getSignAndSizeOfZclType to improve performance. Otherwise, the changes are well-implemented and align with the pull request's objectives.

Comment on lines +1048 to +1071
let min, max
if (a.min != null) {
min = a.min
} else {
if (type_size_and_sign.isTypeSigned) {
// Signed min: -2^(typeSize*8-1)
min = -(2 ** (typeSize * 8 - 1))
} else {
// Unsigned min: 0
min = 0
}
}
if (a.max != null) {
max = a.max
} else {
if (type_size_and_sign.isTypeSigned) {
// Signed max: 2^(typeSize*8-1) - 1
max = 2 ** (typeSize * 8 - 1) - 1
} else {
// Unsigned max: 2^(typeSize*8) - 1
max = 2 ** (typeSize * 8) - 1
}
}

Choose a reason for hiding this comment

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

high

The calculation of default min and max values using the ** operator on standard JavaScript numbers can lead to a loss of precision for integer types larger than 53 bits (e.g., 64-bit integers). This can result in incorrect boundary values being generated.

To ensure correctness for all integer sizes, these calculations should be performed using BigInt.

          let min = a.min
          let max = a.max

          if (min == null) {
            if (type_size_and_sign.isTypeSigned) {
              // Signed min: -2^(typeSize*8-1)
              min = -(2n ** (BigInt(typeSize) * 8n - 1n))
            } else {
              // Unsigned min: 0
              min = 0n
            }
          }

          if (max == null) {
            if (type_size_and_sign.isTypeSigned) {
              // Signed max: 2^(typeSize*8-1) - 1
              max = 2n ** (BigInt(typeSize) * 8n - 1n) - 1n
            } else {
              // Unsigned max: 2^(typeSize*8) - 1
              max = 2n ** (BigInt(typeSize) * 8n) - 1n
            }
          }

@brdandu brdandu force-pushed the bug/fixingMinMaxEndpointConfigHeader/ZAP#1615 branch from adaea79 to 5737fcf Compare July 3, 2025 18:17
- Fixing the generation of min and max values to be auto assumed and generated correctly when either min or max are mentioned instead of both min and max
- Also making sure we do not always typecase everything to (uint16_t) but instead typecast the values based on size and sign
- Updating existing tests
- For unit testing removing max from BallastFactorAdjustment attribute and making sure 0xFF is generated for '{ (uint8_t)0xFF, (uint8_t)0x64, (uint8_t)0xFF }, /* BallastFactorAdjustment */'
- Github: ZAP#1615
@brdandu brdandu force-pushed the bug/fixingMinMaxEndpointConfigHeader/ZAP#1615 branch from 90153da to 92f159f Compare July 3, 2025 18:44
@brdandu brdandu merged commit 93f5d14 into project-chip:master Jul 3, 2025
13 checks passed
@@ -627,13 +628,19 @@ function endpoint_attribute_min_max_list(options) {
.forEach((tok) => {
switch (tok) {
case 'def':
defMinMaxItems.push(`(uint16_t)${defS}`)
defMinMaxItems.push(
`(${mm.isTypeSigned ? '' : 'u'}int${mm.typeSize * 8}_t)${defS}`
Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect this will completely break things, since the actual type those structs store is in fact uint16_t, no?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Did not cross check that. Just fixed this based on the typecasting being wrong

@@ -613,7 +613,8 @@ function endpoint_attribute_min_max_list(options) {

if (isNaN(def)) def = 0
if (isNaN(min)) min = 0
if (isNaN(max)) max = 0xffff
if (isNaN(max)) max = '0x' + 'FF'.repeat(mm.typeSize)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not the correct max for a signed type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -613,7 +613,8 @@ function endpoint_attribute_min_max_list(options) {

if (isNaN(def)) def = 0
if (isNaN(min)) min = 0
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not the correct min for a signed type. Should at least have a comment explaining why this is here in this form and why it's OK.

If the expectation is that this case never happens, shouldn't this error out instead of silently doing something wrong?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

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.

4 participants
0