jcs
/subtext
/amendments
/102
bile: Let negative size of marshalled field be a multiplier
Instead of just using -1, multiply the size taken from the other field
by the negative value of this size, like -4 for an array of longs with
the number of longs stored in the other field
jcs made amendment 102 over 2 years ago
--- bile.c Thu Mar 3 16:13:09 2022
+++ bile.c Wed May 18 10:23:37 2022
@@ -600,15 +600,19 @@ bile_marshall_object(struct bile *bile,
iterate_fields:
for (n = 0; n < nfields; n++) {
- if (fields[n].size == -1) {
- /* dynamically-sized field, get length from its length field */
+ if (fields[n].size < 0) {
+ /*
+ * Dynamically-sized field, get length from its length field
+ * and multiply by negative size, so -1 is the actual length but
+ * -4 could be used if length field is number of longs
+ */
ptr = (char *)object + fields[n].object_len_off;
- fsize = *(size_t *)ptr;
+ fsize = *(size_t *)ptr * -(fields[n].size);
} else
fsize = fields[n].size;
if (write) {
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
/* field is dynamically allocated, first write size */
memcpy(data + size, &fsize, sizeof(fsize));
size += sizeof(fsize);
@@ -617,14 +621,14 @@ iterate_fields:
}
ptr = (char *)object + fields[n].struct_off;
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
ptr = (char *)*(unsigned long *)ptr;
if (ptr == 0)
panic("bile_pack_object field[%lu] points to NULL", n);
}
memcpy(data + size, ptr, fsize);
- } else if (fields[n].size == -1) {
+ } else if (fields[n].size < 0) {
/* account for dynamic field length */
size += sizeof(fsize);
}
@@ -654,9 +658,10 @@ bile_unmarshall_object(struct bile *bile,
char *ptr, *dptr;
for (off = 0, n = 0; n < nfields; n++) {
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
/* dynamically-sized field, read length */
memcpy(&fsize, data + off, sizeof(fsize));
+ fsize *= -(fields[n].size);
off += sizeof(fsize);
} else
fsize = fields[n].size;
@@ -666,7 +671,7 @@ bile_unmarshall_object(struct bile *bile,
ptr = (char *)object + fields[n].struct_off;
- if (fields[n].size == -1) {
+ if (fields[n].size < 0) {
dptr = xmalloc(fsize);
memcpy(ptr, &dptr, sizeof(dptr));
ptr = dptr;