]> git.puffer.fish Git - mirror/frr.git/commitdiff
[lib] Make message lookup function more robust.
authorAndrew J. Schorr <ajschorr@alumni.princeton.edu>
Tue, 20 Mar 2007 20:48:27 +0000 (20:48 +0000)
committerAndrew J. Schorr <ajschorr@alumni.princeton.edu>
Tue, 20 Mar 2007 20:48:27 +0000 (20:48 +0000)
2007-03-20 Andrew J. Schorr <ajschorr@alumni.princeton.edu>

* log.c: (mes_lookup) Make the function more robust: check for
  cases where the index does not match the key value at that position.
  If so, give a warning and fall back to a linear search.
  And improve the error message in cases where even that fails.

lib/ChangeLog
lib/log.c

index 914cc5e82d637b332c64de22ff1309fe7533c030..99b82b1f3f26d7631e5da62499126f047803b4b1 100644 (file)
@@ -1,3 +1,10 @@
+2007-03-20 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+       * log.c: (mes_lookup) Make the function more robust: check for
+         cases where the index does not match the key value at that position.
+         If so, give a warning and fall back to a linear search.
+         And improve the error message in cases where even that fails.
+
 2006-12-12 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
 
        * if.h: (struct connected) Add new ZEBRA_IFA_PEER flag indicating
index 3eb0bd523dbe363644f4bc08ca70e10ffc3acb03..c123f24f6caf6abcf15159c2bb827c7129336a53 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -693,17 +693,32 @@ lookup (struct message *mes, int key)
   return "";
 }
 
-/* Very old hacky version of message lookup function.  Still partly
-   used in bgpd and ospfd. FIXME Seems that it's not used any more. */
+/* Older/faster version of message lookup function, but requires caller to pass
+   in the array size (instead of relying on a 0 key to terminate the search). */
 const char *
 mes_lookup (struct message *meslist, int max, int index)
 {
-  if (index < 0 || index >= max) 
-    {
-      zlog_err ("message index out of bound: %d", max);
-      return NULL;
-    }
-  return meslist[index].str;
+  /* first check for best case: index is in range and matches the key
+     value in that slot */
+  if ((index >= 0) && (index < max) && (meslist[index].key == index))
+    return meslist[index].str;
+
+  /* fall back to linear search */
+  {
+    int i;
+
+    for (i = 0; i < max; i++, meslist++)
+      {
+       if (meslist->key == index)
+         {
+           zlog_warn("message index %d [%s] found in position %d (max is %d)",
+                     index, meslist->str, i, max);
+           return meslist->str;
+         }
+      }
+  }
+  zlog_err("message index %d not found (max is %d)", index, max);
+  return NULL;
 }
 
 /* Wrapper around strerror to handle case where it returns NULL. */