Quoting CHANGES from SWIG 4.2.0:

“
2023-12-20: wsfulton
            #2190 Replace SWIG_Python_str_AsChar with 
SWIG_PyUnicode_AsUTF8AndSize.

            SWIG_Python_str_AsChar has undefined behaviour when Py_LIMITED_API 
is defined
            as it returns a pointer to a string in a PyBytes object that no 
longer exists.

            SWIG_PyUnicode_AsUTF8AndSize is an efficient replacement, but 
requires a
            different API and the caller to decrement the refcount on the 
intermediate
            PyObject in the Py_LIMITED_API < 0x030A0000 implementation. The 
alternative
            would have required copying the returned char * string as was done 
in a
            previous implementation requiring a call to the defunct 
SWIG_Python_str_DelForPy3
            function.

            *** POTENTIAL INCOMPATIBILITY ***
”

This function is somewhat widely used:

  <https://codesearch.debian.net/search?q=SWIG_Python_str_AsChar&literal=1>

I tried to fix the ldns bindings like this:

diff --git a/contrib/python/ldns_rdf.i b/contrib/python/ldns_rdf.i
index 5d7448fd..60daf1a7 100644
--- a/contrib/python/ldns_rdf.i
+++ b/contrib/python/ldns_rdf.i
@@ -56,7 +56,11 @@
  */
 %typemap(arginit, noblock=1) const ldns_rdf *
 {
+#if SWIG_VERSION >= 0x040200
+  PyObject *$1_bytes = NULL;
+#else
   char *$1_str = NULL;
+#endif
 }
 
 /*
@@ -66,11 +70,17 @@
 %typemap(in, noblock=1) const ldns_rdf * (void* argp, $1_ltype tmp = 0, int 
res)
 {
   if (Python_str_Check($input)) {
+    const char *argstr;
+#if SWIG_VERSION >= 0x040200
+    argstr = SWIG_PyUnicode_AsUTF8AndSize($input, NULL, &$1_bytes);
+#else
     $1_str = SWIG_Python_str_AsChar($input);
-    if ($1_str == NULL) {
+    argstr = $1_str;
+#endif
+    if (argstr == NULL) {
       %argument_fail(SWIG_TypeError, "char *", $symname, $argnum);
     }
-    tmp = ldns_dname_new_frm_str($1_str);
+    tmp = ldns_dname_new_frm_str(argstr);
     if (tmp == NULL) {
       %argument_fail(SWIG_TypeError, "char *", $symname, $argnum);
     }
@@ -90,10 +100,17 @@
  */
 %typemap(freearg, noblock=1) const ldns_rdf *
 {
+#if SWIG_VERSION >= 0x040200
+  if ($1_bytes != NULL) {
+    /* Is not NULL only when a conversion form string occurred. */
+    Py_XDECREF($1_bytes);
+  }
+#else
   if ($1_str != NULL) {
     /* Is not NULL only when a conversion form string occurred. */
     SWIG_Python_str_DelForPy3($1_str); /* Is a empty macro for Python < 3. */
   }
+#endif
 }
 
 %nodefaultctor ldns_struct_rdf; /* No default constructor. */

Submitted upstream as <https://github.com/NLnetLabs/ldns/pull/232>.
Maybe that will get some useful comments; I don't know much about SWIG.
(The core of the change above is the call to
SWIG_PyUnicode_AsUTF8AndSize and Py_XDECREF call for the bytes object.)

In the past, this kind of problem would have just compiled and resulted
in a run-time error when the Python extension module is loaded.  In some
cases, issues went completely unnoticed because the Python bindins were
unused.  But with GCC 14, it is necessary to fix calls to undeclared
functions.

Thanks,
Florian
--
_______________________________________________
devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]
Fedora Code of Conduct: 
https://docs.fedoraproject.org/en-US/project/code-of-conduct/
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: 
https://lists.fedoraproject.org/archives/list/[email protected]
Do not reply to spam, report it: 
https://pagure.io/fedora-infrastructure/new_issue

Reply via email to