From 857a060fe3a415c1909ad8a342d9ca21019ab2fc Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 13 Nov 2020 13:49:27 +0100 Subject: pkg/compiler: check for flags with all equal values There is no point in having flags when values are equal. This can only mean a typo or other bug. Check for such cases and fix 3 existing precedents. --- pkg/compiler/check.go | 12 +++++++++++- pkg/compiler/testdata/errors2.txt | 6 ++++++ sys/linux/netfilter.txt | 4 ++-- sys/openbsd/vnet.txt | 2 +- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 468b6e35e..022581771 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -248,7 +248,7 @@ func (comp *compiler) checkTypes() { func (comp *compiler) checkTypeValues() { for _, decl := range comp.desc.Nodes { - switch decl.(type) { + switch n := decl.(type) { case *ast.Call, *ast.Struct, *ast.Resource, *ast.TypeDef: comp.foreachType(decl, func(t *ast.Type, desc *typeDesc, args []*ast.Type, base prog.IntTypeCommon) { @@ -261,6 +261,16 @@ func (comp *compiler) checkTypeValues() { } } }) + case *ast.IntFlags: + allEqual := len(n.Values) >= 2 + for _, val := range n.Values { + if val.Value != n.Values[0].Value { + allEqual = false + } + } + if allEqual { + comp.error(n.Pos, "all %v values are equal %v", n.Name.Name, n.Values[0].Value) + } } } } diff --git a/pkg/compiler/testdata/errors2.txt b/pkg/compiler/testdata/errors2.txt index c862358cf..111db587f 100644 --- a/pkg/compiler/testdata/errors2.txt +++ b/pkg/compiler/testdata/errors2.txt @@ -370,6 +370,12 @@ foo$534(a ptr[in, flags[large_flags, int8]]) ### large_flags U16_MAX=0xffff does large_flags = U8_MAX, U16_MAX +equal_flags_0 = 0, 0, 0, 0 ### all equal_flags_0 values are equal 0 +equal_flags_1 = 42, 42, 42, 42 ### all equal_flags_1 values are equal 42 +non_equal_flags = 0, 0, 0, 1 + +equal_flags_call(a flags[equal_flags_0], b flags[equal_flags_1], c flags[non_equal_flags]) + type type500 proc[C1, 8, int8] ### values starting from 1 with step 8 overflow base type for 32 procs type type501 int8 ### unused type type501 type type502[C] const[C, int8] ### unused type type502 diff --git a/sys/linux/netfilter.txt b/sys/linux/netfilter.txt index a620f40e7..dc8655ec0 100644 --- a/sys/linux/netfilter.txt +++ b/sys/linux/netfilter.txt @@ -573,7 +573,7 @@ xt_ipcomp { hdrres const[0, int8] } -xt_ipcomp_flags = XT_IPCOMP_INV_SPI, XT_IPCOMP_INV_MASK +xt_ipcomp_flags = XT_IPCOMP_INV_SPI xt_statistic_info { mode bool16 @@ -768,7 +768,7 @@ xt_esp { invflags flags[xt_esp_flags, int8] } -xt_esp_flags = XT_ESP_INV_SPI, XT_ESP_INV_MASK +xt_esp_flags = XT_ESP_INV_SPI xt_cpu_info { cpu int32 diff --git a/sys/openbsd/vnet.txt b/sys/openbsd/vnet.txt index bd18f899c..6c229b969 100644 --- a/sys/openbsd/vnet.txt +++ b/sys/openbsd/vnet.txt @@ -566,7 +566,7 @@ ipv6_hopots_ext_header { options array[ipv6_tlv_option] } [packed, align[8]] -ipv6_routing_types = IPV6_RTHDR_LOOSE, IPV6_RTHDR_TYPE_0 +ipv6_routing_types = IPV6_RTHDR_LOOSE ipv6_rt_hdr { next_header flags[ipv6_types, int8] -- cgit mrf-deployment