diff options
author | Jakub Kicinski | 2023-06-12 08:59:19 -0700 |
---|---|---|
committer | Jakub Kicinski | 2023-06-13 11:46:46 -0700 |
commit | 10c4d2a7b88d65471a5e56bb8946b5bb59ff5cf7 (patch) | |
tree | 87d4d8ecbb75cf49fbaaa8859a64234ad2979099 /tools/net | |
parent | 6b5f9a87e12d044f513a4f4c0e31ac7b5e988b66 (diff) |
tools: ynl-gen: correct enum policies
Scalar range validation assumes enums start at 0.
Teach it to properly calculate the value range.
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Diffstat (limited to 'tools/net')
-rwxr-xr-x | tools/net/ynl/ynl-gen-c.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/tools/net/ynl/ynl-gen-c.py b/tools/net/ynl/ynl-gen-c.py index 870f98d0e12c..54777d529f5e 100755 --- a/tools/net/ynl/ynl-gen-c.py +++ b/tools/net/ynl/ynl-gen-c.py @@ -300,8 +300,10 @@ class TypeScalar(Type): return f"NLA_POLICY_MIN({policy}, {self.checks['min']})" elif 'enum' in self.attr: enum = self.family.consts[self.attr['enum']] - cnt = len(enum['entries']) - return f"NLA_POLICY_MAX({policy}, {cnt - 1})" + low, high = enum.value_range() + if low == 0: + return f"NLA_POLICY_MAX({policy}, {high})" + return f"NLA_POLICY_RANGE({policy}, {low}, {high})" return super()._attr_policy(policy) def _attr_typol(self): @@ -676,6 +678,15 @@ class EnumSet(SpecEnumSet): def new_entry(self, entry, prev_entry, value_start): return EnumEntry(self, entry, prev_entry, value_start) + def value_range(self): + low = min([x.value for x in self.entries.values()]) + high = max([x.value for x in self.entries.values()]) + + if high - low + 1 != len(self.entries): + raise Exception("Can't get value range for a noncontiguous enum") + + return low, high + class AttrSet(SpecAttrSet): def __init__(self, family, yaml): |