Interesting (to me) Compiler checking

Hal Murray halmurray at sonic.net
Sun May 19 02:05:03 UTC 2024


While I was working on the extra+port stuff

I had the following code
  bool new_interface;
...
  new_interface = update_interfaces_phase1(NTP_PORT);
  if (extra_port)
    new_interface |= update_interfaces_phase1(extra_port);

Note that there is no initialization on new_interface.

I wanted to reverse the order of that pair of calls.  So I just moved 2 lines 
up.  That gives this code.
  bool new_interface;
...
  if (extra_port)
    new_interface |= update_interfaces_phase1(extra_port);
  new_interface = update_interfaces_phase1(NTP_PORT);

There are 2 bugs in there.  I didn't initialize new_interface and I left the 
last line as an = rather than changing t to an |=


Fedora, Debian, and Ubuntu didn't complain.  
FreeBSD complained: uninitialized variable on the |=.

The |= is working on an uninitialized variable.  But the following line 
"fixes" whatever was there.


Here is the correct code.

static bool
update_interfaces(void)
{
  bool new_interface = false;
  update_interfaces_phase0();
  if (extra_port)
    /* do first so our requests are sent from extra_port */
    new_interface |= update_interfaces_phase1(extra_port);
  new_interface |= update_interfaces_phase1(NTP_PORT);
  update_interfaces_phase2();
  update_interfaces_phase3();
  return new_interface;
}




-- 
These are my opinions.  I hate spam.





More information about the devel mailing list