Change overlay_buffer fields from int to size_t

Fixes many -Wsign-compare warnings
This commit is contained in:
Andrew Bettison 2013-12-10 17:03:30 +10:30
parent 6cd8df74a4
commit ebc3133f5c
10 changed files with 114 additions and 119 deletions

View File

@ -44,11 +44,11 @@ struct overlay_buffer *_ob_new(struct __sourceloc __whence)
// index an existing static buffer. // index an existing static buffer.
// and allow other callers to use the ob_ convenience methods for reading and writing up to size bytes. // 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)); struct overlay_buffer *ret = emalloc_zero(sizeof(struct overlay_buffer));
if (config.debug.overlaybuffer) 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) if (ret == NULL)
return NULL; return NULL;
ret->bytes = bytes; 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. // create a new overlay buffer from an existing piece of another buffer.
// Both buffers will point to the same memory region. // 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. // 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"); WHY("Buffer isn't long enough to slice");
return NULL; return NULL;
} }
struct overlay_buffer *ret = emalloc_zero(sizeof(struct overlay_buffer)); struct overlay_buffer *ret = emalloc_zero(sizeof(struct overlay_buffer));
if (config.debug.overlaybuffer) 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) if (ret == NULL)
return NULL; return NULL;
ret->bytes = b->bytes+offset; ret->bytes = b->bytes + offset;
ret->allocSize = length; ret->allocSize = length;
ret->allocated = NULL; ret->allocated = NULL;
ob_unlimitsize(ret); ob_unlimitsize(ret);
@ -91,9 +91,11 @@ struct overlay_buffer *_ob_dup(struct __sourceloc __whence, struct overlay_buffe
ret->checkpointLength = b->checkpointLength; ret->checkpointLength = b->checkpointLength;
if (b->bytes && b->allocSize){ if (b->bytes && b->allocSize){
// duplicate any bytes that might be relevant // duplicate any bytes that might be relevant
int byteCount = b->sizeLimit; size_t byteCount = b->position;
if (byteCount < b->position) if (b->sizeLimit != SIZE_MAX) {
byteCount = b->position; assert(b->position <= b->sizeLimit);
byteCount = b->sizeLimit;
}
if (byteCount > b->allocSize) if (byteCount > b->allocSize)
byteCount = b->allocSize; byteCount = b->allocSize;
if (byteCount) if (byteCount)
@ -117,7 +119,7 @@ int _ob_checkpoint(struct __sourceloc __whence, struct overlay_buffer *b)
assert(b != NULL); assert(b != NULL);
b->checkpointLength = b->position; b->checkpointLength = b->position;
if (config.debug.overlaybuffer) 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; return 0;
} }
@ -126,30 +128,29 @@ int _ob_rewind(struct __sourceloc __whence, struct overlay_buffer *b)
assert(b != NULL); assert(b != NULL);
b->position = b->checkpointLength; b->position = b->checkpointLength;
if (config.debug.overlaybuffer) 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; 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(b != NULL);
assert(bytes >= 0); assert(bytes != SIZE_MAX);
assert(b->position >= 0);
assert(b->position <= bytes); assert(b->position <= bytes);
assert(b->checkpointLength <= bytes); assert(b->checkpointLength <= bytes);
if (b->bytes && b->allocated == NULL) if (b->bytes && b->allocated == NULL)
assert(bytes <= b->allocSize); assert(bytes <= b->allocSize);
b->sizeLimit = bytes; b->sizeLimit = bytes;
if (config.debug.overlaybuffer) 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) void _ob_unlimitsize(struct __sourceloc __whence, struct overlay_buffer *b)
{ {
assert(b != NULL); assert(b != NULL);
b->sizeLimit = -1; b->sizeLimit = SIZE_MAX;
if (config.debug.overlaybuffer) 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) 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); assert(b != NULL);
if (config.debug.overlaybuffer) 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); b, bytes, b->bytes, b->position, b->allocSize);
assert(b->position >= 0); assert(b->position <= b->sizeLimit);
if (b->sizeLimit != -1) assert(b->position <= b->allocSize);
assert(b->sizeLimit >= 0);
assert(b->allocSize >= 0);
if (b->position) if (b->position)
assert(b->bytes != NULL); assert(b->bytes != NULL);
if (b->sizeLimit != -1 && b->position + bytes > b->sizeLimit) { if (b->position + bytes > b->sizeLimit) {
if (config.debug.packetformats) 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; return 0;
} }
if (b->position + bytes <= b->allocSize) 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. // Don't realloc a static buffer.
if (b->bytes && b->allocated == NULL) { if (b->bytes && b->allocated == NULL) {
if (config.debug.packetformats) 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; return 0;
} }
int newSize=b->position+bytes; size_t newSize = b->position + bytes;
if (newSize<64) newSize=64; if (newSize<64) newSize=64;
if (newSize&63) newSize+=64-(newSize&63); if (newSize&63) newSize+=64-(newSize&63);
if (newSize>1024 && (newSize&1023)) 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)) if (newSize>65536 && (newSize&65535))
newSize+=65536-(newSize&65535); newSize+=65536-(newSize&65535);
if (config.debug.overlaybuffer) 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. /* 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. */ 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)) { if (ob_makespace(b, bytes)) {
b->bytes[b->position] = byte; b->bytes[b->position] = byte;
if (config.debug.overlaybuffer) 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 { } else {
if (config.debug.overlaybuffer) 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; 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); assert(count > 0);
unsigned char *r = ob_makespace(b, count) ? &b->bytes[b->position] : NULL; unsigned char *r = ob_makespace(b, count) ? &b->bytes[b->position] : NULL;
b->position += count; b->position += count;
if (config.debug.overlaybuffer) 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; 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); assert(count > 0);
unsigned char *r = ob_makespace(b, count) ? &b->bytes[b->position] : NULL; unsigned char *r = ob_makespace(b, count) ? &b->bytes[b->position] : NULL;
if (r) { if (r) {
bcopy(bytes, r, count); bcopy(bytes, r, count);
if (config.debug.overlaybuffer) 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 { } else {
if (config.debug.overlaybuffer) 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) if (config.debug.overlaybuffer)
dump("ob_append_bytes", bytes, count); 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] = (v >> 8) & 0xFF;
b->bytes[b->position+1] = v & 0xFF; b->bytes[b->position+1] = v & 0xFF;
if (config.debug.overlaybuffer) 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 { } else {
if (config.debug.overlaybuffer) 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; 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+2] = (v >> 8) & 0xFF;
b->bytes[b->position+3] = v & 0xFF; b->bytes[b->position+3] = v & 0xFF;
if (config.debug.overlaybuffer) 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); b, v, b->bytes, b->position, alloca_tohex(&b->bytes[b->position], bytes), b->position + bytes);
} else { } else {
if (config.debug.overlaybuffer) 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; 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+6] = (v >> 8) & 0xFF;
b->bytes[b->position+7] = v & 0xFF; b->bytes[b->position+7] = v & 0xFF;
if (config.debug.overlaybuffer) 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); b, v, b->bytes, b->position, alloca_tohex(&b->bytes[b->position], bytes), b->position + bytes);
} else { } else {
if (config.debug.overlaybuffer) 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; 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 // 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 (b->position + length > b->sizeLimit)
if (start<0) return -1; return -1;
if (b->sizeLimit>=0 && start+length>b->sizeLimit) return -1; if (b->position + length > b->allocSize)
if (start+length>b->allocSize) return -1; return -1;
return 0; return 0;
} }
// next byte without advancing // next byte without advancing
int ob_peek(struct overlay_buffer *b) int ob_peek(struct overlay_buffer *b)
{ {
if (test_offset(b, b->position, 1)) if (test_offset(b, 1))
return -1; return -1;
return b->bytes[b->position]; return b->bytes[b->position];
} }
@ -429,19 +428,19 @@ void ob_skip(struct overlay_buffer *b, unsigned n)
b->position += n; b->position += n;
} }
int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, int len){ int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, size_t len)
if (test_offset(b, b->position, len)) {
if (test_offset(b, len))
return -1; return -1;
bcopy(b->bytes + b->position, buff, len); bcopy(b->bytes + b->position, buff, len);
b->position+=len; b->position+=len;
return 0; return 0;
} }
unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, int len){ unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, size_t len)
if (test_offset(b, b->position, len)) {
if (test_offset(b, len))
return NULL; return NULL;
unsigned char *ret = b->bytes + b->position; unsigned char *ret = b->bytes + b->position;
b->position+=len; b->position+=len;
return ret; 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) uint32_t ob_get_ui32(struct overlay_buffer *b)
{ {
if (test_offset(b, b->position, 4)) if (test_offset(b, 4))
return 0xFFFFFFFF; // ... unsigned return 0xFFFFFFFF; // ... unsigned
uint32_t ret = b->bytes[b->position] << 24 uint32_t ret = b->bytes[b->position] << 24
| b->bytes[b->position +1] << 16 | b->bytes[b->position +1] << 16
| b->bytes[b->position +2] << 8 | 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) uint64_t ob_get_ui64(struct overlay_buffer *b)
{ {
if (test_offset(b, b->position, 8)) if (test_offset(b, 8))
return 0xFFFFFFFF; // ... unsigned return 0xFFFFFFFF; // ... unsigned
uint64_t ret = (uint64_t)b->bytes[b->position] << 56 uint64_t ret = (uint64_t)b->bytes[b->position] << 56
| (uint64_t)b->bytes[b->position +1] << 48 | (uint64_t)b->bytes[b->position +1] << 48
| (uint64_t)b->bytes[b->position +2] << 40 | (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) uint16_t ob_get_ui16(struct overlay_buffer *b)
{ {
if (test_offset(b, b->position, 2)) if (test_offset(b, 2))
return 0xFFFF; // ... unsigned return 0xFFFF; // ... unsigned
uint16_t ret = b->bytes[b->position] << 8 uint16_t ret = b->bytes[b->position] << 8
| b->bytes[b->position +1]; | b->bytes[b->position +1];
b->position+=2; b->position+=2;
@ -520,18 +516,16 @@ uint64_t ob_get_packed_ui64(struct overlay_buffer *b)
int ob_get(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 -1;
return b->bytes[b->position++]; 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; const int bytes = 2;
assert(b != NULL); assert(b != NULL);
assert(offset >= 0); assert(offset + bytes <= b->sizeLimit);
if (b->sizeLimit != -1)
assert(offset + bytes <= b->sizeLimit);
assert(offset + bytes <= b->allocSize); assert(offset + bytes <= b->allocSize);
b->bytes[offset] = (v >> 8) & 0xFF; b->bytes[offset] = (v >> 8) & 0xFF;
b->bytes[offset+1] = v & 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)); 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; const int bytes = 1;
assert(b != NULL); assert(b != NULL);
assert(offset >= 0); assert(offset + bytes <= b->sizeLimit);
if (b->sizeLimit != -1)
assert(offset + bytes <= b->sizeLimit);
assert(offset + bytes <= b->allocSize); assert(offset + bytes <= b->allocSize);
b->bytes[offset] = byte; b->bytes[offset] = byte;
if (config.debug.overlaybuffer) 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; return b->position;
} }
int ob_limit(struct overlay_buffer *b) size_t ob_limit(struct overlay_buffer *b)
{ {
return b->sizeLimit; return b->sizeLimit;
} }
int ob_remaining(struct overlay_buffer *b) size_t ob_remaining(struct overlay_buffer *b)
{ {
assert(b->sizeLimit != -1); assert(b->sizeLimit != SIZE_MAX);
return b->sizeLimit - b->position; assert(b->position <= b->sizeLimit);
return (size_t)(b->sizeLimit - b->position);
} }
int _ob_overrun(struct __sourceloc __whence, struct overlay_buffer *b) int _ob_overrun(struct __sourceloc __whence, struct overlay_buffer *b)
{ {
assert(b->allocSize >= 0); int ret = b->position > (b->sizeLimit != SIZE_MAX && b->sizeLimit < b->allocSize ? b->sizeLimit : b->allocSize);
if (b->sizeLimit != -1)
assert(b->sizeLimit >= 0);
int ret = b->position > (b->sizeLimit != -1 && b->sizeLimit < b->allocSize ? b->sizeLimit : b->allocSize);
if (config.debug.overlaybuffer) if (config.debug.overlaybuffer)
DEBUGF("ob_overrun(b=%p) return %d", b, ret); DEBUGF("ob_overrun(b=%p) return %d", b, ret);
return ret; return ret;
@ -599,8 +589,13 @@ int asprintable(int c)
int ob_dump(struct overlay_buffer *b, char *desc) 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); DEBUGF("overlay_buffer '%s' at %p (%p) : position=%zu, size=%zu", desc, b, b->bytes, b->position, b->sizeLimit);
if (b->bytes && (b->position || b->sizeLimit)) if (b->bytes) {
dump(desc, b->bytes, b->position?b->position:b->sizeLimit); 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; return 0;
} }

View File

@ -24,43 +24,43 @@ struct overlay_buffer {
unsigned char *bytes; unsigned char *bytes;
// remembered position for rewinding // remembered position for rewinding
int checkpointLength; size_t checkpointLength;
// position of data read / written // position of data read / written
int position; size_t position;
// maximum allowed bytes for reading / writing // maximum allowed bytes for reading / writing
int sizeLimit; size_t sizeLimit;
// size of buffer // size of buffer
int allocSize; size_t allocSize;
// is this an allocated buffer? can it be resized? Should it be freed? // is this an allocated buffer? can it be resized? Should it be freed?
unsigned char * allocated; unsigned char * allocated;
// length position for later patching // 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_new(struct __sourceloc __whence);
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 *_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);
struct overlay_buffer *_ob_dup(struct __sourceloc __whence, struct overlay_buffer *b); struct overlay_buffer *_ob_dup(struct __sourceloc __whence, struct overlay_buffer *b);
void _ob_free(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_checkpoint(struct __sourceloc __whence, struct overlay_buffer *b);
int _ob_rewind(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_flip(struct __sourceloc __whence, struct overlay_buffer *b);
void _ob_unlimitsize(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); 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(struct __sourceloc __whence, struct overlay_buffer *b, size_t ofs, unsigned char byte);
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);
void _ob_patch_rfs(struct __sourceloc __whence, struct overlay_buffer *b); 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_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); 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_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_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); 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); void ob_skip(struct overlay_buffer *b, unsigned n);
// get one byte from the current position, -ve number indicates failure // get one byte from the current position, -ve number indicates failure
int ob_get(struct overlay_buffer *b); int ob_get(struct overlay_buffer *b);
int ob_get_bytes(struct overlay_buffer *b, unsigned char *buff, 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, int len); unsigned char * ob_get_bytes_ptr(struct overlay_buffer *b, size_t len);
uint64_t ob_get_ui64(struct overlay_buffer *b); uint64_t ob_get_ui64(struct overlay_buffer *b);
uint32_t ob_get_ui32(struct overlay_buffer *b); uint32_t ob_get_ui32(struct overlay_buffer *b);
uint16_t ob_get_ui16(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); uint64_t ob_get_packed_ui64(struct overlay_buffer *b);
// information routines // information routines
int ob_position(struct overlay_buffer *b); size_t ob_position(struct overlay_buffer *b);
int ob_limit(struct overlay_buffer *b); size_t ob_limit(struct overlay_buffer *b);
int ob_remaining(struct overlay_buffer *b); size_t ob_remaining(struct overlay_buffer *b);
int _ob_overrun(struct __sourceloc, struct overlay_buffer *b); int _ob_overrun(struct __sourceloc, struct overlay_buffer *b);
unsigned char* ob_ptr(struct overlay_buffer *b); unsigned char* ob_ptr(struct overlay_buffer *b);

