All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] net/ipv4: fib_trie: Avoid cryptic ternary expressions
@ 2019-06-18 21:14 Matthias Kaehlcke
  2019-06-18 21:45 ` Doug Anderson
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Matthias Kaehlcke @ 2019-06-18 21:14 UTC (permalink / raw)
  To: David S . Miller, Alexey Kuznetsov, Hideaki YOSHIFUJI, Alexander Duyck
  Cc: netdev, linux-kernel, Douglas Anderson, Matthias Kaehlcke

empty_child_inc/dec() use the ternary operator for conditional
operations. The conditions involve the post/pre in/decrement
operator and the operation is only performed when the condition
is *not* true. This is hard to parse for humans, use a regular
'if' construct instead and perform the in/decrement separately.

This also fixes two warnings that are emitted about the value
of the ternary expression being unused, when building the kernel
with clang + "kbuild: Remove unnecessary -Wno-unused-value"
(https://lore.kernel.org/patchwork/patch/1089869/):

CC      net/ipv4/fib_trie.o
net/ipv4/fib_trie.c:351:2: error: expression result unused [-Werror,-Wunused-value]
        ++tn_info(n)->empty_children ? : ++tn_info(n)->full_children;

Signed-off-by: Matthias Kaehlcke <mka@chromium.org>
---
I have no good understanding of the fib_trie code, but the
disentangled code looks wrong, and it should be equivalent to the
cryptic version, unless I messed it up. In empty_child_inc()
'full_children' is only incremented when 'empty_children' is -1. I
suspect a bug in the cryptic code, but am surprised why it hasn't
blown up yet. Or is it intended behavior that is just
super-counterintuitive?

For now I'm leaving it at disentangling the cryptic expressions,
if there is a bug we can discuss what action to take.
---
 net/ipv4/fib_trie.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 868c74771fa9..cf7788e336b7 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -338,12 +338,18 @@ static struct tnode *tnode_alloc(int bits)
 
 static inline void empty_child_inc(struct key_vector *n)
 {
-	++tn_info(n)->empty_children ? : ++tn_info(n)->full_children;
+	tn_info(n)->empty_children++;
+
+	if (!tn_info(n)->empty_children)
+		tn_info(n)->full_children++;
 }
 
 static inline void empty_child_dec(struct key_vector *n)
 {
-	tn_info(n)->empty_children-- ? : tn_info(n)->full_children--;
+	if (!tn_info(n)->empty_children)
+		tn_info(n)->full_children--;
+
+	tn_info(n)->empty_children--;
 }
 
 static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
-- 
2.22.0.410.gd8fdbe21b5-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-06-19 21:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-18 21:14 [PATCH] net/ipv4: fib_trie: Avoid cryptic ternary expressions Matthias Kaehlcke
2019-06-18 21:45 ` Doug Anderson
2019-06-18 22:57   ` Matthias Kaehlcke
2019-06-18 23:04 ` Nathan Chancellor
2019-06-18 23:21   ` Matthias Kaehlcke
2019-06-19  2:00     ` Alexander Duyck
2019-06-18 23:23   ` Nick Desaulniers
2019-06-19  9:36     ` Joe Perches
2019-06-19 17:41       ` Nick Desaulniers
2019-06-19 18:10         ` Joe Perches
2019-06-19 21:29 ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.