Skip to content

Commit e7919f0

Browse files
committed
fix: add missing nulls
1 parent 68d3cc8 commit e7919f0

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

src/kasa_crypt/_crypt_impl.pyx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ cdef extern from "crypt_wrapper.h":
88
void _decrypt_into(const char * encrypted, char * unencrypted)
99

1010
cdef char* _decrypt(const char *encrypted):
11-
cdef char* unencrypted = <char *> malloc((len(encrypted)) * sizeof(char))
11+
cdef char* unencrypted = <char *> malloc(sizeof(encrypted))
1212
if not unencrypted:
1313
return NULL # malloc failed
1414
_decrypt_into(encrypted, unencrypted)
1515
return unencrypted
1616

1717
cdef char* _encrypt(const char *unencrypted):
18-
cdef char* encrypted = <char *> malloc((len(unencrypted)) * sizeof(char))
18+
cdef char* encrypted = <char *> malloc(sizeof(unencrypted))
1919
if not encrypted:
2020
return NULL # malloc failed
2121
_encrypt_into(unencrypted, encrypted)

src/kasa_crypt/crypt_wrapper.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@
88
void _encrypt_into(const char * unencrypted, char * encrypted) {
99
uint8_t unencrypted_byte;
1010
uint8_t key = 171;
11-
for(unsigned i = 0; i < strlen(unencrypted); i++) {
11+
unsigned long len = strlen(unencrypted);
12+
for(unsigned i = 0; i < len; i++) {
1213
unencrypted_byte = unencrypted[i];
1314
key = key ^ unencrypted_byte;
1415
encrypted[i] = key;
1516
}
17+
encrypted[len] = '\0';
1618
}
1719
void _decrypt_into(const char * encrypted, char * unencrypted) {
1820
uint8_t unencrypted_byte;
1921
uint8_t encrypted_byte;
2022
uint8_t key = 171;
23+
unsigned long len = strlen(encrypted);
2124
for(unsigned i = 0; i < strlen(encrypted); i++) {
2225
encrypted_byte = encrypted[i];
2326
unencrypted_byte = key ^ encrypted_byte;
2427
key = encrypted_byte;
2528
unencrypted[i] = unencrypted_byte;
2629
}
30+
unencrypted[len] = '\0';
2731
}

0 commit comments

Comments
 (0)