Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up of warnings (tested on clang). #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions src/tpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <stdarg.h> /* va_list */
#include <string.h> /* memcpy, memset, strchr */
#include <stdio.h> /* printf (tpl_hook.oops default function) */
#include <limits.h> /* for INT_MIN, INT_MAX */

#ifndef _WIN32
#include <unistd.h> /* for ftruncate */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Copy link
Collaborator

@tbeu tbeu Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VS reports warning C4068: Unknown Pragma, thus you probably should wrap the pragma in #ifdef.

static const char tpl_datapeek_ok_chars[] = "iucsfIUjvp"; /* valid in datapeek */
static const struct tpl_type_t tpl_types[] = {
/* [TPL_TYPE_ROOT] = */ {'r', 0},
Expand Down Expand Up @@ -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 */
Expand All @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather see the ptrdiff_t type here. See https://www.viva64.com/en/a/0050/ for example.

+ (tpl_types[prev->type].sz * prev->num);
offset = (offset + align_sz - 1) / align_sz * align_sz;
return struct_addr + offset;
Expand Down Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is ok

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*));
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand All @@ -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;

Expand Down Expand Up @@ -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 *);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) {
Expand All @@ -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);
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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);
Expand Down