Blame SOURCES/gpgme-1.13.1-fix-resource-leaks.patch

c440c0
diff --git a/lang/cpp/src/context.cpp b/lang/cpp/src/context.cpp
c440c0
index c0a1dc21..e991dbc8 100644
c440c0
--- a/lang/cpp/src/context.cpp
c440c0
+++ b/lang/cpp/src/context.cpp
c440c0
@@ -223,6 +223,7 @@ Context *Context::createForProtocol(Protocol proto)
c440c0
         }
c440c0
         break;
c440c0
     default:
c440c0
+        gpgme_release(ctx);
c440c0
         return nullptr;
c440c0
     }
c440c0
 
c440c0
@@ -273,6 +274,7 @@ std::unique_ptr<Context> Context::createForEngine(Engine eng, Error *error)
c440c0
         }
c440c0
         break;
c440c0
     default:
c440c0
+        gpgme_release(ctx);
c440c0
         if (error) {
c440c0
             *error = Error::fromCode(GPG_ERR_INV_ARG);
c440c0
         }
c440c0
diff --git a/lang/cpp/src/data.cpp b/lang/cpp/src/data.cpp
c440c0
index 7a93cbc2..d08a29db 100644
c440c0
--- a/lang/cpp/src/data.cpp
c440c0
+++ b/lang/cpp/src/data.cpp
c440c0
@@ -249,6 +249,7 @@ std::vector<GpgME::Key> GpgME::Data::toKeys(Protocol proto) const
c440c0
     }
c440c0
 
c440c0
     if (gpgme_op_keylist_from_data_start (ctx->impl()->ctx, d->data, 0)) {
c440c0
+        delete ctx;
c440c0
         return ret;
c440c0
     }
c440c0
 
c440c0
diff --git a/lang/python/gpgme.i b/lang/python/gpgme.i
c440c0
index 87371af8..64b88d54 100644
c440c0
--- a/lang/python/gpgme.i
c440c0
+++ b/lang/python/gpgme.i
c440c0
@@ -53,7 +53,7 @@
c440c0
     {
c440c0
       encodedInput = PyUnicode_AsUTF8String($input);
c440c0
       if (encodedInput == NULL)
c440c0
-        return NULL;
c440c0
+        SWIG_fail;
c440c0
       $1 = PyBytes_AsString(encodedInput);
c440c0
     }
c440c0
   else if (PyBytes_Check($input))
c440c0
@@ -62,22 +62,25 @@
c440c0
     PyErr_Format(PyExc_TypeError,
c440c0
                  "arg %d: expected str, bytes, or None, got %s",
c440c0
 		 $argnum, $input->ob_type->tp_name);
c440c0
-    return NULL;
c440c0
+    SWIG_fail;
c440c0
   }
c440c0
 }
c440c0
 %typemap(freearg) const char * {
c440c0
   Py_XDECREF(encodedInput$argnum);
c440c0
 }
c440c0
 
c440c0
+%typemap(arginit) const char *[] {
c440c0
+  $1 = NULL;
c440c0
+}
c440c0
+
c440c0
 /* Likewise for a list of strings.  */
