diff --git a/src/kasa_crypt/_crypt_impl.pyx b/src/kasa_crypt/_crypt_impl.pyx index b1dca93..12b6128 100644 --- a/src/kasa_crypt/_crypt_impl.pyx +++ b/src/kasa_crypt/_crypt_impl.pyx @@ -9,18 +9,20 @@ cdef extern from "crypt_wrapper.h": void _decrypt_into(const char * encrypted, char * unencrypted) cdef char* _decrypt(const char *encrypted): - cdef char* unencrypted = malloc(((strlen(encrypted)) + 1) * sizeof(char)) + cdef Py_ssize_t n = strlen(encrypted) + cdef char* unencrypted = malloc((n + 1) * sizeof(char)) if not unencrypted: return NULL # malloc failed _decrypt_into(encrypted, unencrypted) - return unencrypted + return unencrypted[:n] cdef char* _encrypt(const char *unencrypted): - cdef char* encrypted = malloc(((strlen(unencrypted)) + 1) * sizeof(char)) + cdef Py_ssize_t n = strlen(unencrypted) + cdef char* encrypted = malloc((n + 1) * sizeof(char)) if not encrypted: return NULL # malloc failed _encrypt_into(unencrypted, encrypted) - return encrypted + return encrypted[:n] def encrypt(string: str) -> bytes: return _encrypt(string.encode('utf-8'))