Skip to content

Commit 96536e3

Browse files
authored
Merge pull request #137 from Sryborg/memory_leak_fix
[Fix] Memory Leak in py-tlsh
2 parents 25bc5d2 + 0a62e5d commit 96536e3

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

‎py_ext/tlshmodule.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,17 @@ Tlsh_fromTlshStr(tlsh_TlshObject *self, PyObject *args)
368368

369369
arg = PyTuple_GetItem(args, 0);
370370
#if PY_MAJOR_VERSION >= 3
371-
if (!PyUnicode_Check(arg) || (arg = PyUnicode_AsASCIIString(arg)) == NULL) {
372-
PyErr_SetString(PyExc_ValueError, "argument is not a TLSH hex string");
373-
return NULL;
371+
if (!PyUnicode_Check(arg)) {
372+
PyErr_SetString(PyExc_ValueError, "argument is not a TLSH hex string");
373+
return NULL;
374374
}
375+
376+
PyObject *asciiStr = PyUnicode_AsASCIIString(arg);
377+
if (asciiStr == NULL) {
378+
PyErr_SetString(PyExc_ValueError, "failed to convert argument to ASCII string");
379+
return NULL;
380+
}
381+
arg = asciiStr;
375382
#else
376383
if (!PyString_Check(arg)) {
377384
PyErr_SetString(PyExc_ValueError, "argument is not a TLSH hex string");
@@ -381,20 +388,34 @@ Tlsh_fromTlshStr(tlsh_TlshObject *self, PyObject *args)
381388

382389
if (PyBytes_AsStringAndSize(arg, &str, &len) == -1) {
383390
PyErr_SetString(PyExc_ValueError, "argument is not a TLSH hex string");
391+
#if PY_MAJOR_VERSION >= 3
392+
Py_XDECREF(asciiStr);
393+
#endif
384394
return NULL;
385395
}
386396

387397
if ((len != TLSH_STRING_LEN_REQ) && (len != TLSH_STRING_LEN_REQ-2)) {
388398
PyErr_SetString(PyExc_ValueError, "argument length incorrect: not a TLSH hex string");
399+
#if PY_MAJOR_VERSION >= 3
400+
Py_XDECREF(asciiStr);
401+
#endif
389402
return NULL;
390403
}
391404

392405
if (self->tlsh.fromTlshStr(str) != 0) {
393406
PyErr_SetString(PyExc_ValueError, "argument value incorrect: not a TLSH hex string");
407+
#if PY_MAJOR_VERSION >= 3
408+
Py_XDECREF(asciiStr);
409+
#endif
394410
return NULL;
395411
}
412+
396413
self->finalized = true;
397414

415+
#if PY_MAJOR_VERSION >= 3
416+
Py_XDECREF(asciiStr);
417+
#endif
418+
398419
Py_RETURN_NONE;
399420
}
400421

0 commit comments

Comments
 (0)