diff --git a/src/tpl.c b/src/tpl.c index 2e85c03..e8ab4b4 100644 --- a/src/tpl.c +++ b/src/tpl.c @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include /* va_list */ #include /* memcpy, memset, strchr */ #include /* printf (tpl_hook.oops default function) */ +#include /* for INT_MIN, INT_MAX */ #ifndef _WIN32 #include /* for ftruncate */ @@ -185,7 +186,7 @@ static int tpl_mmap_file(char *filename, tpl_mmap_rec *map_rec); static int tpl_mmap_output_file(char *filename, size_t sz, void **text_out); static int tpl_cpu_bigendian(void); static int tpl_needs_endian_swap(void *); -static void tpl_byteswap(void *word, int len); +static void tpl_byteswap(void *word, size_t len); static void tpl_fatal(const char *fmt, ...); static int tpl_serlen(tpl_node *r, tpl_node *n, void *dv, size_t *serlen); static int tpl_unpackA0(tpl_node *r); @@ -229,7 +230,10 @@ tpl_hook_t tpl_hook = { }; static const char tpl_fmt_chars[] = "AS($)BiucsfIUjv#p"; /* valid format chars */ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunused-variable" static const char tpl_S_fmt_chars[] = "iucsfIUjv#$()p"; /* valid within S(...) */ +#pragma clang diagnostic pop static const char tpl_datapeek_ok_chars[] = "iucsfIUjvp"; /* valid in datapeek */ static const struct tpl_type_t tpl_types[] = { /* [TPL_TYPE_ROOT] = */ {'r', 0}, @@ -283,7 +287,7 @@ static tpl_node *tpl_node_new(tpl_node *parent) { */ char *calc_field_addr(tpl_node *parent, int type,char *struct_addr, int ordinal) { tpl_node *prev; - int offset; + size_t offset; int align_sz; if (ordinal == 1) return struct_addr; /* first field starts on structure address */ @@ -302,7 +306,7 @@ char *calc_field_addr(tpl_node *parent, int type,char *struct_addr, int ordinal) align_sz = tpl_types[type].sz; break; } - offset = ((uintptr_t)prev->addr - (uintptr_t)struct_addr) + offset = ((size_t)prev->addr - (size_t)struct_addr) + (tpl_types[prev->type].sz * prev->num); offset = (offset + align_sz - 1) / align_sz * align_sz; return struct_addr + offset; @@ -876,7 +880,7 @@ static void *tpl_dump_atyp(tpl_node *n, tpl_atyp* at, void *dv) { /* dump the string length followed by the string */ for(i=0; i < c->num; i++) { memcpy(&strp,datav,sizeof(char*)); /* cp to aligned */ - slen = strp ? (strlen(strp)+1) : 0; + slen = strp ? (uint32_t)(strlen(strp)+1) : 0; dv = tpl_cpv(dv,&slen,sizeof(uint32_t)); if (slen > 1) dv = tpl_cpv(dv,strp,slen-1); datav = (void*)((uintptr_t)datav + sizeof(char*)); @@ -987,7 +991,8 @@ TPL_API int tpl_dump(tpl_node *r, int mode, ...) { va_list ap; char *filename, *bufv; void **addr_out,*buf, *pa_addr; - int fd,rc=0; + int fd; + int rc = 0; size_t sz,*sz_out, pa_sz; struct stat sbuf; @@ -1019,11 +1024,11 @@ TPL_API int tpl_dump(tpl_node *r, int mode, ...) { tpl_dump_to_mem(r,buf,sz); bufv = buf; do { - rc = write(fd,bufv,sz); - if (rc > 0) { - sz -= rc; - bufv += rc; - } else if (rc == -1) { + long res = write(fd,bufv,sz); + if (res > INT_MAX) + res = INT_MAX; + rc = (int)res; + if (rc == -1) { if (errno == EINTR || errno == EAGAIN) continue; tpl_hook.oops("error writing to fd %d: %s\n", fd, strerror(errno)); free(buf); @@ -1035,6 +1040,10 @@ TPL_API int tpl_dump(tpl_node *r, int mode, ...) { } return -1; } + else { + sz -= rc; + bufv += rc; + } } while (sz > 0); free(buf); rc = 0; @@ -1071,7 +1080,7 @@ TPL_API int tpl_dump(tpl_node *r, int mode, ...) { * the result of tpl_ser_osz(r). */ static int tpl_dump_to_mem(tpl_node *r,void *addr,size_t sz) { - uint32_t slen, sz32; + size_t slen, sz32; int *fxlens, num_fxlens, i; void *dv; char *fmt,flags; @@ -1083,7 +1092,7 @@ static int tpl_dump_to_mem(tpl_node *r,void *addr,size_t sz) { flags = 0; if (tpl_cpu_bigendian()) flags |= TPL_FL_BIGENDIAN; if (strchr(fmt,'s')) flags |= TPL_FL_NULLSTRINGS; - sz32 = sz; + sz32 = sz; dv = addr; dv = tpl_cpv(dv,TPL_MAGIC,3); /* copy tpl magic prefix */ @@ -1282,7 +1291,7 @@ TPL_API char* tpl_peek(int mode, ...) { uint32_t datapeek_ssz, datapeek_csz, datapeek_flen; tpl_mmap_rec mr = {0,NULL,0}; char *fmt,*fmt_cpy=NULL,c; - uint32_t intlsz, **fxlens=NULL, *num_fxlens_out=NULL, *fxlensv; + size_t intlsz, **fxlens=NULL, *num_fxlens_out=NULL, *fxlensv; va_start(ap,mode); if ((mode & TPL_FXLENS) && (mode & TPL_DATAPEEK)) { @@ -1301,8 +1310,8 @@ TPL_API char* tpl_peek(int mode, ...) { datapeek_f = va_arg(ap, char*); } if (mode & TPL_FXLENS) { - num_fxlens_out = va_arg(ap,uint32_t *); - fxlens = va_arg(ap,uint32_t **); + num_fxlens_out = (size_t *)va_arg(ap,uint32_t *); + fxlens = (size_t **)va_arg(ap,uint32_t **); *num_fxlens_out = 0; *fxlens = NULL; } @@ -1368,7 +1377,7 @@ TPL_API char* tpl_peek(int mode, ...) { first_atom = strspn(fmt, "S()"); /* skip any leading S() */ - datapeek_flen = strlen(datapeek_f); + datapeek_flen = (uint32_t)strlen(datapeek_f); if (strspn(datapeek_f, tpl_datapeek_ok_chars) < datapeek_flen) { tpl_hook.oops("invalid TPL_DATAPEEK format: %s\n", datapeek_f); tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */ @@ -1410,7 +1419,7 @@ TPL_API char* tpl_peek(int mode, ...) { } *(char**)datapeek_p = datapeek_s; } else { - datapeek_csz = tpl_size_for(*datapeek_c); + datapeek_csz = (uint32_t)tpl_size_for(*datapeek_c); if ((uintptr_t)dv-(uintptr_t)addr + datapeek_csz > sz) { tpl_hook.oops("tpl_peek: tpl has insufficient length\n"); tpl_hook.free(fmt_cpy); fmt_cpy = NULL; /* fail */ @@ -1436,7 +1445,8 @@ TPL_API int tpl_jot(int mode, ...) { va_list ap; char *filename, *fmt; size_t *sz; - int fd, rc=0; + int fd; + int rc=0; void **buf; tpl_node *tn; @@ -1478,9 +1488,9 @@ TPL_API int tpl_jot(int mode, ...) { TPL_API int tpl_load(tpl_node *r, int mode, ...) { va_list ap; int rc=0,fd=0; - char *filename=NULL; - void *addr; - size_t sz; + char *filename = NULL; + void *addr = NULL; + size_t sz = 0; va_start(ap,mode); if (mode & TPL_FILE) filename = va_arg(ap,char *); @@ -1787,8 +1797,7 @@ static int tpl_mmap_file(char *filename, tpl_mmap_rec *mr) { TPL_API int tpl_pack(tpl_node *r, int i) { tpl_node *n, *child, *np; void *datav=NULL; - size_t sz, itermax; - uint32_t slen; + size_t slen, sz, itermax; char *str; tpl_bin *bin; tpl_pound_data *pd; @@ -1837,7 +1846,7 @@ TPL_API int tpl_pack(tpl_node *r, int i) { bin = tpl_hook.malloc(sizeof(tpl_bin)); if (!bin) fatal_oom(); bin->addr = str; - bin->sz = slen; + bin->sz = (uint32_t)slen; /* now pack its pointer, first deep freeing any pre-existing bin */ if (*(tpl_bin**)(child->data) != NULL) { if ((*(tpl_bin**)(child->data))->sz != 0) { @@ -2164,7 +2173,7 @@ static int tpl_unpackA0(tpl_node *r) { } /* In-place byte order swapping of a word of length "len" bytes */ -static void tpl_byteswap(void *word, int len) { +static void tpl_byteswap(void *word, size_t len) { int i; char c, *w; w = (char*)word; @@ -2233,7 +2242,8 @@ TPL_API int tpl_gather(int mode, ...) { */ static int tpl_gather_blocking(int fd, void **img, size_t *sz) { char preamble[8]; - int i=0, rc; + int i=0; + long rc; uint32_t tpllen; do { @@ -2302,7 +2312,8 @@ static int tpl_gather_blocking(int fd, void **img, size_t *sz) { /* the file descriptor must be non-blocking for this functino to work. */ static int tpl_gather_nonblocking( int fd, tpl_gather_t **gs, tpl_gather_cb *cb, void *data) { char buf[TPL_GATHER_BUFLEN], *img, *tpl; - int rc, keep_looping, cbrc=0; + long rc; + int keep_looping, cbrc=0; size_t catlen; uint32_t tpllen; @@ -2386,7 +2397,7 @@ static int tpl_gather_nonblocking( int fd, tpl_gather_t **gs, tpl_gather_cb *cb, fatal_oom(); } (*gs)->img = tpl; - (*gs)->len = catlen; + (*gs)->len = (int)catlen; } else if (tpl < img+catlen) { /* consumed 1+ tpl(s) from img!=buf or 0 from img==buf */ if ( (*gs = tpl_hook.malloc(sizeof(tpl_gather_t))) == NULL ) { @@ -2395,7 +2406,7 @@ static int tpl_gather_nonblocking( int fd, tpl_gather_t **gs, tpl_gather_cb *cb, if ( ((*gs)->img = tpl_hook.malloc(img+catlen - tpl)) == NULL ) { fatal_oom(); } - (*gs)->len = img+catlen - tpl; + (*gs)->len = (int)(img+catlen - tpl); memcpy( (*gs)->img, tpl, img+catlen - tpl); /* free partially consumed concat buffer if used */ if (img != buf) tpl_hook.free(img); @@ -2471,7 +2482,7 @@ static int tpl_gather_mem( char *buf, size_t len, tpl_gather_t **gs, tpl_gather_ fatal_oom(); } (*gs)->img = tpl; - (*gs)->len = catlen; + (*gs)->len = (int)catlen; } else if (tpl < img+catlen) { /* consumed 1+ tpl(s) from img!=buf or 0 from img==buf */ if ( (*gs = tpl_hook.malloc(sizeof(tpl_gather_t))) == NULL ) { @@ -2480,7 +2491,7 @@ static int tpl_gather_mem( char *buf, size_t len, tpl_gather_t **gs, tpl_gather_ if ( ((*gs)->img = tpl_hook.malloc(img+catlen - tpl)) == NULL ) { fatal_oom(); } - (*gs)->len = img+catlen - tpl; + (*gs)->len = (int)(img+catlen - tpl); memcpy( (*gs)->img, tpl, img+catlen - tpl); /* free partially consumed concat buffer if used */ if (img != buf) tpl_hook.free(img);