c440c0
-%typemap(in) const char *[] (void *vector = NULL,
c440c0
-                             size_t size,
c440c0
+%typemap(in) const char *[] (size_t size,
c440c0
                              PyObject **pyVector = NULL) {
c440c0
   /* Check if is a list */
c440c0
   if (PyList_Check($input)) {
c440c0
     size_t i, j;
c440c0
     size = PyList_Size($input);
c440c0
-    $1 = (char **) (vector = malloc((size+1) * sizeof(char *)));
c440c0
+    $1 = (char **) malloc((size+1) * sizeof(char *));
c440c0
     pyVector = calloc(sizeof *pyVector, size);
c440c0
 
c440c0
     for (i = 0; i < size; i++) {
c440c0
@@ -86,12 +89,7 @@
c440c0
         {
c440c0
           pyVector[i] = PyUnicode_AsUTF8String(o);
c440c0
           if (pyVector[i] == NULL)
c440c0
-            {
c440c0
-              free(vector);
c440c0
-              for (j = 0; j < i; j++)
c440c0
-                Py_XDECREF(pyVector[j]);
c440c0
-              return NULL;
c440c0
-            }
c440c0
+            SWIG_fail;
c440c0
           $1[i] = PyBytes_AsString(pyVector[i]);
c440c0
         }
c440c0
       else if (PyString_Check(o))
c440c0
@@ -101,8 +99,7 @@
c440c0
                      "arg %d: list must contain only str or bytes, got %s "
c440c0
                      "at position %d",
c440c0
                      $argnum, o->ob_type->tp_name, i);
c440c0
-	free($1);
c440c0
-	return NULL;
c440c0
+	SWIG_fail;
c440c0
       }
c440c0
     }
c440c0
     $1[i] = NULL;
c440c0
@@ -110,14 +107,17 @@
c440c0
     PyErr_Format(PyExc_TypeError,
c440c0
                  "arg %d: expected a list of str or bytes, got %s",
c440c0
                  $argnum, $input->ob_type->tp_name);
c440c0
-    return NULL;
c440c0
+    SWIG_fail;
c440c0
   }
c440c0
 }
c440c0
 %typemap(freearg) const char *[] {
c440c0
-  size_t i;
c440c0
-  free(vector$argnum);
c440c0
-  for (i = 0; i < size$argnum; i++)
c440c0
-    Py_XDECREF(pyVector$argnum[i]);
c440c0
+  if (pyVector$argnum) {
c440c0
+    size_t i;
c440c0
+    for (i = 0; i < size$argnum; i++)
c440c0
+      Py_XDECREF(pyVector$argnum[i]);
c440c0
+    free(pyVector$argnum);
c440c0
+  }
c440c0
+  if ($1) free($1);
c440c0
 }
c440c0
 
c440c0
 /* Release returned buffers as necessary.  */
c440c0
@@ -135,7 +135,7 @@
c440c0
   if (!PySequence_Check($input)) {
c440c0
     PyErr_Format(PyExc_ValueError, "arg %d: Expected a list of gpgme_key_t",
c440c0
 		 $argnum);
c440c0
-    return NULL;
c440c0
+    SWIG_fail;
c440c0
   }
c440c0
   if((numb = PySequence_Length($input)) != 0) {
c440c0
     $1 = (gpgme_key_t*)malloc((numb+1)*sizeof(gpgme_key_t));
c440c0
@@ -152,8 +152,7 @@
c440c0
                      "arg %d: list must contain only gpgme_key_ts, got %s "
c440c0
                      "at position %d",
c440c0
                      $argnum, pypointer->ob_type->tp_name, i);
c440c0
-        free($1);
c440c0
-	return NULL;
c440c0
+	SWIG_fail;
c440c0
       }
c440c0
       Py_DECREF(pypointer);
c440c0
     }
c440c0
@@ -179,7 +178,7 @@
c440c0
     pypointer = _gpg_obj2gpgme_data_t($input, $argnum, &wrapper,
c440c0
                                        &bytesio, &view);
c440c0
     if (pypointer == NULL)
c440c0
-      return NULL;
c440c0
+      SWIG_fail;
c440c0
     have_view = !! view.obj;
c440c0
 
c440c0
     /* input = $input, 1 = $1, 1_descriptor = $1_descriptor */
c440c0
@@ -189,7 +188,7 @@
c440c0
     if ((SWIG_ConvertPtr(pypointer,(void **) &$1, $1_descriptor,
c440c0
          SWIG_POINTER_EXCEPTION | $disown )) == -1) {
c440c0
       Py_DECREF(pypointer);
c440c0
-      return NULL;
c440c0
+      SWIG_fail;
c440c0
     }
c440c0
     Py_DECREF(pypointer);
c440c0
   }
c440c0
@@ -346,6 +345,11 @@
c440c0
     PyErr_SetString(PyExc_TypeError, "Numeric argument expected");
