Index: trunk/lib/range_list.c
===================================================================
--- trunk/lib/range_list.c	(revision 147)
+++ trunk/lib/range_list.c	(revision 148)
@@ -18,5 +18,4 @@
  */
 
-#include <math.h>
 #include "range_list.h"
 
@@ -27,5 +26,5 @@
 #define RANGE_LIST_ALLOC_SIZE 256
 
-#if 0
+#if 0 /* For debugging */
 #include <stdio.h>
 static void range_list_print(const range_list* rl)
@@ -53,7 +52,6 @@
   if(rl->size == rl->elem_alloced)
   {
-    tmp = (range_list_element**)realloc(rl->elements, 
-					(rl->elem_alloced+RANGE_LIST_ALLOC_SIZE)
-					* sizeof(range_list_element*));
+    tmp = talloc_realloc(rl, rl->elements, range_list_element*, 
+			 (rl->elem_alloced+RANGE_LIST_ALLOC_SIZE));
     if(tmp == NULL)
       return false;
@@ -125,14 +123,12 @@
   range_list* rl;
 
-  rl = (range_list*)malloc(sizeof(range_list));
+  rl = talloc(NULL, range_list);
   if(rl == NULL)
     return NULL;
 
-  rl->elements = (range_list_element**)malloc(sizeof(range_list_element*)
-					      * RANGE_LIST_ALLOC_SIZE);
-
+  rl->elements = talloc_array(rl, range_list_element*, RANGE_LIST_ALLOC_SIZE);
   if(rl->elements == NULL)
   {
-    free(rl);
+    talloc_free(rl);
     return NULL;
   }
@@ -147,14 +143,8 @@
 void range_list_free(range_list* rl)
 {
-  uint32_t i;
-
   if(rl == NULL)
     return;
 
-  for(i=0; i < rl->size; i++)
-    free(rl->elements[i]);
-
-  free(rl->elements);
-  free(rl);
+  talloc_free(rl);
 }
 
@@ -201,5 +191,5 @@
     return false;
 
-  elem = (range_list_element*)malloc(sizeof(range_list_element));
+  elem = talloc(rl->elements, range_list_element);
   if(elem == NULL)
     return false;
@@ -210,5 +200,5 @@
   if(!range_list_insert(rl, elem, insert_index))
   {
-    free(elem);
+    talloc_free(elem);
     return false;
   }
@@ -226,5 +216,5 @@
     return false;
 
-  free(rl->elements[index]);
+  talloc_free(rl->elements[index]);
 
   /* Do the shuffle to the left. */
@@ -237,7 +227,6 @@
   if(rl->size + 2 * RANGE_LIST_ALLOC_SIZE  < rl->elem_alloced)
   {
-    tmp = (range_list_element**)realloc(rl->elements, 
-					(rl->elem_alloced-2*RANGE_LIST_ALLOC_SIZE)
-					* sizeof(range_list_element*));
+    tmp = talloc_realloc(rl, rl->elements, range_list_element*, 
+			 (rl->elem_alloced-2*RANGE_LIST_ALLOC_SIZE));
     if(tmp != NULL)
     {
@@ -305,5 +294,5 @@
     return false;
 
-  new_elem = (range_list_element*)malloc(sizeof(range_list_element));
+  new_elem = talloc(rl->elements, range_list_element);
   if(new_elem == NULL)
     return false;
@@ -315,5 +304,5 @@
   if(!range_list_insert(rl, new_elem, index+1))
   {
-    free(new_elem);
+    talloc_free(new_elem);
     return false;
   }
Index: trunk/lib/regfi.c
===================================================================
--- trunk/lib/regfi.c	(revision 147)
+++ trunk/lib/regfi.c	(revision 148)
@@ -343,9 +343,9 @@
       {
 	ret_val = tmp_val;
-	size += snprintf(ret_val+size, extra, "%s%s%c%s%c%s%c%s",
-			 ace_delim,sid_str,
-			 field_delim,type_str,
-			 field_delim,perms_str,
-			 field_delim,flags_str);
+	size += sprintf(ret_val+size, "%s%s%c%s%c%s%c%s",
+			ace_delim,sid_str,
+			field_delim,type_str,
+			field_delim,perms_str,
+			field_delim,flags_str);
 	ace_delim = "|";
       }
@@ -1279,8 +1279,9 @@
   {
     rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin);
+    if(rla)
+      talloc_steal(rb->hbins, hbin);
     hbin_off = hbin->file_off + hbin->block_size;
     hbin = regfi_parse_hbin(rb, hbin_off, true);
   }
-
 
   /* This secret isn't very secret, but we don't need a good one.  This 
@@ -1301,10 +1302,9 @@
 
 
-/*******************************************************************
- *******************************************************************/
+/******************************************************************************
+ ******************************************************************************/
 int regfi_close(REGFI_FILE *file)
 {
   int fd;
-  uint32 i;
 
   /* nothing to do if there is no open file */
@@ -1314,13 +1314,11 @@
   fd = file->fd;
   file->fd = -1;
-  for(i=0; i < range_list_size(file->hbins); i++)
-    free(range_list_get(file->hbins, i)->data);
+
   range_list_free(file->hbins);
 
-  
   if(file->sk_cache != NULL)
     lru_cache_destroy(file->sk_cache);
+
   free(file);
-
   return close(fd);
 }
@@ -1330,5 +1328,5 @@
  * There should be only *one* root key in the registry file based 
  * on my experience.  --jerry
- *****************************************************************************/
+ ******************************************************************************/
 REGFI_NK_REC* regfi_rootkey(REGFI_FILE *file)
 {
@@ -1791,10 +1789,8 @@
 
 
-/*******************************************************************
+/******************************************************************************
  * Given real file offset, read and parse the hbin at that location
  * along with it's associated cells.
- *******************************************************************/
-/* XXX: Need a way to return types of errors.
- */
+ ******************************************************************************/
 REGFI_HBIN* regfi_parse_hbin(REGFI_FILE* file, uint32 offset, bool strict)
 {
@@ -1825,5 +1821,6 @@
   }
 
-  if(!(hbin = (REGFI_HBIN*)zalloc(sizeof(REGFI_HBIN)))) 
+  hbin = talloc(NULL, REGFI_HBIN);
+  if(hbin == NULL)
     return NULL;
   hbin->file_off = offset;
@@ -1836,5 +1833,5 @@
 		      " 0x%.8X.", hbin->magic[0], hbin->magic[1], 
 		      hbin->magic[2], hbin->magic[3], offset);
-    free(hbin);
+    talloc_free(hbin);
     return NULL;
   }
@@ -1858,5 +1855,5 @@
 		      " or runs off the end of the file"
 		      " while parsing hbin at offset 0x%.8X.", offset);
