Index: trunk/include/regfi.h
===================================================================
--- trunk/include/regfi.h	(revision 135)
+++ trunk/include/regfi.h	(revision 136)
@@ -36,4 +36,5 @@
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdarg.h>
 #include <string.h>
 #include <errno.h>
@@ -70,5 +71,5 @@
 /* XXX: Has MS defined a REG_QWORD_BE? */
 /* Not a real type in the registry */
-#define REG_KEY                        0x7FFFFFFF
+#define REG_KEY                    0x7FFFFFFF
 
 #define REGFI_REGF_SIZE            0x1000 /* "regf" header block size */
@@ -325,6 +326,6 @@
 int                   regfi_close(REGFI_FILE* r);
 
-/* regfi_get_message: Get errors, warnings, and/or verbose information
- *                    relating to processing of the given registry file.
+/* regfi_get_messages: Get errors, warnings, and/or verbose information
+ *                     relating to processing of the given registry file.
  *
  * Arguments:
@@ -334,5 +335,5 @@
  *   A newly allocated char* which must be free()d by the caller.
  */
-char*                 regfi_get_message(REGFI_FILE* file);
+char*                 regfi_get_messages(REGFI_FILE* file);
 
 REGFI_ITERATOR*       regfi_iterator_new(REGFI_FILE* fh);
@@ -434,4 +435,4 @@
 					      REGFI_SUBKEY_LIST** lists,
 					      bool strict);
-void                  regfi_add_message(REGFI_FILE* file, const char* error);
+void                  regfi_add_message(REGFI_FILE* file, const char* fmt, ...);
 #endif	/* _REGFI_H */
Index: trunk/lib/lru_cache.c
===================================================================
--- trunk/lib/lru_cache.c	(revision 135)
+++ trunk/lib/lru_cache.c	(revision 136)
@@ -236,4 +236,6 @@
 	return false;
       }
+      else
+	e->index = tmp_index;
     }
     else
Index: trunk/lib/regfi.c
===================================================================
--- trunk/lib/regfi.c	(revision 135)
+++ trunk/lib/regfi.c	(revision 136)
@@ -37,25 +37,31 @@
 /******************************************************************************
  ******************************************************************************/
-void regfi_add_message(REGFI_FILE* file, const char* error)
-{
-  /* XXX: This function is not particularly efficient, 
+void regfi_add_message(REGFI_FILE* file, const char* fmt, ...)
+{
+  /* XXX: This function is not particularly efficient,
    *      but then it is mostly used during errors. 
    */
-  uint32 length;
-  char* tmp;
+  /* XXX: Should we add support for filtering by levels of severity? */
+  uint32 buf_size, buf_used;
+  char* new_msg;
+  va_list args;
 
   if(file->last_message == NULL)
-    length = 0;
+    buf_used = 0;
   else
-    length = strlen(error);
-
-  tmp = realloc(file->last_message, length+strlen(file->last_message)+2);
-  if(tmp == NULL)
-    /* XXX: should we do something else here?  */
+    buf_used = strlen(file->last_message);
+
+  buf_size = buf_used+strlen(fmt)+2+128;
+  new_msg = realloc(file->last_message, buf_size);
+  if(new_msg == NULL)
+    /* XXX: should we report this? */
     return;
   
-  if(length > 0)
-    strcat(tmp, "\n");
-  strcat(tmp, error);
+  va_start(args, fmt);
+  vsnprintf(new_msg+buf_used, buf_size-buf_used, fmt, args);
+  va_end(args);
+  strncat(new_msg, "\n", buf_size-1);
+
+  file->last_message = new_msg;
 }
 
@@ -63,5 +69,5 @@
 /******************************************************************************
  ******************************************************************************/
