Skip to content

Commit

Permalink
Correct GMPy_MPC_To_Binary()
Browse files Browse the repository at this point in the history
Docs (for PyBytes_AsString) says: The data must not be modified in any
way, unless the object was just created using
PyBytes_FromStringAndSize(NULL, size).

That's not the case for GMPy_MPFR_To_Binary().  So, PyPy seems to be
right in rejecting modifications.
  • Loading branch information
skirpichev committed Jan 5, 2025
1 parent 7a7b7dc commit b6bd13b
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/gmpy2_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,39 @@ GMPy_MPC_To_Binary(MPC_Object *obj)
Py_DECREF((PyObject*)real);
Py_DECREF((PyObject*)imag);

PyBytes_AS_STRING(result)[0] = 0x05;
PyBytes_AS_STRING(temp)[0] = 0x05;
Py_ssize_t result_size, temp_size;
char *result_str, *temp_str;

PyBytes_ConcatAndDel(&result, temp);
return result;
if ((PyBytes_AsStringAndSize(result, &result_str, &result_size) < 0)
|| (PyBytes_AsStringAndSize(temp, &temp_str, &temp_size) < 0))
{
/* LCOV_EXCL_START */
Py_DECREF(result);
Py_DECREF(temp);
return NULL;
/* LCOV_EXCL_STOP */
}
result_str[0] = 0x05;
temp_str[0] = 0x05;

char *buf = PyMem_Malloc(result_size + temp_size);

if (!buf) {
/* LCOV_EXCL_START */
Py_DECREF(result);
Py_DECREF(temp);
return NULL;
/* LCOV_EXCL_STOP */
}
memcpy(buf, result_str, result_size);
memcpy(buf + result_size, temp_str, temp_size);
Py_DECREF(result);
Py_DECREF(temp);

PyObject *ret = PyBytes_FromStringAndSize(buf, result_size + temp_size);

PyMem_Free(buf);
return ret;
}

PyDoc_STRVAR(doc_from_binary,
Expand Down

0 comments on commit b6bd13b

Please sign in to comment.