-    free(hbin);
+    talloc_free(hbin);
     return NULL;
   }
Index: trunk/lib/winsec.c
===================================================================
--- trunk/lib/winsec.c	(revision 147)
+++ trunk/lib/winsec.c	(revision 148)
@@ -55,8 +55,5 @@
   if (buf == NULL || buf_len <  WINSEC_DESC_HEADER_SIZE)
     return NULL;
-  /*
-  if((ret_val = (WINSEC_DESC*)zalloc(sizeof(WINSEC_DESC))) == NULL)
-    return NULL;
-  */
+
   if((ret_val = talloc(talloc_ctx, WINSEC_DESC)) == NULL)
     return NULL;
@@ -86,5 +83,7 @@
   }
 
-  if(ret_val->off_owner_sid != 0)
+  if(ret_val->off_owner_sid == 0)
+    ret_val->owner_sid = NULL;
+  else
   {
     ret_val->owner_sid = winsec_parse_dom_sid(ret_val, 
@@ -98,5 +97,7 @@
   }
 
-  if (ret_val->off_grp_sid != 0) 
+  if(ret_val->off_grp_sid == 0) 
+    ret_val->grp_sid = NULL;
+  else
   {
     ret_val->grp_sid = winsec_parse_dom_sid(ret_val, buf + ret_val->off_grp_sid,
@@ -109,5 +110,5 @@
   }
 
-  if ((ret_val->control & WINSEC_DESC_SACL_PRESENT) && ret_val->off_sacl)
+  if((ret_val->control & WINSEC_DESC_SACL_PRESENT) && ret_val->off_sacl)
   {
     ret_val->sacl = winsec_parse_acl(ret_val, buf + ret_val->off_sacl,
@@ -119,6 +120,8 @@
     }
   }
-
-  if ((ret_val->control & WINSEC_DESC_DACL_PRESENT) && ret_val->off_dacl != 0) 
+  else
+    ret_val->sacl = NULL;
+
+  if((ret_val->control & WINSEC_DESC_DACL_PRESENT) && ret_val->off_dacl != 0) 
   {
     ret_val->dacl = winsec_parse_acl(ret_val, buf + ret_val->off_dacl,
@@ -130,4 +133,6 @@
     }
   }
+  else
+    ret_val->dacl = NULL;
 
   return ret_val;
@@ -150,8 +155,5 @@
   if (buf == NULL || buf_len < 8)
     return NULL;
-  /*
-  if((ret_val = (WINSEC_ACL*)zalloc(sizeof(WINSEC_ACL))) == NULL)
-    return NULL;
-  */
+
   if((ret_val = talloc(talloc_ctx, WINSEC_ACL)) == NULL)
     return NULL;
@@ -175,7 +177,4 @@
    * (allow no access).
    */
-  /*  if((ret_val->aces = (WINSEC_ACE**)zcalloc(sizeof(WINSEC_ACE*),
-					   ret_val->num_aces+1)) == NULL)
-  */
   if((ret_val->aces = talloc_array(ret_val, WINSEC_ACE*, 
 				   ret_val->num_aces+1)) == NULL)
@@ -203,4 +202,5 @@
     }
   }
+  ret_val->aces[ret_val->num_aces] = NULL;
 
   return ret_val;
@@ -219,6 +219,4 @@
   if(buf == NULL || buf_len < WINSEC_ACE_MIN_SIZE)
     return NULL;
-
-  /*  if((ret_val = (WINSEC_ACE*)zalloc(sizeof(WINSEC_ACE))) == NULL)*/
 
   if((ret_val = talloc(talloc_ctx, WINSEC_ACE)) == NULL)
@@ -249,4 +247,6 @@
       offset += sizeof(WINSEC_UUID);
     }
+    else
+      ret_val->obj_guid = NULL;
 
     if(ret_val->obj_flags & WINSEC_ACE_OBJECT_INHERITED_PRESENT)
@@ -261,4 +261,6 @@
       offset += sizeof(WINSEC_UUID);
     }
+    else
+      ret_val->inh_guid = NULL;
   }
 
@@ -322,5 +324,4 @@
     return false;
 
-  /* if((ret_val = (WINSEC_UUID*)zalloc(sizeof(WINSEC_UUID))) == NULL)*/
   if((ret_val = talloc(talloc_ctx, WINSEC_UUID)) == NULL)
     return NULL;
