Repository: python/cpython
Pull request: gh-92434: Silence a compiler warning in _sqlite/connection.c for 32bit version
MSVC emits a `possible loss of data` warning when building 32bit version of `_sqlite3.Connection.serialize()`, whose data size ~is~ should be limited to `0x7fffffff` in `sqlite-3.38.4.0/sqlite3.c`, ~even on 64bit python~ (EDIT: Sorry, I missed the pages):
```
SQLITE_PRIVATE void *sqlite3Malloc(u64 n){
  void *p;
  if( n==0 || n>=0x7fffff00 ){
    /* A memory allocation of a number of bytes which is near the maximum
    ** signed integer value might cause an integer overflow inside of the
    ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
    ** 255 bytes of overhead.  SQLite itself will never use anything near
    ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
    p = 0;
  }else if( sqlite3GlobalConfig.bMemstat ){
    ...
```
#92434

Problem statement:
Possible loss of data warnings when building 32bit executable since 3.11
Windows (x86) buildbot shows two warnings on main branch:

- [x]  `channel_list_all()` in _xxsubinterpretersmodule.c, using `PyList_SET_ITEM()` function
```
D:\a\cpython\cpython\Modules\_xxsubinterpretersmodule.c(2341,9): warning C4244:
'function': conversion from 'int64_t' to 'Py_ssize_t', possible loss of data
```
<br>

- [x]  `serialize_impl()` in _sqlite/connection.c
```
D:\a\cpython\cpython\Modules\_sqlite\connection.c(2122,53): warning C4244:
'function': conversion from 'sqlite3_int64' to 'Py_ssize_t', possible loss of data
```
<br>

I'm not sure how to deal with them.

Hints from issue discussion:

Commit message: Remove OverflowError

Patch to review:
diff --git a/Misc/NEWS.d/next/Library/2022-05-25-04-48-30.gh-issue-92434.ZCv8o0.rst b/Misc/NEWS.d/next/Library/2022-05-25-04-48-30.gh-issue-92434.ZCv8o0.rst
new file mode 100644
index 00000000000000..2010200e0110e2
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-25-04-48-30.gh-issue-92434.ZCv8o0.rst
@@ -0,0 +1,2 @@
+:meth:`sqlite3.Connection.serialize` now raises :exc:`OverflowError` on 32-bit
+platforms if the serialized database exceeds ``INT_MAX`` bytes.
diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c
index 7f7de8e709228d..959683a5a5e424 100644
--- a/Modules/_sqlite/connection.c
+++ b/Modules/_sqlite/connection.c
@@ -2091,7 +2091,10 @@ serialize_impl(pysqlite_Connection *self, const char *name)
                      name);
         return NULL;
     }
-    PyObject *res = PyBytes_FromStringAndSize(data, size);
+#if PY_SSIZE_T_MAX < 9223372036854775807
+    size = Py_MIN(size, INT_MAX);
+#endif
+    PyObject *res = PyBytes_FromStringAndSize(data, (Py_ssize_t)size);
     if (!(flags & SQLITE_SERIALIZE_NOCOPY)) {
         sqlite3_free((void *)data);
     }


Code review comments:
diff_hunk: @@ -2091,7 +2091,10 @@ serialize_impl(pysqlite_Connection *self, const char *name)
                      name);
         return NULL;
     }
-    PyObject *res = PyBytes_FromStringAndSize(data, size);
+#if PY_SSIZE_T_MAX < 9223372036854775807
+    size = Py_MIN(size, INT_MAX);
+#endif
path: Modules/_sqlite/connection.c
text: @user1:
```suggestion
```