Issue #16416: Fix error handling in _Py_wchar2char() _Py_char2wchar() functions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 42a532d..2cd75ce 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -67,10 +67,12 @@
 #ifdef __APPLE__
     wchar_t *wstr;
     wstr = _Py_DecodeUTF8_surrogateescape(arg, strlen(arg));
-    if (wstr == NULL)
-        return NULL;
-    if (size != NULL)
-        *size = wcslen(wstr);
+    if (size != NULL) {
+        if (wstr != NULL)
+            *size = wcslen(wstr);
+        else
+            *size = (size_t)-1;
+    }
     return wstr;
 #else
     wchar_t *res;
@@ -204,22 +206,25 @@
     char *cpath;
 
     unicode = PyUnicode_FromWideChar(text, wcslen(text));
-    if (unicode == NULL) {
-        Py_DECREF(unicode);
+    if (unicode == NULL)
         return NULL;
-    }
 
     bytes = _PyUnicode_AsUTF8String(unicode, "surrogateescape");
     Py_DECREF(unicode);
     if (bytes == NULL) {
         PyErr_Clear();
+        if (error_pos != NULL)
+            *error_pos = (size_t)-1;
         return NULL;
     }
 
     len = PyBytes_GET_SIZE(bytes);
     cpath = PyMem_Malloc(len+1);
     if (cpath == NULL) {
+        PyErr_Clear();
         Py_DECREF(bytes);
+        if (error_pos != NULL)
+            *error_pos = (size_t)-1;
         return NULL;
     }
     memcpy(cpath, PyBytes_AsString(bytes), len + 1);
@@ -231,9 +236,6 @@
     size_t i, size, converted;
     wchar_t c, buf[2];
 
-    if (error_pos != NULL)
-        *error_pos = (size_t)-1;
-
     /* The function works in two steps:
        1. compute the length of the output buffer in bytes (size)
        2. outputs the bytes */
@@ -280,8 +282,11 @@
 
         size += 1; /* nul byte at the end */
         result = PyMem_Malloc(size);
-        if (result == NULL)
+        if (result == NULL) {
+            if (error_pos != NULL)
+                *error_pos = (size_t)-1;
             return NULL;
+        }
         bytes = result;
     }
     return result;