diff --git a/overlay_buffer.c b/overlay_buffer.c index 78f25218..cfb278b7 100644 --- a/overlay_buffer.c +++ b/overlay_buffer.c @@ -44,11 +44,11 @@ struct overlay_buffer *_ob_new(struct __sourceloc __whence) // index an existing static buffer. // and allow other callers to use the ob_ convenience methods for reading and writing up to size bytes. -struct overlay_buffer *_ob_static(struct __sourceloc __whence, unsigned char *bytes, int size) +struct overlay_buffer *_ob_static(struct __sourceloc __whence, unsigned char *bytes, size_t size) { struct overlay_buffer *ret = emalloc_zero(sizeof(struct overlay_buffer)); if (config.debug.overlaybuffer) - DEBUGF("ob_static(bytes=%p, size=%d) return %p", bytes, size, ret); + DEBUGF("ob_static(bytes=%p, size=%zu) return %p", bytes, size, ret); if (ret == NULL) return NULL; ret->bytes = bytes; @@ -61,18 +61,18 @@ struct overlay_buffer *_ob_static(struct __sourceloc __whence, unsigned char *by // create a new overlay buffer from an existing piece of another buffer. // Both buffers will point to the same memory region. // It is up to the caller to ensure this buffer is not used after the parent buffer is freed. -struct overlay_buffer *_ob_slice(struct __sourceloc __whence, struct overlay_buffer *b, int offset, int length) +struct overlay_buffer *_ob_slice(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, size_t length) { - if (offset+length > b->allocSize) { + if (offset + length > b->allocSize) { WHY("Buffer isn't long enough to slice"); return NULL; } struct overlay_buffer *ret = emalloc_zero(sizeof(struct overlay_buffer)); if (config.debug.overlaybuffer) - DEBUGF("ob_slice(b=%p, offset=%d, length=%d) return %p", b, offset, length, ret); + DEBUGF("ob_slice(b=%p, offset=%zu, length=%zu) return %p", b, offset, length, ret); if (ret == NULL) return NULL; - ret->bytes = b->bytes+offset; + ret->bytes = b->bytes + offset; ret->allocSize = length; ret->allocated = NULL; ob_unlimitsize(ret); @@ -91,9 +91,11 @@ struct overlay_buffer *_ob_dup(struct __sourceloc __whence, struct overlay_buffe ret->checkpointLength = b->checkpointLength; if (b->bytes && b->allocSize){ // duplicate any bytes that might be relevant - int byteCount = b->sizeLimit; - if (byteCount < b->position) - byteCount = b->position; + size_t byteCount = b->position; + if (b->sizeLimit != SIZE_MAX) { + assert(b->position <= b->sizeLimit); + byteCount = b->sizeLimit; + } if (byteCount > b->allocSize) byteCount = b->allocSize; if (byteCount) @@ -117,7 +119,7 @@ int _ob_checkpoint(struct __sourceloc __whence, struct overlay_buffer *b) assert(b != NULL); b->checkpointLength = b->position; if (config.debug.overlaybuffer) - DEBUGF("ob_checkpoint(b=%p) checkpointLength=%d", b, b->checkpointLength); + DEBUGF("ob_checkpoint(b=%p) checkpointLength=%zu", b, b->checkpointLength); return 0; } @@ -126,30 +128,29 @@ int _ob_rewind(struct __sourceloc __whence, struct overlay_buffer *b) assert(b != NULL); b->position = b->checkpointLength; if (config.debug.overlaybuffer) - DEBUGF("ob_rewind(b=%p) position=%d", b, b->position); + DEBUGF("ob_rewind(b=%p) position=%zu", b, b->position); return 0; } -void _ob_limitsize(struct __sourceloc __whence, struct overlay_buffer *b, int bytes) +void _ob_limitsize(struct __sourceloc __whence, struct overlay_buffer *b, size_t bytes) { assert(b != NULL); - assert(bytes >= 0); - assert(b->position >= 0); + assert(bytes != SIZE_MAX); assert(b->position <= bytes); assert(b->checkpointLength <= bytes); if (b->bytes && b->allocated == NULL) assert(bytes <= b->allocSize); b->sizeLimit = bytes; if (config.debug.overlaybuffer) - DEBUGF("ob_limitsize(b=%p, bytes=%d) sizeLimit=%d", b, bytes, b->sizeLimit); + DEBUGF("ob_limitsize(b=%p, bytes=%zu) sizeLimit=%zu", b, bytes, b->sizeLimit); } void _ob_unlimitsize(struct __sourceloc __whence, struct overlay_buffer *b) { assert(b != NULL); - b->sizeLimit = -1; + b->sizeLimit = SIZE_MAX; if (config.debug.overlaybuffer) - DEBUGF("ob_unlimitsize(b=%p) sizeLimit=%d", b, b->sizeLimit); + DEBUGF("ob_unlimitsize(b=%p) sizeLimit=%zu", b, b->sizeLimit); } void _ob_flip(struct __sourceloc __whence, struct overlay_buffer *b) @@ -167,17 +168,15 @@ ssize_t _ob_makespace(struct __sourceloc __whence, struct overlay_buffer *b, siz { assert(b != NULL); if (config.debug.overlaybuffer) - DEBUGF("ob_makespace(b=%p, bytes=%zd) b->bytes=%p b->position=%d b->allocSize=%d", + DEBUGF("ob_makespace(b=%p, bytes=%zd) b->bytes=%p b->position=%zu b->allocSize=%zu", b, bytes, b->bytes, b->position, b->allocSize); - assert(b->position >= 0); - if (b->sizeLimit != -1) - assert(b->sizeLimit >= 0); - assert(b->allocSize >= 0); + assert(b->position <= b->sizeLimit); + assert(b->position <= b->allocSize); if (b->position) assert(b->bytes != NULL); - if (b->sizeLimit != -1 && b->position + bytes > b->sizeLimit) { + if (b->position + bytes > b->sizeLimit) { if (config.debug.packetformats) - DEBUGF("ob_makespace(): asked for space to %zu, beyond size limit of %u", b->position + bytes, b->sizeLimit); + DEBUGF("ob_makespace(): asked for space to %zu, beyond size limit of %zu", b->position + bytes, b->sizeLimit); return 0; } if (b->position + bytes <= b->allocSize) @@ -185,10 +184,10 @@ ssize_t _ob_makespace(struct __sourceloc __whence, struct overlay_buffer *b, siz // Don't realloc a static buffer. if (b->bytes && b->allocated == NULL) { if (config.debug.packetformats) - DEBUGF("ob_makespace(): asked for space to %zu, beyond static buffer size of %u", b->position + bytes, b->allocSize); + DEBUGF("ob_makespace(): asked for space to %zu, beyond static buffer size of %zu", b->position + bytes, b->allocSize); return 0; } - int newSize=b->position+bytes; + size_t newSize = b->position + bytes; if (newSize<64) newSize=64; if (newSize&63) newSize+=64-(newSize&63); if (newSize>1024 && (newSize&1023)) @@ -196,7 +195,7 @@ ssize_t _ob_makespace(struct __sourceloc __whence, struct overlay_buffer *b, siz if (newSize>65536 && (newSize&65535)) newSize+=65536-(newSize&65535); if (config.debug.overlaybuffer) - DEBUGF("realloc(b->bytes=%p,newSize=%d)", b->bytes,newSize); + DEBUGF("realloc(b->bytes=%p, newSize=%zu)", b->bytes, newSize); /* XXX OSX realloc() seems to be able to corrupt things if the heap is not happy when calling realloc(), making debugging memory corruption much harder. So will do a three-stage malloc,bcopy,free to see if we can tease bugs out that way. */ /* @@ -247,35 +246,35 @@ void _ob_append_byte(struct __sourceloc __whence, struct overlay_buffer *b, unsi if (ob_makespace(b, bytes)) { b->bytes[b->position] = byte; if (config.debug.overlaybuffer) - DEBUGF("ob_append_byte(b=%p, byte=0x%02x) %p[%d]=%02x position=%d", b, byte, b->bytes, b->position, byte, b->position + bytes); + DEBUGF("ob_append_byte(b=%p, byte=0x%02x) %p[%d]=%02x position=%zu", b, byte, b->bytes, b->position, byte, b->position + bytes); } else { if (config.debug.overlaybuffer) - DEBUGF("ob_append_byte(b=%p, byte=0x%02x) OVERRUN position=%d", b, byte, b->position + bytes); + DEBUGF("ob_append_byte(b=%p, byte=0x%02x) OVERRUN position=%zu", b, byte, b->position + bytes); } b->position += bytes; } -unsigned char *_ob_append_space(struct __sourceloc __whence, struct overlay_buffer *b, int count) +unsigned char *_ob_append_space(struct __sourceloc __whence, struct overlay_buffer *b, size_t count) { assert(count > 0); unsigned char *r = ob_makespace(b, count) ? &b->bytes[b->position] : NULL; b->position += count; if (config.debug.overlaybuffer) - DEBUGF("ob_append_space(b=%p, count=%d) position=%d return %p", b, count, b->position, r); + DEBUGF("ob_append_space(b=%p, count=%zu) position=%zu return %p", b, count, b->position, r); return r; } -void _ob_append_bytes(struct __sourceloc __whence, struct overlay_buffer *b, const unsigned char *bytes, int count) +void _ob_append_bytes(struct __sourceloc __whence, struct overlay_buffer *b, const unsigned char *bytes, size_t count) { assert(count > 0); unsigned char *r = ob_makespace(b, count) ? &b->bytes[b->position] : NULL; if (r) { bcopy(bytes, r, count); if (config.debug.overlaybuffer) - DEBUGF("ob_append_bytes(b=%p, bytes=%p, count=%d) position=%d return %p", b, bytes, count, b->position + count, r); + DEBUGF("ob_append_bytes(b=%p, bytes=%p, count=%zu) position=%zu return %p", b, bytes, count, b->position + count, r); } else { if (config.debug.overlaybuffer) - DEBUGF("ob_append_bytes(b=%p, bytes=%p, count=%d) OVERRUN position=%d return NULL", b, bytes, count, b->position + count); + DEBUGF("ob_append_bytes(b=%p, bytes=%p, count=%zu) OVERRUN position=%zu return NULL", b, bytes, count, b->position + count); } if (config.debug.overlaybuffer) dump("ob_append_bytes", bytes, count); @@ -294,10 +293,10 @@ void _ob_append_ui16(struct __sourceloc __whence, struct overlay_buffer *b, uint b->bytes[b->position] = (v >> 8) & 0xFF; b->bytes[b->position+1] = v & 0xFF; if (config.debug.overlaybuffer) - DEBUGF("ob_append_ui16(b=%p, v=%u) %p[%d]=%s position=%d", b, v, b->bytes, b->position, alloca_tohex(&b->bytes[b->position], bytes), b->position + bytes); + DEBUGF("ob_append_ui16(b=%p, v=%u) %p[%d]=%s position=%zu", b, v, b->bytes, b->position, alloca_tohex(&b->bytes[b->position], bytes), b->position + bytes); } else { if (config.debug.overlaybuffer) - DEBUGF("ob_append_ui16(b=%p, v=%u) OVERRUN position=%d", b, v, b->position + bytes); + DEBUGF("ob_append_ui16(b=%p, v=%u) OVERRUN position=%zu", b, v, b->position + bytes); } b->position += bytes; } @@ -311,11 +310,11 @@ void _ob_append_ui32(struct __sourceloc __whence, struct overlay_buffer *b, uint b->bytes[b->position+2] = (v >> 8) & 0xFF; b->bytes[b->position+3] = v & 0xFF; if (config.debug.overlaybuffer) - DEBUGF("ob_append_ui32(b=%p, v=%"PRIu32") %p[%d]=%s position=%d", + DEBUGF("ob_append_ui32(b=%p, v=%"PRIu32") %p[%d]=%s position=%zu", b, v, b->bytes, b->position, alloca_tohex(&b->bytes[b->position], bytes), b->position + bytes); } else { if (config.debug.overlaybuffer) - DEBUGF("ob_append_ui32(b=%p, v=%"PRIu32") OVERRUN position=%d", b, v, b->position + bytes); + DEBUGF("ob_append_ui32(b=%p, v=%"PRIu32") OVERRUN position=%zu", b, v, b->position + bytes); } b->position += bytes; } @@ -333,11 +332,11 @@ void _ob_append_ui64(struct __sourceloc __whence, struct overlay_buffer *b, uint b->bytes[b->position+6] = (v >> 8) & 0xFF; b->bytes[b->position+7] = v & 0xFF; if (config.debug.overlaybuffer) - DEBUGF("ob_append_ui64(b=%p, v=%"PRIu64") %p[%d]=%s position=%d", + DEBUGF("ob_append_ui64(b=%p, v=%"PRIu64") %p[%d]=%s position=%zu", b, v, b->bytes, b->position, alloca_tohex(&b->bytes[b->position], bytes), b->position + bytes); } else { if (config.debug.overlaybuffer) - DEBUGF("ob_append_ui64(b=%p, v=%"PRIu64") OVERRUN position=%d", b, v, b->position + bytes); + DEBUGF("ob_append_ui64(b=%p, v=%"PRIu64") OVERRUN position=%zu", b, v, b->position + bytes); } b->position += bytes; } @@ -407,19 +406,19 @@ void _ob_append_rfs(struct __sourceloc __whence, struct overlay_buffer *b, int l // make sure a range of bytes is valid for reading -int test_offset(struct overlay_buffer *b,int start,int length) +static int test_offset(struct overlay_buffer *b, size_t length) { - if (!b) return -1; - if (start<0) return -1; - if (b->sizeLimit>=0 && start+length>b->sizeLimit) return -1; - if (start+length>b->allocSize) return -1; + if (b->position + length > b->sizeLimit) + return -1; + if (b->position + length > b->allocSize) + return -1; return 0; } // next byte without advancing int ob_peek(struct overlay_buffer *b) { - if (test_offset(b, b->position, 1)) + if (test_offset(b, 1)) return -1; return b->bytes[b->position]; } @@ -429,19 +428,19 @@ void ob_skip(struct overlay_buffer *b, unsigned n) b->position += n; } -int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, int len){ - if (test_offset(b, b->position, len)) +int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, size_t len) +{ + if (test_offset(b, len)) return -1; - bcopy(b->bytes + b->position, buff, len); b->position+=len; return 0; } -unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, int len){ - if (test_offset(b, b->position, len)) +unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, size_t len) +{ + if (test_offset(b, len)) return NULL; - unsigned char *ret = b->bytes + b->position; b->position+=len; return ret; @@ -449,9 +448,8 @@ unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, int len){ uint32_t ob_get_ui32(struct overlay_buffer *b) { - if (test_offset(b, b->position, 4)) + if (test_offset(b, 4)) return 0xFFFFFFFF; // ... unsigned - uint32_t ret = b->bytes[b->position] << 24 | b->bytes[b->position +1] << 16 | b->bytes[b->position +2] << 8 @@ -462,9 +460,8 @@ uint32_t ob_get_ui32(struct overlay_buffer *b) uint64_t ob_get_ui64(struct overlay_buffer *b) { - if (test_offset(b, b->position, 8)) + if (test_offset(b, 8)) return 0xFFFFFFFF; // ... unsigned - uint64_t ret = (uint64_t)b->bytes[b->position] << 56 | (uint64_t)b->bytes[b->position +1] << 48 | (uint64_t)b->bytes[b->position +2] << 40 @@ -479,9 +476,8 @@ uint64_t ob_get_ui64(struct overlay_buffer *b) uint16_t ob_get_ui16(struct overlay_buffer *b) { - if (test_offset(b, b->position, 2)) + if (test_offset(b, 2)) return 0xFFFF; // ... unsigned - uint16_t ret = b->bytes[b->position] << 8 | b->bytes[b->position +1]; b->position+=2; @@ -520,18 +516,16 @@ uint64_t ob_get_packed_ui64(struct overlay_buffer *b) int ob_get(struct overlay_buffer *b) { - if (test_offset(b, b->position, 1)) + if (test_offset(b, 1)) return -1; return b->bytes[b->position++]; } -void _ob_set_ui16(struct __sourceloc __whence, struct overlay_buffer *b, int offset, uint16_t v) +void _ob_set_ui16(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, uint16_t v) { const int bytes = 2; assert(b != NULL); - assert(offset >= 0); - if (b->sizeLimit != -1) - assert(offset + bytes <= b->sizeLimit); + assert(offset + bytes <= b->sizeLimit); assert(offset + bytes <= b->allocSize); b->bytes[offset] = (v >> 8) & 0xFF; b->bytes[offset+1] = v & 0xFF; @@ -539,13 +533,11 @@ void _ob_set_ui16(struct __sourceloc __whence, struct overlay_buffer *b, int off DEBUGF("ob_set_ui16(b=%p, offset=%d, v=%u) %p[%d]=%s", b, offset, v, b->bytes, offset, alloca_tohex(&b->bytes[offset], bytes)); } -void _ob_set(struct __sourceloc __whence, struct overlay_buffer *b, int offset, unsigned char byte) +void _ob_set(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, unsigned char byte) { const int bytes = 1; assert(b != NULL); - assert(offset >= 0); - if (b->sizeLimit != -1) - assert(offset + bytes <= b->sizeLimit); + assert(offset + bytes <= b->sizeLimit); assert(offset + bytes <= b->allocSize); b->bytes[offset] = byte; if (config.debug.overlaybuffer) @@ -558,28 +550,26 @@ void _ob_patch_rfs(struct __sourceloc __whence, struct overlay_buffer *b) } -int ob_position(struct overlay_buffer *b) +size_t ob_position(struct overlay_buffer *b) { return b->position; } -int ob_limit(struct overlay_buffer *b) +size_t ob_limit(struct overlay_buffer *b) { return b->sizeLimit; } -int ob_remaining(struct overlay_buffer *b) +size_t ob_remaining(struct overlay_buffer *b) { - assert(b->sizeLimit != -1); - return b->sizeLimit - b->position; + assert(b->sizeLimit != SIZE_MAX); + assert(b->position <= b->sizeLimit); + return (size_t)(b->sizeLimit - b->position); } int _ob_overrun(struct __sourceloc __whence, struct overlay_buffer *b) { - assert(b->allocSize >= 0); - if (b->sizeLimit != -1) - assert(b->sizeLimit >= 0); - int ret = b->position > (b->sizeLimit != -1 && b->sizeLimit < b->allocSize ? b->sizeLimit : b->allocSize); + int ret = b->position > (b->sizeLimit != SIZE_MAX && b->sizeLimit < b->allocSize ? b->sizeLimit : b->allocSize); if (config.debug.overlaybuffer) DEBUGF("ob_overrun(b=%p) return %d", b, ret); return ret; @@ -599,8 +589,13 @@ int asprintable(int c) int ob_dump(struct overlay_buffer *b, char *desc) { - DEBUGF("overlay_buffer '%s' at %p (%p) : position=%d, size=%d", desc, b, b->bytes, b->position, b->sizeLimit); - if (b->bytes && (b->position || b->sizeLimit)) - dump(desc, b->bytes, b->position?b->position:b->sizeLimit); + DEBUGF("overlay_buffer '%s' at %p (%p) : position=%zu, size=%zu", desc, b, b->bytes, b->position, b->sizeLimit); + if (b->bytes) { + if (b->sizeLimit != SIZE_MAX && b->sizeLimit > 0) { + assert(b->position <= b->sizeLimit); + dump(desc, b->bytes, b->sizeLimit); + } else if (b->position > 0) + dump(desc, b->bytes, b->position); + } return 0; } diff --git a/overlay_buffer.h b/overlay_buffer.h index 7aa3c5e4..a9cb4df9 100644 --- a/overlay_buffer.h +++ b/overlay_buffer.h @@ -24,43 +24,43 @@ struct overlay_buffer { unsigned char *bytes; // remembered position for rewinding - int checkpointLength; + size_t checkpointLength; // position of data read / written - int position; + size_t position; // maximum allowed bytes for reading / writing - int sizeLimit; + size_t sizeLimit; // size of buffer - int allocSize; + size_t allocSize; // is this an allocated buffer? can it be resized? Should it be freed? unsigned char * allocated; // length position for later patching - int var_length_offset; + size_t var_length_offset; }; struct overlay_buffer *_ob_new(struct __sourceloc __whence); -struct overlay_buffer *_ob_static(struct __sourceloc __whence, unsigned char *bytes, int size); -struct overlay_buffer *_ob_slice(struct __sourceloc __whence, struct overlay_buffer *b, int offset, int length); +struct overlay_buffer *_ob_static(struct __sourceloc __whence, unsigned char *bytes, size_t size); +struct overlay_buffer *_ob_slice(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, size_t length); struct overlay_buffer *_ob_dup(struct __sourceloc __whence, struct overlay_buffer *b); void _ob_free(struct __sourceloc __whence, struct overlay_buffer *b); int _ob_checkpoint(struct __sourceloc __whence, struct overlay_buffer *b); int _ob_rewind(struct __sourceloc __whence, struct overlay_buffer *b); -void _ob_limitsize(struct __sourceloc __whence, struct overlay_buffer *b,int bytes); +void _ob_limitsize(struct __sourceloc __whence, struct overlay_buffer *b, size_t bytes); void _ob_flip(struct __sourceloc __whence, struct overlay_buffer *b); void _ob_unlimitsize(struct __sourceloc __whence, struct overlay_buffer *b); ssize_t _ob_makespace(struct __sourceloc whence, struct overlay_buffer *b, size_t bytes); -void _ob_set(struct __sourceloc __whence, struct overlay_buffer *b, int ofs, unsigned char byte); -void _ob_set_ui16(struct __sourceloc __whence, struct overlay_buffer *b, int offset, uint16_t v); +void _ob_set(struct __sourceloc __whence, struct overlay_buffer *b, size_t ofs, unsigned char byte); +void _ob_set_ui16(struct __sourceloc __whence, struct overlay_buffer *b, size_t offset, uint16_t v); void _ob_patch_rfs(struct __sourceloc __whence, struct overlay_buffer *b); void _ob_append_byte(struct __sourceloc whence, struct overlay_buffer *b,unsigned char byte); -void _ob_append_bytes(struct __sourceloc whence, struct overlay_buffer *b,const unsigned char *bytes,int count); +void _ob_append_bytes(struct __sourceloc whence, struct overlay_buffer *b,const unsigned char *bytes, size_t count); void _ob_append_buffer(struct __sourceloc whence, struct overlay_buffer *b,struct overlay_buffer *s); -unsigned char *_ob_append_space(struct __sourceloc whence, struct overlay_buffer *b,int count); +unsigned char *_ob_append_space(struct __sourceloc whence, struct overlay_buffer *b, size_t count); void _ob_append_ui16(struct __sourceloc whence, struct overlay_buffer *b, uint16_t v); void _ob_append_ui32(struct __sourceloc whence, struct overlay_buffer *b, uint32_t v); void _ob_append_ui64(struct __sourceloc whence, struct overlay_buffer *b, uint64_t v); @@ -99,8 +99,8 @@ int ob_peek(struct overlay_buffer *b); void ob_skip(struct overlay_buffer *b, unsigned n); // get one byte from the current position, -ve number indicates failure int ob_get(struct overlay_buffer *b); -int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, int len); -unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, int len); +int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, size_t len); +unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, size_t len); uint64_t ob_get_ui64(struct overlay_buffer *b); uint32_t ob_get_ui32(struct overlay_buffer *b); uint16_t ob_get_ui16(struct overlay_buffer *b); @@ -110,9 +110,9 @@ uint32_t ob_get_packed_ui32(struct overlay_buffer *b); uint64_t ob_get_packed_ui64(struct overlay_buffer *b); // information routines -int ob_position(struct overlay_buffer *b); -int ob_limit(struct overlay_buffer *b); -int ob_remaining(struct overlay_buffer *b); +size_t ob_position(struct overlay_buffer *b); +size_t ob_limit(struct overlay_buffer *b); +size_t ob_remaining(struct overlay_buffer *b); int _ob_overrun(struct __sourceloc, struct overlay_buffer *b); unsigned char* ob_ptr(struct overlay_buffer *b); diff --git a/overlay_interface.c b/overlay_interface.c index 44239bab..5a6c821d 100644 --- a/overlay_interface.c +++ b/overlay_interface.c @@ -810,13 +810,13 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o { assert(destination && destination->interface); const unsigned char *bytes = ob_ptr(buffer); - int len = ob_position(buffer); + size_t len = ob_position(buffer); struct overlay_interface *interface = destination->interface; destination->last_tx = gettime_ms(); if (config.debug.packettx){ - DEBUGF("Sending this packet via interface %s (len=%d)",interface->name,len); + DEBUGF("Sending this packet via interface %s (len=%zu)",interface->name, len); DEBUG_packet_visualise(NULL, bytes, len); } @@ -826,7 +826,7 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o } if (interface->debug) - DEBUGF("Sending on %s, len %d: %s", interface->name, len, alloca_tohex(bytes, len>64?64:len)); + DEBUGF("Sending on %s, len %zu: %s", interface->name, len, alloca_tohex(bytes, len>64?64:len)); interface->tx_count++; @@ -842,11 +842,11 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o .pid = getpid(), }; - if (len > sizeof(packet.payload)){ + if (len > sizeof packet.payload) { WARN("Truncating long packet to fit within MTU byte limit for dummy interface"); - len = sizeof(packet.payload); + len = sizeof packet.payload; } - packet.payload_length=len; + packet.payload_length = len; bcopy(bytes, packet.payload, len); ob_free(buffer); /* This lseek() is unneccessary because the dummy file is opened in O_APPEND mode. It's @@ -886,22 +886,22 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o case SOCK_DGRAM: { if (config.debug.overlayinterfaces) - DEBUGF("Sending %zu byte overlay frame on %s to %s", (size_t)len, interface->name, inet_ntoa(destination->address.sin_addr)); + DEBUGF("Sending %zu byte overlay frame on %s to %s", len, interface->name, inet_ntoa(destination->address.sin_addr)); ssize_t sent = sendto(interface->alarm.poll.fd, bytes, (size_t)len, 0, (struct sockaddr *)&destination->address, sizeof(destination->address)); ob_free(buffer); - if (sent == -1 || (size_t)sent != (size_t)len) { + if (sent == -1 || (size_t)sent != len) { if (sent == -1) WHYF_perror("sendto(fd=%d,len=%zu,addr=%s) on interface %s", interface->alarm.poll.fd, - (size_t)len, + len, alloca_sockaddr((struct sockaddr *)&destination->address, sizeof destination->address), interface->name ); else WHYF("sendto() sent %zu bytes of overlay frame (%zu) to interface %s (socket=%d)", - (size_t)sent, (size_t)len, interface->name, interface->alarm.poll.fd); + (size_t)sent, len, interface->name, interface->alarm.poll.fd); // close the interface if we had any error while sending broadcast packets, // unicast packets should not bring the interface down if (destination == interface->destination) diff --git a/overlay_mdp.c b/overlay_mdp.c index 0b8c1538..613b0b35 100644 --- a/overlay_mdp.c +++ b/overlay_mdp.c @@ -766,7 +766,7 @@ static int overlay_send_frame( if (ob_overrun(plaintext)) { if (config.debug.mdprequests) - DEBUGF("Frame overrun: position=%d allocSize=%d sizeLimit=%d", + DEBUGF("Frame overrun: position=%zu allocSize=%zu sizeLimit=%zu", plaintext->position, plaintext->allocSize, plaintext->sizeLimit); op_free(frame); ob_free(plaintext); diff --git a/overlay_olsr.c b/overlay_olsr.c index 57ed7911..aa4c4677 100644 --- a/overlay_olsr.c +++ b/overlay_olsr.c @@ -196,7 +196,7 @@ static void parse_frame(struct overlay_buffer *buff){ frame.modifiers=ob_get(buff); if (config.debug.overlayinterfaces) - DEBUGF("Received %d byte payload via olsr", buff->sizeLimit - buff->position); + DEBUGF("Received %zu byte payload via olsr", buff->sizeLimit - buff->position); // the remaining bytes are an mdp payload, process it frame.payload = buff; @@ -297,7 +297,7 @@ int olsr_send(struct overlay_frame *frame){ ob_append_byte(b, frame->modifiers); if (config.debug.overlayinterfaces) - DEBUGF("Sending %d byte payload via olsr", frame->payload->sizeLimit); + DEBUGF("Sending %zu byte payload via olsr", frame->payload->sizeLimit); // send the packet int ret = send_packet(b->bytes, b->position, frame->payload->bytes, frame->payload->sizeLimit); diff --git a/overlay_packetformats.c b/overlay_packetformats.c index ffd18f86..1412fb44 100644 --- a/overlay_packetformats.c +++ b/overlay_packetformats.c @@ -425,7 +425,7 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s } // TODO allow for single byte length? - unsigned int payload_len; + size_t payload_len; switch (context.encapsulation){ case ENCAP_SINGLE: @@ -459,7 +459,7 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s if (header_valid!=0){ - f.payload = ob_slice(b, b->position, payload_len); + f.payload = ob_slice(b, ob_position(b), payload_len); if (!f.payload){ // out of memory? WHY("Unable to slice payload"); diff --git a/overlay_payload.c b/overlay_payload.c index 05f43215..f7059d4c 100644 --- a/overlay_payload.c +++ b/overlay_payload.c @@ -94,7 +94,7 @@ int overlay_frame_append_payload(struct decode_context *context, int encapsulati alloca_tohex_sid_t(p->destination->sid),p->type, "append_payload stuffing into packet"); if (p->payload) - dump("payload contents", &p->payload->bytes[0],p->payload->position); + dump("payload contents", &p->payload->bytes[0], ob_position(p->payload)); } struct broadcast *broadcast=NULL; diff --git a/overlay_queue.c b/overlay_queue.c index 011a0504..88f73817 100644 --- a/overlay_queue.c +++ b/overlay_queue.c @@ -335,7 +335,7 @@ overlay_stuff_packet(struct outgoing_packet *packet, overlay_txqueue *queue, tim while(frame){ if (frame->enqueued_at + queue->latencyTarget < now){ if (config.debug.overlayframes) - DEBUGF("Dropping frame type %x (length %d) for %s due to expiry timeout", + DEBUGF("Dropping frame type %x (length %zu) for %s due to expiry timeout", frame->type, frame->payload->checkpointLength, frame->destination?alloca_tohex_sid_t(frame->destination->sid):"All"); frame = overlay_queue_remove(queue, frame); diff --git a/rhizome_packetformats.c b/rhizome_packetformats.c index 5eec9688..8ed9899d 100644 --- a/rhizome_packetformats.c +++ b/rhizome_packetformats.c @@ -168,12 +168,12 @@ static int append_bars(struct overlay_buffer *e, sqlite_retry_state *retry, cons DEBUG("Found a BAR that is the wrong size - ignoring"); continue; } - if (ob_remaining(e) < blob_bytes) { + if (ob_remaining(e) < RHIZOME_BAR_BYTES) { // out of room count--; break; } - ob_append_bytes(e, (unsigned char *)data, blob_bytes); + ob_append_bytes(e, (unsigned char *)data, RHIZOME_BAR_BYTES); *last_rowid=rowid; } if (statement) diff --git a/route_link.c b/route_link.c index a8d612da..8761209b 100644 --- a/route_link.c +++ b/route_link.c @@ -488,7 +488,7 @@ static int append_link_state(struct overlay_buffer *payload, char flags, flags|=FLAG_HAS_ACK; if (drop_rate!=-1) flags|=FLAG_HAS_DROP_RATE; - int length_pos = ob_position(payload); + size_t length_pos = ob_position(payload); ob_append_byte(payload, 0); ob_append_byte(payload, flags); overlay_address_append(NULL, payload, receiver); @@ -507,7 +507,7 @@ static int append_link_state(struct overlay_buffer *payload, char flags, if (ob_overrun(payload)) return -1; // patch the record length - int end_pos = ob_position(payload); + size_t end_pos = ob_position(payload); ob_set(payload, length_pos, end_pos - length_pos); ob_checkpoint(payload); return 0; @@ -898,7 +898,7 @@ static void link_send(struct sched_ent *alarm) ob_limitsize(frame->payload, 400); overlay_mdp_encode_ports(frame->payload, MDP_PORT_LINKSTATE, MDP_PORT_LINKSTATE); ob_checkpoint(frame->payload); - int pos = ob_position(frame->payload); + size_t pos = ob_position(frame->payload); enum_subscribers(NULL, append_link, frame->payload); ob_rewind(frame->payload); if (ob_position(frame->payload) == pos) @@ -1243,7 +1243,7 @@ int link_receive(struct overlay_frame *frame, overlay_mdp_frame *mdp) struct subscriber *receiver=NULL, *transmitter=NULL; struct overlay_interface *interface = NULL; - int start_pos = ob_position(payload); + size_t start_pos = ob_position(payload); int length = ob_get(payload); if (length <=0) break;