diff mbox series

[net-next,1/2,v2] tools: ynl-gen: fix enum index in _decode_enum(..)

Message ID 20230713090550.132858-2-arkadiusz.kubalewski@intel.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series tools: ynl-gen: fix parse multi-attr enum attribute | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers warning 1 maintainers not CCed: donald.hunter@gmail.com
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 18 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Arkadiusz Kubalewski July 13, 2023, 9:05 a.m. UTC
Remove wrong index adjustment, which is a leftover from adding
support for sparse enums.
enum.entries_by_val() function shall not subtract the start-value, as
it is indexed with real enum value.

Fixes: c311aaa74ca1 ("tools: ynl: fix enum-as-flags in the generic CLI")
Signed-off-by: Arkadiusz Kubalewski <arkadiusz.kubalewski@intel.com>
---
 tools/net/ynl/lib/ynl.py | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

Comments

Jakub Kicinski July 13, 2023, 4:02 p.m. UTC | #1
On Thu, 13 Jul 2023 11:05:49 +0200 Arkadiusz Kubalewski wrote:
> -        i = attr_spec.get('value-start', 0)
>          if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
>              value = set()
>              while raw:
>                  if raw & 1:
> -                    value.add(enum.entries_by_val[i].name)
> +                    value.add(enum.entries_by_val[raw & 1].name)
>                  raw >>= 1
> -                i += 1

This doesn't make sense, as I suggested you need to keep i for this
loop. Move it to the inside of the if 'enum-as-fla... and init to 0.

i is tracking which bit number we are at as we consume / shift out
bits from raw.

Have you ever used ChatGPT? No shame, just curious.
Arkadiusz Kubalewski July 18, 2023, 4:30 p.m. UTC | #2
>From: Jakub Kicinski <kuba@kernel.org>
>Sent: Thursday, July 13, 2023 6:03 PM
>
>On Thu, 13 Jul 2023 11:05:49 +0200 Arkadiusz Kubalewski wrote:
>> -        i = attr_spec.get('value-start', 0)
>>          if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
>>              value = set()
>>              while raw:
>>                  if raw & 1:
>> -                    value.add(enum.entries_by_val[i].name)
>> +                    value.add(enum.entries_by_val[raw & 1].name)
>>                  raw >>= 1
>> -                i += 1
>
>This doesn't make sense, as I suggested you need to keep i for this
>loop. Move it to the inside of the if 'enum-as-fla... and init to 0.
>
>i is tracking which bit number we are at as we consume / shift out
>bits from raw.
>

Yeah, you are right, I don't think I had clear mind when created those..

>Have you ever used ChatGPT? No shame, just curious.

I have used it, but I am not using it to prepare code, this was my fault,
chat would probably do better..

Anyway just sent v3 with this part fixed.

Thank you!
Arkadiusz
 
>--
>pw-bot: cr
diff mbox series

Patch

diff --git a/tools/net/ynl/lib/ynl.py b/tools/net/ynl/lib/ynl.py
index 1b3a36fbb1c3..3908438d3716 100644
--- a/tools/net/ynl/lib/ynl.py
+++ b/tools/net/ynl/lib/ynl.py
@@ -420,16 +420,14 @@  class YnlFamily(SpecFamily):
     def _decode_enum(self, rsp, attr_spec):
         raw = rsp[attr_spec['name']]
         enum = self.consts[attr_spec['enum']]
-        i = attr_spec.get('value-start', 0)
         if 'enum-as-flags' in attr_spec and attr_spec['enum-as-flags']:
             value = set()
             while raw:
                 if raw & 1:
-                    value.add(enum.entries_by_val[i].name)
+                    value.add(enum.entries_by_val[raw & 1].name)
                 raw >>= 1
-                i += 1
         else:
-            value = enum.entries_by_val[raw - i].name
+            value = enum.entries_by_val[raw].name
         rsp[attr_spec['name']] = value
 
     def _decode_binary(self, attr, attr_spec):