Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

openvswitch: Distribute switch variables for initialization

Variables declared in a switch statement before any case statements
cannot be automatically initialized with compiler instrumentation (as
they are not part of any execution flow). With GCC's proposed automatic
stack variable initialization feature, this triggers a warning (and they
don't get initialized). Clang's automatic stack variable initialization
(via CONFIG_INIT_STACK_ALL=y) doesn't throw a warning, but it also
doesn't initialize such variables[1]. Note that these warnings (or silent
skipping) happen before the dead-store elimination optimization phase,
so even when the automatic initializations are later elided in favor of
direct initializations, the warnings remain.

To avoid these problems, move such variables into the "case" where
they're used or lift them up into the main function body.

net/openvswitch/flow_netlink.c: In function ‘validate_set’:
net/openvswitch/flow_netlink.c:2711:29: warning: statement will never be executed [-Wswitch-unreachable]
2711 | const struct ovs_key_ipv4 *ipv4_key;
| ^~~~~~~~

[1] https://bugs.llvm.org/show_bug.cgi?id=44916

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Kees Cook and committed by
David S. Miller
16a556ee 46d30cb1

+10 -8
+10 -8
net/openvswitch/flow_netlink.c
··· 2708 2708 return -EINVAL; 2709 2709 2710 2710 switch (key_type) { 2711 - const struct ovs_key_ipv4 *ipv4_key; 2712 - const struct ovs_key_ipv6 *ipv6_key; 2713 - int err; 2714 - 2715 2711 case OVS_KEY_ATTR_PRIORITY: 2716 2712 case OVS_KEY_ATTR_SKB_MARK: 2717 2713 case OVS_KEY_ATTR_CT_MARK: ··· 2719 2723 return -EINVAL; 2720 2724 break; 2721 2725 2722 - case OVS_KEY_ATTR_TUNNEL: 2726 + case OVS_KEY_ATTR_TUNNEL: { 2727 + int err; 2728 + 2723 2729 if (masked) 2724 2730 return -EINVAL; /* Masked tunnel set not supported. */ 2725 2731 ··· 2730 2732 if (err) 2731 2733 return err; 2732 2734 break; 2735 + } 2736 + case OVS_KEY_ATTR_IPV4: { 2737 + const struct ovs_key_ipv4 *ipv4_key; 2733 2738 2734 - case OVS_KEY_ATTR_IPV4: 2735 2739 if (eth_type != htons(ETH_P_IP)) 2736 2740 return -EINVAL; 2737 2741 ··· 2753 2753 return -EINVAL; 2754 2754 } 2755 2755 break; 2756 + } 2757 + case OVS_KEY_ATTR_IPV6: { 2758 + const struct ovs_key_ipv6 *ipv6_key; 2756 2759 2757 - case OVS_KEY_ATTR_IPV6: 2758 2760 if (eth_type != htons(ETH_P_IPV6)) 2759 2761 return -EINVAL; 2760 2762 ··· 2783 2781 return -EINVAL; 2784 2782 2785 2783 break; 2786 - 2784 + } 2787 2785 case OVS_KEY_ATTR_TCP: 2788 2786 if ((eth_type != htons(ETH_P_IP) && 2789 2787 eth_type != htons(ETH_P_IPV6)) ||