Commit 37944b11 37944b1184319def0eee6cb9ba50dfeb476f7301 by Sergey Poznyakoff

python: fix passing size_t values on 64-bit architectures

* lib/python.c (python_proc): Pass a copy of mu_program_name
as argv[0].
* python/libmu_py/libmu_py.h (ASSERT_INDEX_RANGE): New macro.
* python/libmu_py/address.c: Use Py_ssize_t and check returned
value range when getting size_t arguments.
* python/libmu_py/auth.c: Likewise.
* python/libmu_py/folder.c: Likewise.
* python/libmu_py/header.c: Likewise.
* python/libmu_py/mailbox.c: Likewise.
* python/libmu_py/mailcap.c: Likewise.
* python/libmu_py/message.c: Likewise.
* python/libmu_py/mime.c: Likewise.
* python/libmu_py/secret.c: Likewise.
* python/libmu_py/stream.c: Likewise.
1 parent a551d489
......@@ -45,8 +45,9 @@ python_proc (mu_script_descr_t descr, mu_message_t msg)
mu_py_dict dict[2];
mu_py_script_data data[1];
char *argv[] = { NULL, NULL };
argv[0] = mu_program_name;
char *argv0 = mu_strdup (mu_program_name);
argv[0] = argv0;
mu_py_script_init (1, argv);
......@@ -62,6 +63,9 @@ python_proc (mu_script_descr_t descr, mu_message_t msg)
mu_py_script_run ((char*)descr, data);
mu_py_script_finish ();
free (argv0);
return 0;
}
......
......@@ -160,12 +160,15 @@ api_address_destroy (PyObject *self, PyObject *args)
static PyObject *
api_address_is_group (PyObject *self, PyObject *args)
{
int status, n, isgroup;
int status, isgroup;
Py_ssize_t n;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address");
status = mu_address_is_group (py_addr->addr, n, &isgroup);
return status_object (status, PyBool_FromLong (isgroup));
}
......@@ -186,13 +189,16 @@ api_address_get_count (PyObject *self, PyObject *args)
static PyObject *
api_address_get_email (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
const char *buf = NULL;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address");
status = mu_address_sget_email (py_addr->addr, n, &buf);
return status_object (status, PyString_FromString (buf ? buf : ""));
}
......@@ -200,13 +206,16 @@ api_address_get_email (PyObject *self, PyObject *args)
static PyObject *
api_address_get_local_part (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
const char *buf = NULL;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address part");
status = mu_address_sget_local_part (py_addr->addr, n, &buf);
return status_object (status, PyString_FromString (buf ? buf : ""));
}
......@@ -214,13 +223,14 @@ api_address_get_local_part (PyObject *self, PyObject *args)
static PyObject *
api_address_get_domain (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
const char *buf = NULL;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address part");
status = mu_address_sget_domain (py_addr->addr, n, &buf);
return status_object (status, PyString_FromString (buf ? buf : ""));
}
......@@ -228,13 +238,14 @@ api_address_get_domain (PyObject *self, PyObject *args)
static PyObject *
api_address_get_personal (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
const char *buf = NULL;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address part");
status = mu_address_sget_personal (py_addr->addr, n, &buf);
return status_object (status, PyString_FromString (buf ? buf : ""));
}
......@@ -242,13 +253,14 @@ api_address_get_personal (PyObject *self, PyObject *args)
static PyObject *
api_address_get_comments (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
const char *buf = NULL;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address part");
status = mu_address_sget_comments (py_addr->addr, n, &buf);
return status_object (status, PyString_FromString (buf ? buf : ""));
}
......@@ -256,13 +268,14 @@ api_address_get_comments (PyObject *self, PyObject *args)
static PyObject *
api_address_get_route (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
const char *buf = NULL;
PyAddress *py_addr;
if (!PyArg_ParseTuple (args, "O!i", &PyAddressType, &py_addr, &n))
if (!PyArg_ParseTuple (args, "O!n", &PyAddressType, &py_addr, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "address part");
status = mu_address_sget_route (py_addr->addr, n, &buf);
return status_object (status, PyString_FromString (buf ? buf : ""));
}
......
......@@ -591,11 +591,12 @@ static PyObject *
api_get_auth_by_uid (PyObject *self, PyObject *args)
{
uid_t uid;
Py_ssize_t n;
PyAuthData *py_ad = PyAuthData_NEW ();
if (!PyArg_ParseTuple (args, "i", &uid))
if (!PyArg_ParseTuple (args, "n", &n))
return NULL;
uid = (uid_t) n;
Py_INCREF (py_ad);
py_ad->auth_data = mu_get_auth_by_uid (uid);
......
......@@ -203,16 +203,20 @@ static PyObject *
api_folder_list (PyObject *self, PyObject *args)
{
int status = 0;
size_t max_level = 0;
Py_ssize_t max_level = 0;
char *dirname, *pattern;
PyFolder *py_folder;
PyObject *py_list;
mu_list_t c_list = NULL;
if (!PyArg_ParseTuple (args, "O!zs|i", &PyFolderType, &py_folder,
if (!PyArg_ParseTuple (args, "O!zs|n", &PyFolderType, &py_folder,
&dirname, &pattern, &max_level))
return NULL;
if (max_level < 0)
{
PyErr_SetString (PyExc_RuntimeError, "max level out of range");
return NULL;
}
status = mu_folder_list (py_folder->folder, dirname, pattern, max_level,
&c_list);
......
......@@ -122,14 +122,15 @@ api_header_get_value (PyObject *self, PyObject *args)
static PyObject *
api_header_get_value_n (PyObject *self, PyObject *args)
{
int status, n;
int status;
Py_ssize_t n;
char *name;
const char *value = NULL;
PyHeader *py_hdr;
if (!PyArg_ParseTuple (args, "O!si", &PyHeaderType, &py_hdr, &name, &n))
if (!PyArg_ParseTuple (args, "O!sn", &PyHeaderType, &py_hdr, &name, &n))
return NULL;
ASSERT_INDEX_RANGE (n, "header");
status = mu_header_sget_value_n (py_hdr->hdr, name, n, &value);
return status_object (status, PyString_FromString (value ? value : ""));
}
......@@ -167,13 +168,13 @@ static PyObject *
api_header_get_field_name (PyObject *self, PyObject *args)
{
int status;
size_t idx;
Py_ssize_t idx;
const char *name = NULL;
PyHeader *py_hdr;
if (!PyArg_ParseTuple (args, "O!i", &PyHeaderType, &py_hdr, &idx))
if (!PyArg_ParseTuple (args, "O!n", &PyHeaderType, &py_hdr, &idx))
return NULL;
ASSERT_INDEX_RANGE (idx, "header");
status = mu_header_sget_field_name (py_hdr->hdr, idx, &name);
return status_object (status, PyString_FromString (name ? name : ""));
}
......@@ -182,13 +183,13 @@ static PyObject *
api_header_get_field_value (PyObject *self, PyObject *args)
{
int status;
size_t idx;
Py_ssize_t idx;
const char *value = NULL;
PyHeader *py_hdr;
if (!PyArg_ParseTuple (args, "O!i", &PyHeaderType, &py_hdr, &idx))
if (!PyArg_ParseTuple (args, "O!n", &PyHeaderType, &py_hdr, &idx))
return NULL;
ASSERT_INDEX_RANGE (idx, "header");
status = mu_header_sget_field_value (py_hdr->hdr, idx, &value);
return status_object (status, PyString_FromString (value ? value : ""));
}
......
......@@ -52,6 +52,17 @@
#define PY_ROOT_NAME "c_api"
#define PY_PACKAGE_VERSION PACKAGE_VERSION
#define ASSERT_INDEX_RANGE(n, t) \
do \
{ \
if ((n) <= 0) \
{ \
PyErr_SetString (PyExc_RuntimeError, t " index out of range"); \
return NULL; \
} \
} \
while (0)
extern inline PyObject * _ro (PyObject *obj);
extern void _py_dealloc (PyObject *self);
extern PyObject * status_object (int status, PyObject *py_obj);
......
......@@ -131,7 +131,8 @@ api_mailbox_open (PyObject *self, PyObject *args)
if (!PyArg_ParseTuple (args, "O!i", &PyMailboxType, &py_mbox, &flag))
return NULL;
if (!flag)
flag = MU_STREAM_READ;
status = mu_mailbox_open (py_mbox->mbox, flag);
return _ro (PyInt_FromLong (status));
}
......@@ -208,13 +209,15 @@ static PyObject *
api_mailbox_get_message (PyObject *self, PyObject *args)
{
int status;
size_t msgno;
Py_ssize_t msgno;
PyMailbox *py_mbox;
PyMessage *py_msg = PyMessage_NEW ();
if (!PyArg_ParseTuple (args, "O!i", &PyMailboxType, &py_mbox, &msgno))
if (!PyArg_ParseTuple (args, "O!n", &PyMailboxType, &py_mbox, &msgno))
return NULL;
ASSERT_INDEX_RANGE (msgno, "message");
status = mu_mailbox_get_message (py_mbox->mbox, msgno, &py_msg->msg);
Py_INCREF (py_msg);
......
......@@ -184,13 +184,14 @@ api_mailcap_entries_count (PyObject *self, PyObject *args)
static PyObject *
api_mailcap_get_entry (PyObject *self, PyObject *args)
{
int status, i;
int status;
Py_ssize_t i;
PyMailcap *py_mc;
PyMailcapEntry *py_entry = PyMailcapEntry_NEW ();
if (!PyArg_ParseTuple (args, "O!i", &PyMailcapType, &py_mc, &i))
if (!PyArg_ParseTuple (args, "O!n", &PyMailcapType, &py_mc, &i))
return NULL;
ASSERT_INDEX_RANGE (i, "mailcap");
status = mu_mailcap_get_entry (py_mc->mc, i, &py_entry->entry);
Py_INCREF (py_entry);
......@@ -214,14 +215,14 @@ api_mailcap_entry_fields_count (PyObject *self, PyObject *args)
static PyObject *
api_mailcap_entry_get_field (PyObject *self, PyObject *args)
{
int status, i;
int status;
Py_ssize_t i;
char buf[256];
PyMailcapEntry *py_entry;
if (!PyArg_ParseTuple (args, "O!i", &PyMailcapEntryType, &py_entry,
&i))
if (!PyArg_ParseTuple (args, "O!n", &PyMailcapEntryType, &py_entry, &i))
return NULL;
ASSERT_INDEX_RANGE (i, "mailcap");
status = mu_mailcap_entry_get_field (py_entry->entry, i, buf,
sizeof (buf), NULL);
return status_object (status, PyString_FromString (buf));
......
......@@ -230,13 +230,13 @@ static PyObject *
api_message_get_part (PyObject *self, PyObject *args)
{
int status;
size_t npart;
Py_ssize_t npart;
PyMessage *py_msg;
PyMessage *py_part = PyMessage_NEW ();
if (!PyArg_ParseTuple (args, "O!i", &PyMessageType, &py_msg, &npart))
if (!PyArg_ParseTuple (args, "O!n", &PyMessageType, &py_msg, &npart))
return NULL;
ASSERT_INDEX_RANGE (npart, "message part");
status = mu_message_get_part (py_msg->msg, npart, &py_part->msg);
Py_INCREF (py_part);
......
......@@ -140,13 +140,13 @@ static PyObject *
api_mime_get_part (PyObject *self, PyObject *args)
{
int status;
size_t npart;
Py_ssize_t npart;
PyMime *py_mime;
PyMessage *py_part = PyMessage_NEW ();
if (!PyArg_ParseTuple (args, "O!i", &PyMimeType, &py_mime, &npart))
if (!PyArg_ParseTuple (args, "O!n", &PyMimeType, &py_mime, &npart))
return NULL;
ASSERT_INDEX_RANGE (npart, "mime part");
status = mu_mime_get_part (py_mime->mime, npart, &py_part->msg);
Py_INCREF (py_part);
......
......@@ -87,13 +87,17 @@ api_secret_create (PyObject *self, PyObject *args)
{
int status;
char *str;
size_t len;
Py_ssize_t len;
PySecret *py_secret;
if (!PyArg_ParseTuple (args, "O!si", &PySecretType, &py_secret,
if (!PyArg_ParseTuple (args, "O!sn", &PySecretType, &py_secret,
&str, &len))
return NULL;
if (len <= 0)
{
PyErr_SetString (PyExc_RuntimeError, "secret length out of range");
return NULL;
}
status = mu_secret_create (&py_secret->secret, str, len);
return _ro (PyInt_FromLong (status));
}
......
......@@ -303,12 +303,17 @@ api_stream_write (PyObject *self, PyObject *args)
{
int status;
char *wbuf;
size_t size, write_count;
Py_ssize_t size;
size_t write_count;
PyStream *py_stm;
if (!PyArg_ParseTuple (args, "O!si", &PyStreamType, &py_stm, &wbuf, &size))
if (!PyArg_ParseTuple (args, "O!sn", &PyStreamType, &py_stm, &wbuf, &size))
return NULL;
if (size < 0)
{
PyErr_SetString (PyExc_RuntimeError, "negative size");
return NULL;
}
status = mu_stream_write (py_stm->stm, wbuf, size, &write_count);
return status_object (status, PyInt_FromLong (write_count));
}
......