c440c0
 }
c440c0
 
c440c0
+%typemap(arginit) (void *buffer, size_t size), (char *buf, size_t buflen) {
c440c0
+  $1 = NULL;
c440c0
+  $2 = 0;
c440c0
+}
c440c0
+
c440c0
 /* Those are for gpgme_data_read() and gpgme_strerror_r().  */
c440c0
 %typemap(in) (void *buffer, size_t size), (char *buf, size_t buflen) {
c440c0
   {
c440c0
@@ -359,12 +363,12 @@
c440c0
     else
c440c0
       {
c440c0
         PyErr_SetString(PyExc_TypeError, "Numeric argument expected");
c440c0
-        return NULL;
c440c0
+        SWIG_fail;
c440c0
       }
c440c0
 
c440c0
     if (tmp$argnum < 0) {
c440c0
       PyErr_SetString(PyExc_ValueError, "Positive integer expected");
c440c0
-      return NULL;
c440c0
+      SWIG_fail;
c440c0
     }
c440c0
     $2 = (size_t) tmp$argnum;
c440c0
     $1 = ($1_ltype) malloc($2+1);
c440c0
@@ -373,11 +377,11 @@
c440c0
 %typemap(argout) (void *buffer, size_t size), (char *buf, size_t buflen) {
c440c0
   Py_XDECREF($result);   /* Blow away any previous result */
c440c0
   if (result < 0) {      /* Check for I/O error */
c440c0
-    free($1);
c440c0
+    if ($1) free($1);
c440c0
     return PyErr_SetFromErrno(PyExc_RuntimeError);
c440c0
   }
c440c0
   $result = PyBytes_FromStringAndSize($1,result);
c440c0
-  free($1);
c440c0
+  if ($1) free($1);
c440c0
 }
c440c0
 
c440c0
 /* For gpgme_data_write, but should be universal.  */
c440c0
@@ -390,11 +394,11 @@
c440c0
     {
c440c0
       encodedInput = PyUnicode_AsUTF8String($input);
c440c0
       if (encodedInput == NULL)
c440c0
-        return NULL;
c440c0
+        SWIG_fail;
c440c0
       if (PyBytes_AsStringAndSize(encodedInput, (char **) &$1, &ssize) == -1)
c440c0
         {
c440c0
           Py_DECREF(encodedInput);
c440c0
-          return NULL;
c440c0
+          SWIG_fail;
c440c0
         }
c440c0
     }
c440c0
   else if (PyBytes_Check($input))
c440c0
@@ -403,7 +407,7 @@
c440c0
     PyErr_Format(PyExc_TypeError,
c440c0
                  "arg %d: expected str, bytes, or None, got %s",
c440c0
 		 $argnum, $input->ob_type->tp_name);
c440c0
-    return NULL;
c440c0
+    SWIG_fail;
c440c0
   }
c440c0
 
c440c0
   if (! $1)
c440c0
@@ -432,8 +436,17 @@
c440c0
     size++;
c440c0
   }
c440c0
   $result = PyList_New(size);
c440c0
+  if ($result == NULL)
c440c0
+    SWIG_fail;
c440c0
   for (i=0,curr=$1; i<size; i++,curr=curr->next) {
c440c0
     PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(curr), $1_descriptor, %newpointer_flags);
c440c0
+    if (o == NULL) {
c440c0
+      int j;
c440c0
+      for (j = 0; j < i; j++)
c440c0
+        Py_XDECREF(PyList_GetItem($result, j));
c440c0
+      Py_DECREF($result);
c440c0
+      SWIG_fail;
c440c0
+    }
c440c0
     PyList_SetItem($result, i, o);
c440c0
   }
c440c0
 }
c440c0
@@ -446,6 +459,8 @@
c440c0
   PyObject *fragile;
c440c0
   fragile = SWIG_NewPointerObj(SWIG_as_voidptr($1), $1_descriptor,
c440c0
                                %newpointer_flags);
