]> sigrok.org Git - libsigrokdecode.git/commitdiff
type_decoder: fixup memory leak in Decoder.put() (annotation, binary)
authorGerhard Sittig <redacted>
Tue, 8 May 2018 18:44:05 +0000 (20:44 +0200)
committerUwe Hermann <redacted>
Tue, 8 May 2018 23:32:37 +0000 (01:32 +0200)
The text presentation of decoder annotations' payload data was allocated
but not freed. As were the byte strings of binary output. Fix it.

This fixes parts of bug #329.

type_decoder.c

index 880dbade0a7697d054308001b1e3a66b988d829c..79e9c4924ebd3083605037a0b42b051a53bcf024 100644 (file)
@@ -40,6 +40,15 @@ static const char *output_type_name(unsigned int idx)
        return names[MIN(idx, G_N_ELEMENTS(names) - 1)];
 }
 
        return names[MIN(idx, G_N_ELEMENTS(names) - 1)];
 }
 
+static void release_annotation(struct srd_proto_data_annotation *pda)
+{
+       if (!pda)
+               return;
+       if (pda->ann_text)
+               g_strfreev(pda->ann_text);
+       g_free(pda);
+}
+
 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
                struct srd_proto_data *pdata)
 {
 static int convert_annotation(struct srd_decoder_inst *di, PyObject *obj,
                struct srd_proto_data *pdata)
 {
@@ -112,6 +121,15 @@ err:
        return SRD_ERR_PYTHON;
 }
 
        return SRD_ERR_PYTHON;
 }
 
+static void release_binary(struct srd_proto_data_binary *pdb)
+{
+       if (!pdb)
+               return;
+       if (pdb->data)
+               g_free((void *)pdb->data);
+       g_free(pdb);
+}
+
 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
                struct srd_proto_data *pdata)
 {
 static int convert_binary(struct srd_decoder_inst *di, PyObject *obj,
                struct srd_proto_data *pdata)
 {
@@ -287,6 +305,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
+                       release_annotation(pdata.data);
                }
                break;
        case SRD_OUTPUT_PYTHON:
                }
                break;
        case SRD_OUTPUT_PYTHON:
@@ -319,6 +338,7 @@ static PyObject *Decoder_put(PyObject *self, PyObject *args)
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
                        Py_BEGIN_ALLOW_THREADS
                        cb->cb(&pdata, cb->cb_data);
                        Py_END_ALLOW_THREADS
+                       release_binary(pdata.data);
                }
                break;
        case SRD_OUTPUT_META:
                }
                break;
        case SRD_OUTPUT_META: