From e113bf16570c7345ecbf7efc336dc8c6e3a29eef Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 4 Dec 2021 14:40:20 +0530 Subject: [PATCH] Use py_ssize_t for a # format when calling py_buildvalue --- Changelog.txt | 2 +- src/calibre/db/sqlite_extension.cpp | 3 ++- src/calibre/devices/mtp/windows/content_enumeration.cpp | 3 ++- src/calibre/ebooks/djvu/bzzdecoder.c | 3 ++- src/calibre/utils/certgen.c | 9 ++++++--- src/calibre/utils/podofo/doc.cpp | 3 ++- src/calibre/utils/windows/winutil.cpp | 6 ++++-- 7 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index 0b65d0792a..dd0690a839 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -58,7 +58,7 @@ - Google metadata plugin: When searching by ISBN if no results are found retry using an alternate query syntax - 5.33.1 fixes a couple of regressions that broke the toolbar in the popup comments editor dialog and rendering of the download - metadata button in the edit metadata dialog on windows + metadata button in the edit metadata dialog on Windows :: improved recipes - Smithsonian Magazine diff --git a/src/calibre/db/sqlite_extension.cpp b/src/calibre/db/sqlite_extension.cpp index 866f1d2994..a6b3bdf4a0 100644 --- a/src/calibre/db/sqlite_extension.cpp +++ b/src/calibre/db/sqlite_extension.cpp @@ -483,7 +483,8 @@ set_ui_language(PyObject *self, PyObject *args) { static int py_callback(void *ctx, int flags, const char *text, int text_length, int start_offset, int end_offset) { PyObject *ans = reinterpret_cast(ctx); - pyobject_raii item(Py_BuildValue("{ss# si si si}", "text", text, text_length, "start", start_offset, "end", end_offset, "flags", flags)); + Py_ssize_t psz = text_length; + pyobject_raii item(Py_BuildValue("{ss# si si si}", "text", text, psz, "start", start_offset, "end", end_offset, "flags", flags)); if (item) PyList_Append(ans, item.ptr()); return SQLITE_OK; } diff --git a/src/calibre/devices/mtp/windows/content_enumeration.cpp b/src/calibre/devices/mtp/windows/content_enumeration.cpp index 59640d92b2..411d5dc210 100644 --- a/src/calibre/devices/mtp/windows/content_enumeration.cpp +++ b/src/calibre/devices/mtp/windows/content_enumeration.cpp @@ -565,7 +565,8 @@ wpd::get_file(IPortableDevice *device, const wchar_t *object_id, PyObject *dest, } else if (SUCCEEDED(hr)) { if (bytes_read > 0) { total_read += bytes_read; - pyobject_raii res(PyObject_CallMethod(dest, "write", "y#", buf.ptr(), bytes_read)); + Py_ssize_t br = bytes_read; + pyobject_raii res(PyObject_CallMethod(dest, "write", "y#", buf.ptr(), br)); if (!res) { return NULL; } if (callback != NULL) { pyobject_raii r(PyObject_CallFunction(callback, "kK", total_read, filesize)); diff --git a/src/calibre/ebooks/djvu/bzzdecoder.c b/src/calibre/ebooks/djvu/bzzdecoder.c index be0d13fdb9..5b4641e585 100644 --- a/src/calibre/ebooks/djvu/bzzdecoder.c +++ b/src/calibre/ebooks/djvu/bzzdecoder.c @@ -676,7 +676,8 @@ end: for (i = 0; i < 3; i++) { buflen <<= 8; buflen += (uint8_t)buf[i]; } - ans = Py_BuildValue("y#", buf + 3, MIN(buflen, pos - buf)); + Py_ssize_t psz = MIN(buflen, pos - buf); + ans = Py_BuildValue("y#", buf + 3, psz); } if (buf != NULL) free(buf); if (PyErr_Occurred()) return NULL; diff --git a/src/calibre/utils/certgen.c b/src/calibre/utils/certgen.c index 72a6728182..e28efa1395 100644 --- a/src/calibre/utils/certgen.c +++ b/src/calibre/utils/certgen.c @@ -293,7 +293,8 @@ static PyObject* serialize_cert(PyObject *self, PyObject *args) { if (!mem) return set_error("BIO_new"); if (!PEM_write_bio_X509(mem, cert)) { BIO_free(mem); return set_error("PEM_write_bio_X509"); } sz = BIO_get_mem_data(mem, &p); - ans = Py_BuildValue("s#", p, sz); + Py_ssize_t psz = sz; + ans = Py_BuildValue("s#", p, psz); BIO_free(mem); return ans; } @@ -314,7 +315,8 @@ static PyObject* cert_info(PyObject *self, PyObject *args) { if (!mem) return set_error("BIO_new"); if (!X509_print_ex(mem, cert, XN_FLAG_COMPAT, X509_FLAG_COMPAT)) { BIO_free(mem); return set_error("X509_print_ex"); } sz = BIO_get_mem_data(mem, &p); - ans = Py_BuildValue("s#", p, sz); + Py_ssize_t psz = sz; + ans = Py_BuildValue("s#", p, psz); BIO_free(mem); return ans; } @@ -344,7 +346,8 @@ static PyObject* serialize_rsa_key(PyObject *self, PyObject *args) { else ok = PEM_write_bio_PrivateKey(mem, key, NULL, NULL, 0, 0, NULL); if (!ok) { set_error("PEM_write_bio_PrivateKey"); goto error; } sz = BIO_get_mem_data(mem, &p); - ans = Py_BuildValue("s#", p, sz); + Py_ssize_t psz = sz; + ans = Py_BuildValue("s#", p, psz); error: if (mem) BIO_free(mem); if (key) EVP_PKEY_free(key); diff --git a/src/calibre/utils/podofo/doc.cpp b/src/calibre/utils/podofo/doc.cpp index 8af45e6214..7cedee1a95 100644 --- a/src/calibre/utils/podofo/doc.cpp +++ b/src/calibre/utils/podofo/doc.cpp @@ -354,7 +354,8 @@ PDFDoc_get_xmp_metadata(PDFDoc *self, PyObject *args) { if ((str = metadata->GetStream()) != NULL) { str->GetFilteredCopy(&buf, &len); if (buf != NULL) { - ans = Py_BuildValue("y#", buf, len); + Py_ssize_t psz = len; + ans = Py_BuildValue("y#", buf, psz); free(buf); buf = NULL; if (ans == NULL) goto error; } diff --git a/src/calibre/utils/windows/winutil.cpp b/src/calibre/utils/windows/winutil.cpp index c9696011e7..1ae3eb97d8 100644 --- a/src/calibre/utils/windows/winutil.cpp +++ b/src/calibre/utils/windows/winutil.cpp @@ -460,7 +460,8 @@ winutil_read_directory_changes(PyObject *self, PyObject *args) { p = (PFILE_NOTIFY_INFORMATION)(PyBytes_AS_STRING(buffer) + offset); offset += p->NextEntryOffset; if (p->FileNameLength) { - PyObject *temp = Py_BuildValue("ku#", p->Action, p->FileName, p->FileNameLength / sizeof(wchar_t)); + Py_ssize_t psz = p->FileNameLength / sizeof(wchar_t); + PyObject *temp = Py_BuildValue("ku#", p->Action, p->FileName, psz); if (!temp) { Py_DECREF(ans); return NULL; } int ret = PyList_Append(ans, temp); Py_DECREF(temp); @@ -1048,7 +1049,8 @@ load_icon(PyObject *args, HMODULE handle, GRPICONDIRENTRY *entry) { DWORD sz = SizeofResource(handle, res); if (!sz) return NULL; HICON icon = CreateIconFromResourceEx(data, sz, TRUE, 0x00030000, 0, 0, LR_DEFAULTCOLOR); - return Py_BuildValue("y#N", data, sz, Handle_create(icon, IconHandle)); + Py_ssize_t psz = sz; + return Py_BuildValue("y#N", data, psz, Handle_create(icon, IconHandle)); } struct GRPICONDIR {