View File

@ -810,13 +810,13 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o
{ {
assert(destination && destination->interface); assert(destination && destination->interface);
const unsigned char *bytes = ob_ptr(buffer); const unsigned char *bytes = ob_ptr(buffer);
int len = ob_position(buffer); size_t len = ob_position(buffer);
struct overlay_interface *interface = destination->interface; struct overlay_interface *interface = destination->interface;
destination->last_tx = gettime_ms(); destination->last_tx = gettime_ms();
if (config.debug.packettx){ 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); DEBUG_packet_visualise(NULL, bytes, len);
} }
@ -826,7 +826,7 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o
} }
if (interface->debug) 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++; interface->tx_count++;
@ -842,11 +842,11 @@ int overlay_broadcast_ensemble(struct network_destination *destination, struct o
.pid = getpid(), .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"); 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); bcopy(bytes, packet.payload, len);
ob_free(buffer); ob_free(buffer);
/* This lseek() is unneccessary because the dummy file is opened in O_APPEND mode. It's /* 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: case SOCK_DGRAM:
{ {
if (config.debug.overlayinterfaces) 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, ssize_t sent = sendto(interface->alarm.poll.fd,
bytes, (size_t)len, 0, bytes, (size_t)len, 0,
(struct sockaddr *)&destination->address, sizeof(destination->address)); (struct sockaddr *)&destination->address, sizeof(destination->address));
ob_free(buffer); ob_free(buffer);
if (sent == -1 || (size_t)sent != (size_t)len) { if (sent == -1 || (size_t)sent != len) {
if (sent == -1) if (sent == -1)
WHYF_perror("sendto(fd=%d,len=%zu,addr=%s) on interface %s", WHYF_perror("sendto(fd=%d,len=%zu,addr=%s) on interface %s",
interface->alarm.poll.fd, interface->alarm.poll.fd,
(size_t)len, len,
alloca_sockaddr((struct sockaddr *)&destination->address, sizeof destination->address), alloca_sockaddr((struct sockaddr *)&destination->address, sizeof destination->address),
interface->name interface->name
); );
else else
WHYF("sendto() sent %zu bytes of overlay frame (%zu) to interface %s (socket=%d)", 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, // close the interface if we had any error while sending broadcast packets,
// unicast packets should not bring the interface down // unicast packets should not bring the interface down
if (destination == interface->destination) if (destination == interface->destination)

View File

@ -766,7 +766,7 @@ static int overlay_send_frame(
if (ob_overrun(plaintext)) { if (ob_overrun(plaintext)) {
if (config.debug.mdprequests) 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); plaintext->position, plaintext->allocSize, plaintext->sizeLimit);
op_free(frame); op_free(frame);
ob_free(plaintext); ob_free(plaintext);

View File

@ -196,7 +196,7 @@ static void parse_frame(struct overlay_buffer *buff){
frame.modifiers=ob_get(buff); frame.modifiers=ob_get(buff);
if (config.debug.overlayinterfaces) 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 // the remaining bytes are an mdp payload, process it
frame.payload = buff; frame.payload = buff;
@ -297,7 +297,7 @@ int olsr_send(struct overlay_frame *frame){
ob_append_byte(b, frame->modifiers); ob_append_byte(b, frame->modifiers);
if (config.debug.overlayinterfaces) 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 // send the packet
int ret = send_packet(b->bytes, b->position, frame->payload->bytes, frame->payload->sizeLimit); int ret = send_packet(b->bytes, b->position, frame->payload->bytes, frame->payload->sizeLimit);

View File

@ -425,7 +425,7 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
} }
// TODO allow for single byte length? // TODO allow for single byte length?
unsigned int payload_len; size_t payload_len;
switch (context.encapsulation){ switch (context.encapsulation){
case ENCAP_SINGLE: case ENCAP_SINGLE:
@ -459,7 +459,7 @@ int packetOkOverlay(struct overlay_interface *interface,unsigned char *packet, s
if (header_valid!=0){ 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){ if (!f.payload){
// out of memory? // out of memory?
WHY("Unable to slice payload"); WHY("Unable to slice payload");

View File

@ -94,7 +94,7 @@ int overlay_frame_append_payload(struct decode_context *context, int encapsulati
alloca_tohex_sid_t(p->destination->sid),p->type, alloca_tohex_sid_t(p->destination->sid),p->type,
"append_payload stuffing into packet"); "append_payload stuffing into packet");
if (p->payload) 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; struct broadcast *broadcast=NULL;

View File

@ -335,7 +335,7 @@ overlay_stuff_packet(struct outgoing_packet *packet, overlay_txqueue *queue, tim
while(frame){ while(frame){
if (frame->enqueued_at + queue->latencyTarget < now){ if (frame->enqueued_at + queue->latencyTarget < now){
if (config.debug.overlayframes) 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->type, frame->payload->checkpointLength,
frame->destination?alloca_tohex_sid_t(frame->destination->sid):"All"); frame->destination?alloca_tohex_sid_t(frame->destination->sid):"All");
frame = overlay_queue_remove(queue, frame); frame = overlay_queue_remove(queue, frame);

View File

@ -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"); DEBUG("Found a BAR that is the wrong size - ignoring");
continue; continue;
} }
if (ob_remaining(e) < blob_bytes) { if (ob_remaining(e) < RHIZOME_BAR_BYTES) {
// out of room // out of room
count--; count--;
break; break;
} }
ob_append_bytes(e, (unsigned char *)data, blob_bytes); ob_append_bytes(e, (unsigned char *)data, RHIZOME_BAR_BYTES);
*last_rowid=rowid; *last_rowid=rowid;
} }
if (statement) if (statement)

View File

@ -488,7 +488,7 @@ static int append_link_state(struct overlay_buffer *payload, char flags,
flags|=FLAG_HAS_ACK; flags|=FLAG_HAS_ACK;
if (drop_rate!=-1) if (drop_rate!=-1)
flags|=FLAG_HAS_DROP_RATE; 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, 0);
ob_append_byte(payload, flags); ob_append_byte(payload, flags);
overlay_address_append(NULL, payload, receiver); 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)) if (ob_overrun(payload))
return -1; return -1;
// patch the record length // 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_set(payload, length_pos, end_pos - length_pos);
ob_checkpoint(payload); ob_checkpoint(payload);
return 0; return 0;
@ -898,7 +898,7 @@ static void link_send(struct sched_ent *alarm)
ob_limitsize(frame->payload, 400); ob_limitsize(frame->payload, 400);
overlay_mdp_encode_ports(frame->payload, MDP_PORT_LINKSTATE, MDP_PORT_LINKSTATE); overlay_mdp_encode_ports(frame->payload, MDP_PORT_LINKSTATE, MDP_PORT_LINKSTATE);
ob_checkpoint(frame->payload); ob_checkpoint(frame->payload);
int pos = ob_position(frame->payload); size_t pos = ob_position(frame->payload);
enum_subscribers(NULL, append_link, frame->payload); enum_subscribers(NULL, append_link, frame->payload);
ob_rewind(frame->payload); ob_rewind(frame->payload);
if (ob_position(frame->payload) == pos) 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 subscriber *receiver=NULL, *transmitter=NULL;
struct overlay_interface *interface = NULL; struct overlay_interface *interface = NULL;
int start_pos = ob_position(payload); size_t start_pos = ob_position(payload);
int length = ob_get(payload); int length = ob_get(payload);
if (length <=0) if (length <=0)
break; break;