jcs
/amend
/amendments
/84
bile: Sync with upstream, minor fixes
jcs made amendment 84 over 3 years ago
--- bile.c	Wed Jun 15 09:11:17 2022
+++ bile.c	Wed Aug 17 10:17:27 2022
@@ -15,6 +15,7 @@
  */
 
 #include <string.h>
+#include <stdio.h>
 #include "bile.h"
 #include "util.h"
 
@@ -115,6 +116,7 @@ bile_create(const Str255 filename, short vrefnum, cons
 	
 create_bail:
 	FSClose(bile->frefnum);
+	bile->frefnum = -1;
 	if (bile != NULL)
 		free(bile);
 	return NULL;
@@ -192,6 +194,7 @@ bile_open(const Str255 filename, short vrefnum)
 	
 open_bail:
 	FSClose(bile->frefnum);
+	bile->frefnum = -1;
 	if (bile != NULL)
 		free(bile);
 	return NULL;
@@ -258,6 +261,7 @@ bile_close(struct bile *bile)
 	_bile_error = 0;
 
 	FSClose(bile->frefnum);
+	bile->frefnum = -1;
 	if (bile->map != NULL)
 		free(bile->map);
 }
@@ -300,12 +304,16 @@ bile_count_by_type(struct bile *bile, const OSType typ
 }
 
 size_t
-bile_sorted_ids_by_type(struct bile *bile, const OSType type, size_t **ret)
+bile_sorted_ids_by_type(struct bile *bile, const OSType type,
+  unsigned long **ret)
 {
 	struct bile_object *o;
-	size_t count = 0, size = 0, n, j, t;
-	size_t *ids;
+	size_t count, size = 0, n, j, t;
+	unsigned long *ids = NULL;
 	
+	count = 0;
+	*ret = NULL;
+	
 	bile_check_sanity(bile);
 
 	for (n = 0; n < bile->nobjects; n++) {
@@ -333,7 +341,7 @@ bile_sorted_ids_by_type(struct bile *bile, const OSTyp
 }
 
 struct bile_object *
-bile_get_nth_of_type(struct bile *bile, const size_t index,
+bile_get_nth_of_type(struct bile *bile, const unsigned long index,
   const OSType type)
 {
 	struct bile_object *o, *ocopy;
@@ -359,11 +367,12 @@ bile_get_nth_of_type(struct bile *bile, const size_t i
 	return NULL;
 }
 
-size_t
+unsigned long
 bile_next_id(struct bile *bile, const OSType type)
 {
 	struct bile_object *o;
-	size_t n, id = 1;
+	size_t n;
+	unsigned long id = 1;
 	unsigned long highest;
 
 	bile_check_sanity(bile);
@@ -696,7 +705,8 @@ iterate_fields:
 short
 bile_unmarshall_object(struct bile *bile,
   const struct bile_object_field *fields, const size_t nfields,
-  const void *data, const size_t size, void *object, bool deep)
+  const void *data, const size_t data_size, void *object,
+  const size_t object_size, bool deep)
 {
 	size_t off, fsize = 0, n;
 	char *ptr, *dptr;
@@ -711,13 +721,17 @@ bile_unmarshall_object(struct bile *bile,
 		} else
 			fsize = fields[n].size;
 		
-		if (off + fsize > size)
+		if (off + fsize > data_size)
 			panic("bile_unmarshall_object: overflow at field %lu of %lu!",
 			  n + 1, nfields);
 		
 		ptr = (char *)object + fields[n].struct_off;
-		
+
 		if (fields[n].size < 0 && deep) {
+			if (fsize == 0) {
+				memset(ptr, 0, sizeof(dptr));
+				continue;
+			}
 			dptr = xmalloc(fsize);
 			memcpy(ptr, &dptr, sizeof(dptr));
 			ptr = dptr;
@@ -725,15 +739,20 @@ bile_unmarshall_object(struct bile *bile,
 		
 		if (fields[n].size < 0 && !deep)
 			memset(ptr, 0, sizeof(dptr));
-		else
+		else {
+			if (fields[n].size > 0 &&
+			  fields[n].struct_off + fsize > object_size)
+				panic("bile_unmarshall_object: overflow writing to object "
+				  "at field %lu! (%lu > %lu)", n + 1,
+				  fields[n].struct_off + fsize, object_size);
 			memcpy(ptr, (char *)data + off, fsize);
+		}
 			
 		off += fsize;
 	}
 	
 	return 0;
 }
-
 
 short
 bile_verify(struct bile *bile)
--- bile.h	Tue Jun 14 22:22:07 2022
+++ bile.h	Wed Aug 17 10:16:55 2022
@@ -114,10 +114,10 @@ struct bile_object *	bile_find(struct bile *bile, cons
 size_t					bile_count_by_type(struct bile *bile,
 						  const OSType type);
 size_t					bile_sorted_ids_by_type(struct bile *bile,
-						  const OSType type, size_t **ret);
+						  const OSType type, unsigned long **ret);
 struct bile_object *	bile_get_nth_of_type(struct bile *bile,
-						  const size_t index, const OSType type);
-size_t					bile_next_id(struct bile *bile, const OSType type);
+						  const unsigned long index, const OSType type);
+unsigned long			bile_next_id(struct bile *bile, const OSType type);
 short					bile_delete(struct bile *bile, const OSType type,
 						  const unsigned long id);
 size_t					bile_read_object(struct bile *bile,
@@ -141,6 +141,7 @@ short					bile_marshall_object(struct bile *bile,
 short					bile_unmarshall_object(struct bile *bile,
 						  const struct bile_object_field *fields,
 						  const size_t nfields, const void *data,
-						  const size_t size, void *object, bool deep);
+						  const size_t data_size, void *object,
+						  const size_t object_size, bool deep);
 
 #endif