nbctl_lr_policy_list() would sometimes, depending on compiler flags,
emit a warning message:

utilities/ovn-nbctl.c:5089:20: warning: ‘chain_current’ may be used
uninitialized.

The warning is actually wrong due to the i == 0 guard on the if
statement. However, even if the compiler is wrong, we strive to make our
code warning-free.

An easy way to get rid of the compiler warning would be to initialize
chain_current to be an empty string. However, I decided that if the code
is esoteric enough to make the compiler complain about a potentially
uninitialized value, then we could probably stand to make the code
easier to read.

Instead of having the i == 0 guard in the if statement, we now have an
explicit chain_prev pointer that we initialize to NULL. We can then use
!chain_prev as our guard, and compare chain_current with chain_prev to
determine if the chain has changed in our sorted array of policies.

Signed-off-by: Mark Michelson <[email protected]>
---
 utilities/ovn-nbctl.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/utilities/ovn-nbctl.c b/utilities/ovn-nbctl.c
index 0c111a9fd..f33da5be3 100644
--- a/utilities/ovn-nbctl.c
+++ b/utilities/ovn-nbctl.c
@@ -5079,15 +5079,16 @@ nbctl_lr_policy_list(struct ctl_context *ctx)
        Note that at this point policies are sorted out by chain names too.
     */
     char *chain_current;
+    char *chain_prev = NULL;
     int  print_chain_head = (n_policies
                              && policies[n_policies - 1].chain[0]);
 
     for (int i = 0; i < n_policies; i++) {
+        chain_current = policies[i].chain;
         /* Print chain heading */
         if (print_chain_head
-            && (i == 0
-                || strcmp(chain_current, policies[i].chain))) {
-            chain_current = policies[i].chain;
+            && (!chain_prev || strcmp(chain_prev, chain_current))) {
+            chain_prev = policies[i].chain;
             if (*chain_current) {
                 ds_put_format(&ctx->output, "\nChain %s:\n", chain_current);
             } else {
-- 
2.55.0

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to