Skip to content

Commit aa0a472

Browse files
committed
Fix keyexpr memory access issues
1 parent 3482ecf commit aa0a472

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/protocol/keyexpr.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ zp_keyexpr_canon_status_t __zp_canon_prefix(const char *start, size_t *len) {
146146
char const *next_slash;
147147

148148
do {
149-
next_slash = strchr(chunk_start, '/');
149+
next_slash = memchr(chunk_start, '/', _z_ptr_char_diff(end, chunk_start));
150150
const char *chunk_end = next_slash ? next_slash : end;
151151
size_t chunk_len = _z_ptr_char_diff(chunk_end, chunk_start);
152152
switch (chunk_len) {
@@ -291,7 +291,7 @@ void __zp_ke_write_chunk(char **writer, const char *chunk, size_t len, const cha
291291
writer[0] = _z_ptr_char_offset(writer[0], 1);
292292
}
293293

294-
(void)memcpy(writer[0], chunk, len);
294+
(void)memmove(writer[0], chunk, len);
295295
writer[0] = _z_ptr_char_offset(writer[0], (ptrdiff_t)len);
296296
}
297297

@@ -766,10 +766,9 @@ zp_keyexpr_canon_status_t _z_keyexpr_canonize(char *start, size_t *len) {
766766
} else {
767767
assert(false); // anything before "$*" or "**" must be part of the canon prefix
768768
}
769-
770769
while (next_slash != NULL) {
771770
reader = _z_ptr_char_offset(next_slash, 1);
772-
next_slash = strchr(reader, '/');
771+
next_slash = memchr(reader, '/', _z_ptr_char_diff(end, reader));
773772
chunk_end = next_slash ? next_slash : end;
774773
switch (_z_ptr_char_diff(chunk_end, reader)) {
775774
case 0: {

tests/z_keyexpr_test.c

+2
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ void test_canonize(void) {
314314
printf(" Match: %.*s : %s\n", (int)canon_len, canon, canonized[i]);
315315
assert(strncmp(canonized[i], canon, canon_len) == 0);
316316
}
317+
free(canon);
317318
}
318319

319320
for (int i = 0; i < N; i++) {
@@ -331,6 +332,7 @@ void test_canonize(void) {
331332
assert(strncmp(canonized[i], canon, canon_len) == 0);
332333
}
333334
printf("\n");
335+
free(canon);
334336
}
335337
}
336338

0 commit comments

Comments
 (0)