@@ -368,33 +368,54 @@ Tlsh_fromTlshStr(tlsh_TlshObject *self, PyObject *args)
368
368
369
369
arg = PyTuple_GetItem (args, 0 );
370
370
#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 ;
374
+ }
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 ;
374
380
}
381
+ arg = asciiStr;
375
382
#else
376
383
if (!PyString_Check (arg)) {
377
- PyErr_SetString (PyExc_ValueError, " argument is not a TLSH hex string" );
378
- return NULL ;
384
+ PyErr_SetString (PyExc_ValueError, " argument is not a TLSH hex string" );
385
+ return NULL ;
379
386
}
380
387
#endif
381
388
382
389
if (PyBytes_AsStringAndSize (arg, &str, &len) == -1 ) {
383
- PyErr_SetString (PyExc_ValueError, " argument is not a TLSH hex string" );
390
+ PyErr_SetString (PyExc_ValueError, " failed to extract string and size" );
391
+ #if PY_MAJOR_VERSION >= 3
392
+ Py_XDECREF (asciiStr);
393
+ #endif
384
394
return NULL ;
385
395
}
386
396
387
- if ((len != TLSH_STRING_LEN_REQ) && (len != TLSH_STRING_LEN_REQ- 2 )) {
397
+ if ((len != TLSH_STRING_LEN_REQ) && (len != TLSH_STRING_LEN_REQ - 2 )) {
388
398
PyErr_SetString (PyExc_ValueError, " argument length incorrect: not a TLSH hex string" );
399
+ #if PY_MAJOR_VERSION >= 3
400
+ Py_XDECREF (asciiStr);
401
+ #endif
389
402
return NULL ;
390
403
}
391
404
392
405
if (self->tlsh .fromTlshStr (str) != 0 ) {
393
406
PyErr_SetString (PyExc_ValueError, " argument value incorrect: not a TLSH hex string" );
407
+ #if PY_MAJOR_VERSION >= 3
408
+ Py_XDECREF (asciiStr);
409
+ #endif
394
410
return NULL ;
395
411
}
412
+
396
413
self->finalized = true ;
397
414
415
+ #if PY_MAJOR_VERSION >= 3
416
+ Py_XDECREF (asciiStr);
417
+ #endif
418
+
398
419
Py_RETURN_NONE;
399
420
}
400
421
0 commit comments