Skip to content

Commit

Permalink
Make the code -Wconversion ready: explicit casts only
Browse files Browse the repository at this point in the history
This patch, while adding lots of extra boilerplate, converts all code
to explicit casts only; it now builds with `-Wconversion` cleanly,
although that's a bit too strong to turn on by default (for now).
  • Loading branch information
iustin committed May 1, 2015
1 parent 9365483 commit 0da2c5a
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions xattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ pygetxattr(PyObject *self, PyObject *args)
int nofollow = 0;
char *attrname = NULL;
char *buf;
ssize_t nalloc, nret;
ssize_t nalloc_s, nret;
size_t nalloc;
PyObject *res;

/* Parse the arguments */
Expand All @@ -288,11 +289,13 @@ pygetxattr(PyObject *self, PyObject *args)
}

/* Find out the needed size of the buffer */
if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
if((nalloc_s = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto free_tgt;
}

nalloc = (size_t) nalloc_s;

/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
Expand Down Expand Up @@ -353,7 +356,8 @@ xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
const char *fullname;
char *buf;
const char *ns = NULL;
ssize_t nalloc, nret;
ssize_t nalloc_s, nret;
size_t nalloc;
PyObject *res;
static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};

Expand All @@ -372,11 +376,13 @@ xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
}

/* Find out the needed size of the buffer */
if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
if((nalloc_s = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto free_name_buf;
}

nalloc = (size_t) nalloc_s;

/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
Expand Down Expand Up @@ -452,7 +458,8 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds)
const char *ns = NULL;
char *buf_list, *buf_val, *buf_val_tmp;
const char *s;
ssize_t nalloc, nlist, nval;
ssize_t nalloc_s, nlist, nval_s;
size_t nalloc, nval;
PyObject *mylist;
target_t tgt;
static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
Expand All @@ -467,18 +474,20 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds)
/* Compute first the list of attributes */

/* Find out the needed size of the buffer for the attribute list */
nalloc = _list_obj(&tgt, NULL, 0);
nalloc_s = _list_obj(&tgt, NULL, 0);

if(nalloc == -1) {
if(nalloc_s == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto free_tgt;
}

if(nalloc == 0) {
if(nalloc_s == 0) {
res = PyList_New(0);
goto free_tgt;
}

nalloc = (size_t) nalloc_s;

/* Try to allocate the memory, using Python's allocator */
if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
Expand Down Expand Up @@ -518,26 +527,27 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds)
/* Now retrieve the attribute value */
missing = 0;
while(1) {
nval = _get_obj(&tgt, s, buf_val, nalloc);
nval_s = _get_obj(&tgt, s, buf_val, nalloc);

if(nval == -1) {
if(nval_s == -1) {
if(errno == ERANGE) {
nval = _get_obj(&tgt, s, NULL, 0);
ssize_t realloc_size_s = _get_obj(&tgt, s, NULL, 0);
/* ERANGE + proper size should not fail, but it
still can, so let's check first */
if(nval == -1) {
if(realloc_size_s == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
Py_DECREF(mylist);
goto free_buf_val;
}
if((buf_val_tmp = PyMem_Realloc(buf_val, nval)) == NULL) {
size_t realloc_size = (size_t) realloc_size_s;
if((buf_val_tmp = PyMem_Realloc(buf_val, realloc_size))
== NULL) {
res = PyErr_NoMemory();
Py_DECREF(mylist);
goto free_buf_val;
} else {
buf_val = buf_val_tmp;
}
nalloc = nval;
buf_val = buf_val_tmp;
nalloc = realloc_size;
continue;
} else if(errno == ENODATA || errno == ENOATTR) {
/* this attribute has gone away since we queried
Expand All @@ -550,6 +560,8 @@ get_all(PyObject *self, PyObject *args, PyObject *keywds)
Py_DECREF(mylist);
res = PyErr_SetFromErrno(PyExc_IOError);
goto free_buf_val;
} else {
nval = (size_t) nval_s;
}
break;
}
Expand Down Expand Up @@ -886,7 +898,8 @@ pylistxattr(PyObject *self, PyObject *args)
{
char *buf;
int nofollow=0;
ssize_t nalloc, nret;
ssize_t nalloc_s, nret;
size_t nalloc;
PyObject *myarg;
PyObject *mylist;
Py_ssize_t nattrs;
Expand All @@ -900,16 +913,18 @@ pylistxattr(PyObject *self, PyObject *args)
return NULL;

/* Find out the needed size of the buffer */
if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
if((nalloc_s = _list_obj(&tgt, NULL, 0)) == -1) {
mylist = PyErr_SetFromErrno(PyExc_IOError);
goto free_tgt;
}

if(nalloc == 0) {
if(nalloc_s == 0) {
mylist = PyList_New(0);
goto free_tgt;
}

nalloc = (size_t) nalloc_s;

/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
mylist = PyErr_NoMemory();
Expand Down Expand Up @@ -986,7 +1001,8 @@ xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
{
char *buf;
int nofollow = 0;
ssize_t nalloc, nret;
ssize_t nalloc_s, nret;
size_t nalloc;
PyObject *myarg;
PyObject *res;
const char *ns = NULL;
Expand All @@ -1005,16 +1021,18 @@ xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
}

/* Find out the needed size of the buffer */
if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
if((nalloc_s = _list_obj(&tgt, NULL, 0)) == -1) {
res = PyErr_SetFromErrno(PyExc_IOError);
goto free_tgt;
}

if(nalloc == 0) {
if(nalloc_s == 0) {
res = PyList_New(0);
goto free_tgt;
}

nalloc = (size_t) nalloc_s;

/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
res = PyErr_NoMemory();
Expand Down

0 comments on commit 0da2c5a

Please sign in to comment.