Forum: Cfengine Help
Subject: Re: Cfengine Help: Re: Cfengine Help: Possible bug in cfengine 2
Author: kevinmusker
Link to topic: https://cfengine.com/forum/read.php?3,16865,16870#msg-16870

That would fix it too, but the zero hash value is a valid value. 

When adding a value to the hash table, it hashes the variable name to generate 
a slot id. If the slot in the hash table is already taken, it tries the next 
one, and so on until it finds an empty slot. If the loop reaches the end of the 
array used to store the values, it wraps around to the first (0th) slot and 
carries on looking for an empty slot.

The problem comes in the exit condition of the GetMacroValue function. If the 
value is not present in the hash, starts at the slot corresponding to the hash 
value and loops around the hashtable. It exits when it either finds the correct 
key, or it has checked the slot before the one it started at. To exit for a 
hash-value of zero, the counter would have to be -1; but as we only reset to 0, 
this will never be true.

Rolling the initial IF statement into the main loop makes the code shorter and 
simpler to understand (disclaimer: this has not been tested):

i = slot = Hash(vname);

while (true)
   {
   if (CompareMacro(vname,ptr->hashtable) == 0)
      {
      /* Found the key, so return the value */
      for(sp = ptr->hashtable; *sp != '='; sp++)
         {
         }

      return(sp+1);
      }

   i++;

   if (i >= CF_HASHTABLESIZE-1)
      {
      /* End of the hashtable array, so loop back to the beginning */
      i = 0;
      }

   if (i == slot)
      {
      return(getenv(vname));  /* Look in environment if not found */
      }
   }
}


_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to