From 532bd3c195f47d91f50ffa23ab4b2b045c5de22c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 15 Jun 2023 19:42:22 -1000 Subject: [PATCH] fix: handle 0s in the byte stream --- src/kasa_crypt/_crypt_impl.pyx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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'))