c440c0
+  if (fragile == NULL)
c440c0
+    SWIG_fail;
c440c0
   $result = _gpg_wrap_result(fragile, name);
c440c0
   Py_DECREF(fragile);
c440c0
 }
c440c0
@@ -469,22 +484,28 @@ wrapresult(gpgme_vfs_mount_result_t, "VFSMountResult")
c440c0
   }
c440c0
   $result = PyList_New(size);
c440c0
   if ($result == NULL)
c440c0
-    return NULL;	/* raise */
c440c0
+    SWIG_fail;	/* raise */
c440c0
   for (i=0,curr=$1; i<size; i++,curr=curr->next) {
c440c0
     PyObject *fragile, *o;
c440c0
     fragile = SWIG_NewPointerObj(SWIG_as_voidptr(curr), $1_descriptor,
c440c0
                                  %newpointer_flags);
c440c0
     if (fragile == NULL)
c440c0
       {
c440c0
+        int j;
c440c0
+        for (j = 0; j < i; j++)
c440c0
+          Py_XDECREF(PyList_GetItem($result, j));
c440c0
         Py_DECREF($result);
c440c0
-        return NULL;	/* raise */
c440c0
+        SWIG_fail;	/* raise */
c440c0
       }
c440c0
     o = _gpg_wrap_result(fragile, "EngineInfo");
c440c0
     Py_DECREF(fragile);
c440c0
     if (o == NULL)
c440c0
       {
c440c0
+        int j;
c440c0
+        for (j = 0; j < i; j++)
c440c0
+          Py_XDECREF(PyList_GetItem($result, j));
c440c0
         Py_DECREF($result);
c440c0
-        return NULL;	/* raise */
c440c0
+        SWIG_fail;	/* raise */
c440c0
       }
c440c0
     PyList_SetItem($result, i, o);
c440c0
   }
c440c0
diff --git a/src/engine-gpg.c b/src/engine-gpg.c
c440c0
index dc2d9455..a1e40fe2 100644
c440c0
--- a/src/engine-gpg.c
c440c0
+++ b/src/engine-gpg.c
c440c0
@@ -1122,6 +1122,7 @@ build_argv (engine_gpg_t gpg, const char *pgmname)
c440c0
                    to avoid and given that we reach this here only
c440c0
                    after a malloc failure for a small object, it is
c440c0
                    probably better not to do anything.  */
c440c0
+		free_argv (argv);
c440c0
 		return gpg_error (GPG_ERR_GENERAL);
c440c0
 	      }
c440c0
 	    /* If the data_type is FD, we have to do a dup2 here.  */
c440c0
@@ -1239,6 +1240,9 @@ read_status (engine_gpg_t gpg)
c440c0
       buffer = realloc (buffer, bufsize);
c440c0
       if (!buffer)
c440c0
 	return gpg_error_from_syserror ();
c440c0
+      /* Update buffer and bufsize here to prevent memory leaks.  */
c440c0
+      gpg->status.buffer = buffer;
c440c0
+      gpg->status.bufsize = bufsize;
c440c0
     }
c440c0
 
c440c0
   nread = _gpgme_io_read (gpg->status.fd[0],
c440c0
@@ -1351,8 +1355,6 @@ read_status (engine_gpg_t gpg)
c440c0
     }
c440c0
 
c440c0
   /* Update the gpg object.  */
c440c0
-  gpg->status.bufsize = bufsize;
c440c0
-  gpg->status.buffer = buffer;
c440c0
   gpg->status.readpos = readpos;
c440c0
   return 0;
c440c0
 }
c440c0
@@ -1392,6 +1394,9 @@ read_colon_line (engine_gpg_t gpg)
c440c0
       buffer = realloc (buffer, bufsize);
c440c0
       if (!buffer)
c440c0
 	return gpg_error_from_syserror ();
c440c0
+      /* Prevent memory leaks.  */
c440c0
+      gpg->colon.bufsize = bufsize;
c440c0
+      gpg->colon.buffer  = buffer;
c440c0
     }
