Skip to contentSkip to conversation

coverity issue 2 - dead default / code cleanup in mtcp.c

Openvpn-devel Gert Doering 2 participants 3 messages Dec 14, 2012 – Dec 21, 2012

lore
Share & tools
lore
#0 lore raw

Hi,

next one: mtcp.c, about line 470:

  unsigned int flags = MTP_NONE;

  if (TUN_OUT(c))
    flags |= MTP_TUN_OUT;
  if (LINK_OUT(c))
    flags |= MTP_LINK_OUT;

  switch (flags)
    {
    case MTP_TUN_OUT|MTP_LINK_OUT:
    case MTP_TUN_OUT:
      newaction = TA_TUN_WRITE;
      break;
    case MTP_LINK_OUT:
      newaction = TA_SOCKET_WRITE;
      break;
    case MTP_NONE:
      if (mi && socket_read_residual (c->c2.link_socket))
        newaction = TA_SOCKET_READ_RESIDUAL;
      else
        multi_tcp_set_global_rw_flags (m, mi);
      break;
    default:
      {
        struct gc_arena gc = gc_new ();
        msg (M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=%s flags=%d",
             multi_instance_string (mi, false, &gc),
             flags);
        gc_free (&gc);
        break;
      }
    }

coverity is complaining that the "default:" clause can never be reached, as all the possible values for "flags" are covered - and it's obviously right. It's not something critical, more a "code cleanup" thing.

(The whole usage of switch/case here reeks a bit, given that I think the code would be simpler by directly setting "newaction" depending on "if (TUN_OUT(c))" etc., not bothering with "flags" in the first place)

Cleanup in 2.4?

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...
#1 lore raw
Quoted 43 lines · Gert Doering

Hi,

next one: mtcp.c, about line 470:

  unsigned int flags = MTP_NONE;

  if (TUN_OUT(c))
    flags |= MTP_TUN_OUT;
  if (LINK_OUT(c))
    flags |= MTP_LINK_OUT;

  switch (flags)
    {
    case MTP_TUN_OUT|MTP_LINK_OUT:
    case MTP_TUN_OUT:
      newaction = TA_TUN_WRITE;
      break;
    case MTP_LINK_OUT:
      newaction = TA_SOCKET_WRITE;
      break;
    case MTP_NONE:
      if (mi && socket_read_residual (c->c2.link_socket))
        newaction = TA_SOCKET_READ_RESIDUAL;
      else
        multi_tcp_set_global_rw_flags (m, mi);
      break;
    default:
      {
        struct gc_arena gc = gc_new ();
        msg (M_FATAL, "MULTI TCP: multi_tcp_post bad state, mi=%s flags=%d",
             multi_instance_string (mi, false, &gc),
             flags);
        gc_free (&gc);
        break;
      }
    }

coverity is complaining that the "default:" clause can never be reached, as all the possible values for "flags" are covered - and it's obviously right. It's not something critical, more a "code cleanup" thing.

Quoted 5 lines

(The whole usage of switch/case here reeks a bit, given that I think the code would be simpler by directly setting "newaction" depending on "if (TUN_OUT(c))" etc., not bothering with "flags" in the first place)

Cleanup in 2.4?

Does it hurt to have this "dead code"? Agreed, if all the flags are covered, we'll never see 'default' in action. But is it possible that these flags can be extended at some point in the future, which we need to account for? I somehow find confidence in having this "trap" with msg(M_FATAL,...) for future scenarios. Not because I strongly believe it will be needed, but more like a precaution (as in "Yes, we have thought about this")

Signature · 3 lines
kind regards,

David Sommerseth
#2 lore raw

Hi,

Quoted 9 lines · David Sommerseth
Quoted 1 line

Cleanup in 2.4?

Does it hurt to have this "dead code"? Agreed, if all the flags are covered, we'll never see 'default' in action. But is it possible that these flags can be extended at some point in the future, which we need to account for? I somehow find confidence in having this "trap" with msg(M_FATAL,...) for future scenarios. Not because I strongly believe it will be needed, but more like a precaution (as in "Yes, we have thought about this")

Well, I'd completely get rid of the switch/case here - we only have 4 different cases, 2 of which are handled in the same subcase, so "setting up flags and then switch(flags), plus default: handler" seems to add more confusion than just handling possible cases directly in the if() statements...

In general, having a "must not happen" handler to validate assumptions is a good thing, but if those preconditions are set by code 5 lines up, it's a bit over the top...

gert

Signature · 4 lines
USENET is *not* the non-clickable part of WWW!
                                                           //www.muc.de/~gert/
Gert Doering - Munich, Germany                             [email protected]...
fax: +49-89-35655025                        [email protected]...