-char* regfi_get_message(REGFI_FILE* file)
+char* regfi_get_messages(REGFI_FILE* file)
 {
   char* ret_val = file->last_message;
@@ -310,5 +316,5 @@
       /* XXX: this is slow */
       extra = strlen(sid_str) + strlen(type_str) 
-	+ strlen(perms_str) + strlen(flags_str)+5;
+	+ strlen(perms_str) + strlen(flags_str) + 5;
       tmp_val = realloc(ret_val, size+extra);
 
@@ -316,4 +322,5 @@
       {
 	free(ret_val);
+	ret_val = NULL;
 	failed = true;
       }
@@ -840,5 +847,5 @@
     return NULL;
 
-  ret_val = (REGFI_VK_REC**)zalloc(sizeof(REGFI_VK_REC*) * num_values);
+  ret_val = (REGFI_VK_REC**)zalloc(sizeof(REGFI_VK_REC*) * usable_num_values);
   if(ret_val == NULL)
   {
@@ -1375,5 +1382,5 @@
 const REGFI_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i)
 {
-  REGFI_SK_REC* ret_val;
+  REGFI_SK_REC* ret_val = NULL;
   REGFI_HBIN* hbin;
   uint32 max_length, off;
@@ -1982,5 +1989,5 @@
     if(ret_val->data_in_offset)
     {
-      ret_val->data = regfi_parse_data(file, data_offset, 
+      ret_val->data = regfi_parse_data(file, ret_val->data_off, 
 				       raw_data_size, 4, strict);
     }
@@ -2030,5 +2037,4 @@
       return NULL;
 
-    offset = offset - REGFI_REGF_SIZE;
     for(i = 0; i < length; i++)
       ret_val[i] = (uint8)((offset >> i*8) & 0xFF);
Index: trunk/lib/smb_deps.c
===================================================================
--- trunk/lib/smb_deps.c	(revision 135)
+++ trunk/lib/smb_deps.c	(revision 136)
@@ -33,6 +33,7 @@
 {
   void* ret_val = NULL;
-  if((ret_val = (void*)malloc(size)) != NULL)
+  if((size > 0) && (ret_val = (void*)malloc(size)) != NULL)
     memset(ret_val, 0, size);
+
   return ret_val;
 }
Index: trunk/src/common.c
===================================================================
--- trunk/src/common.c	(revision 135)
+++ trunk/src/common.c	(revision 136)
@@ -52,7 +52,9 @@
 
   unsigned int buf_len = sizeof(char)*(len+1);
-  char* ret_val = malloc(buf_len);
+  char* ret_val = NULL; 
   char* tmp_buf;
 
+  if(buf_len > 0) 
+    ret_val = malloc(buf_len);
   if(ret_val == NULL)
     return NULL;
@@ -155,10 +157,11 @@
 {
   char* ret_val;
-  char* ascii;
+  char* ascii = NULL;
   char* tmp_err;
   int ret_err;
   *error_msg = NULL;
 
-  ascii = malloc(length+1);
+  if(length+1 > 0)
+    ascii = malloc(length+1);
   if(ascii == NULL)
   {
@@ -175,10 +178,7 @@
     free(ascii);
     tmp_err = strerror(-ret_err);
-    *error_msg = (char*)malloc(54+strlen(tmp_err));
+    *error_msg = (char*)malloc(61+strlen(tmp_err));
     if(*error_msg == NULL)
-    {
-      free(ascii);
-      return NULL;
-    }
+      return NULL;
 
     sprintf(*error_msg, 
@@ -305,7 +305,11 @@
     {
       tmp_err = strerror(-ret_err);
-      *error_msg = (char*)malloc(54+strlen(tmp_err));
+      *error_msg = (char*)malloc(61+strlen(tmp_err));
       if(*error_msg == NULL)
+      {
+	free(ascii_tmp);
 	return NULL;
+      }
+
       sprintf(*error_msg, "MULTI_SZ unicode conversion"
 	      " failed with '%s'. Quoting as binary.", tmp_err);
Index: trunk/src/reglookup-recover.c
===================================================================
--- trunk/src/reglookup-recover.c	(revision 135)
+++ trunk/src/reglookup-recover.c	(revision 136)
@@ -99,4 +99,5 @@
   if(print_parsedraw)
     free(quoted_raw);
+  free(quoted_name);
 }
 
@@ -829,5 +830,9 @@
       tmp_path = (char*)malloc(strlen(parent_paths[i])+strlen(tmp_name)+2);
       if(tmp_path == NULL)
+      {
+	free(tmp_name);
 	return 10;
+      }
+
       sprintf(tmp_path, "%s/%s", parent_paths[i], tmp_name);
       for(j=0; j < tmp_key->num_values; j++)
Index: trunk/src/reglookup.c
===================================================================
--- trunk/src/reglookup.c	(revision 135)
+++ trunk/src/reglookup.c	(revision 136)
@@ -255,4 +255,5 @@
       if((new_buf = realloc(buf, buf_len)) == NULL)
       {
+	free(name);
 	free(buf);
 	free(iter);
@@ -372,4 +373,5 @@
   const REGFI_NK_REC* sub = NULL;
   char* path = NULL;
+  char* msgs = NULL;
   int key_type = regfi_type_str2val("KEY");
   bool print_this = true;
@@ -377,5 +379,7 @@
   root = cur = regfi_iterator_cur_key(iter);
   sub = regfi_iterator_first_subkey(iter);
-  
+  msgs = regfi_get_messages(iter->f);
+  if(msgs != NULL)
+    fprintf(stderr, "%s", msgs);
   if(root == NULL)
     bailOut(EX_DATAERR, "ERROR: root cannot be NULL.\n");
@@ -455,9 +459,7 @@
   /* Strip any potential value name at end of path */
   for(i=0; 
-      (path[i] != NULL) && (path[i+1] != NULL) 
-	&& (i < REGFI_MAX_DEPTH+1+1);
+      (path[i] != NULL) && (path[i+1] != NULL) && (i < REGFI_MAX_DEPTH+1);
       i++)
-    tmp_path[i] = path[i];
-
+  { tmp_path[i] = path[i]; }
   tmp_path[i] = NULL;
 
@@ -471,4 +473,5 @@
     if(print_verbose)
       fprintf(stderr, "VERBOSE: Found final path element as root key.\n");
+    free(tmp_path);
     return 2;
   }