c440c0
 
c440c0
   nread = _gpgme_io_read (gpg->colon.fd[0], buffer+readpos, bufsize-readpos);
c440c0
@@ -1471,8 +1476,6 @@ read_colon_line (engine_gpg_t gpg)
c440c0
     }
c440c0
 
c440c0
   /* Update the gpg object.  */
c440c0
-  gpg->colon.bufsize = bufsize;
c440c0
-  gpg->colon.buffer  = buffer;
c440c0
   gpg->colon.readpos = readpos;
c440c0
   return 0;
c440c0
 }
c440c0
diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c
c440c0
index ae5d8ef1..1f603f19 100644
c440c0
--- a/src/engine-gpgsm.c
c440c0
+++ b/src/engine-gpgsm.c
c440c0
@@ -1533,8 +1533,10 @@ gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode,
c440c0
   gpgsm->output_cb.data = keydata;
c440c0
   err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor"
c440c0
 		      : map_data_enc (gpgsm->output_cb.data));
c440c0
-  if (err)
c440c0
+  if (err) {
c440c0
+    free (cmd);
c440c0
     return err;
c440c0
+  }
c440c0
   gpgsm_clear_fd (gpgsm, INPUT_FD);
c440c0
   gpgsm_clear_fd (gpgsm, MESSAGE_FD);
c440c0
   gpgsm->inline_data = NULL;
c440c0
@@ -1634,8 +1636,10 @@ gpgsm_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode,
c440c0
   gpgsm->output_cb.data = keydata;
c440c0
   err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor"
c440c0
 		      : map_data_enc (gpgsm->output_cb.data));
c440c0
-  if (err)
c440c0
+  if (err) {
c440c0
+    free (line);
c440c0
     return err;
c440c0
+  }
c440c0
   gpgsm_clear_fd (gpgsm, INPUT_FD);
c440c0
   gpgsm_clear_fd (gpgsm, MESSAGE_FD);
c440c0
   gpgsm->inline_data = NULL;
c440c0
diff --git a/src/engine.c b/src/engine.c
c440c0
index 05979c15..7e2e3800 100644
c440c0
--- a/src/engine.c
c440c0
+++ b/src/engine.c
c440c0
@@ -460,7 +460,8 @@ _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t proto,
c440c0
       if (!new_version)
c440c0
         {
c440c0
           free (new_file_name);
c440c0
-          free (new_home_dir);
c440c0
+          if (new_home_dir)
c440c0
+            free (new_home_dir);
c440c0
           return gpg_error_from_syserror ();
c440c0
         }
c440c0
     }
c440c0
diff --git a/src/gpgme-tool.c b/src/gpgme-tool.c
c440c0
index 7a0bfcb3..f4ddd83b 100644
c440c0
--- a/src/gpgme-tool.c
c440c0
+++ b/src/gpgme-tool.c
c440c0
@@ -1189,6 +1189,7 @@ gt_get_key (gpgme_tool_t gt, const char *pattern, gpgme_key_t *r_key)
c440c0
                  similar hack to sort out such duplicates but it can't
c440c0
                  do that while listing keys.  */
c440c0
               gpgme_key_unref (key);
c440c0
+              key = NULL;
c440c0
               goto try_next_key;
c440c0
             }
c440c0
 	  if (!err)
c440c0
diff --git a/src/keylist.c b/src/keylist.c
c440c0
index cdb115fd..b7cbf3c3 100644
c440c0
--- a/src/keylist.c
c440c0
+++ b/src/keylist.c
c440c0
@@ -1321,6 +1321,7 @@ gpgme_get_key (gpgme_ctx_t ctx, const char *fpr, gpgme_key_t *r_key,
c440c0
                  similar hack to sort out such duplicates but it can't
c440c0
                  do that while listing keys.  */
c440c0
               gpgme_key_unref (key);
c440c0
+              key = NULL;
c440c0
               goto try_next_key;
c440c0
             }
c440c0
 	  if (!err)