]> git.puffer.fish Git - matthieu/frr.git/commitdiff
[lib] Fix the struct message LOOKUP function to be more robust
authorPaul Jakma <paul.jakma@sun.com>
Thu, 28 Feb 2008 23:26:02 +0000 (23:26 +0000)
committerPaul Jakma <paul.jakma@sun.com>
Thu, 28 Feb 2008 23:26:02 +0000 (23:26 +0000)
2008-02-28 Paul Jakma <paul.jakma@sun.com>

* log.c: (mes_lookup) Sowmini Varadhan diagnosed a problem where
  this function can cause a NULL dereference, on lookups for unknown
  indices, or messages with NULL strings. Can occur, e.g., debug
  logging code when processing received messages. Fixed to accept a
  pointer to a default string to be used if there is no match.
* log.h: LOOKUP adjusted to match

lib/ChangeLog
lib/log.c
lib/log.h

index 613a6fcb6dcb4726dcca042627fc6bafb68b3085..da0fa8ca3b3e545f95b7dc5ac4ef5f68dfe1f64e 100644 (file)
@@ -1,3 +1,12 @@
+2008-02-28 Paul Jakma <paul.jakma@sun.com>
+
+       * log.c: (mes_lookup) Sowmini Varadhan diagnosed a problem where
+         this function can cause a NULL dereference, on lookups for unknown
+         indices, or messages with NULL strings. Can occur, e.g., debug
+         logging code when processing received messages. Fixed to accept a
+         pointer to a default string to be used if there is no match.
+       * log.h: LOOKUP adjusted to match
+
 2008-02-28 Paul Jakma <paul.jakma@sun.com>
 
        * linklist.c: This implementation expects that the data pointer not
index ff47cae0dddfc88f10e3a6448031b63a6d109e8c..ce00bfbb04c2c4eb3bc50e9efa7f0bc5089c56a9 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -752,14 +752,24 @@ lookup (struct message *mes, int key)
 }
 
 /* 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). */
+ * in the array size (instead of relying on a 0 key to terminate the search). 
+ *
+ * The return value is the message string if found, or the 'none' pointer
+ * provided otherwise.
+ */
 const char *
-mes_lookup (struct message *meslist, int max, int index)
+mes_lookup (struct message *meslist, int max, int index, const char *none)
 {
+  int pos = index - meslist[0].key;
+  
   /* 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;
+   * value in that slot.
+   * NB: key numbering might be offset from 0. E.g. protocol constants
+   * often start at 1.
+   */
+  if ((pos >= 0) && (pos < max)
+      && (meslist[pos].key == index))
+    return meslist[pos].str;
 
   /* fall back to linear search */
   {
@@ -769,14 +779,17 @@ mes_lookup (struct message *meslist, int max, int index)
       {
        if (meslist->key == index)
          {
+           const char *str = (meslist->str ? meslist->str : none);
+           
            zlog_debug ("message index %d [%s] found in position %d (max is %d)",
-                     index, meslist->str, i, max);
-           return meslist->str;
+                     index, str, i, max);
+           return str;
          }
       }
   }
   zlog_err("message index %d not found (max is %d)", index, max);
-  return NULL;
+  assert (none);
+  return none;
 }
 
 /* Wrapper around strerror to handle case where it returns NULL. */
index da8fbea8fd9473d28400d0ec68f3bd5cafc07ea6..7432b25e41e6fab74588dd89562c3545196eb609 100644 (file)
--- a/lib/log.h
+++ b/lib/log.h
@@ -142,10 +142,12 @@ extern int zlog_reset_file (struct zlog *zl);
 extern int zlog_rotate (struct zlog *);
 
 /* For hackey massage lookup and check */
-#define LOOKUP(x, y) mes_lookup(x, x ## _max, y)
+#define LOOKUP(x, y) mes_lookup(x, x ## _max, y, "(no item found)")
 
 extern const char *lookup (struct message *, int);
-extern const char *mes_lookup (struct message *meslist, int max, int index);
+extern const char *mes_lookup (struct message *meslist, 
+                               int max, int index,
+                               const char *no_item);
 
 extern const char *zlog_priority[];
 extern const char *zlog_proto_names[];