diff options
author | Mika Lindqvist <postmaster@raasu.org> | 2021-06-19 02:08:20 +0300 |
---|---|---|
committer | Hans Kristian Rosbach <hk-git@circlestorm.org> | 2021-06-21 11:34:28 +0200 |
commit | c4511df98f535712f4d06eea22bff99db346b63f (patch) | |
tree | 506505d0fa1196df27fa47731e17902ef8cd9e27 /chunkset_tpl.h | |
parent | 089b2e7065b73cebb1f75099997d8f0c2194759d (diff) |
[chunkcopy_safe] Don't call chunkcopy().
* chunkcopy() can read or write more than the safe length if the length is not multiple of chunk size.
Diffstat (limited to 'chunkset_tpl.h')
-rw-r--r-- | chunkset_tpl.h | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/chunkset_tpl.h b/chunkset_tpl.h index 2026ff3..256475a 100644 --- a/chunkset_tpl.h +++ b/chunkset_tpl.h @@ -39,37 +39,46 @@ Z_INTERNAL uint8_t* CHUNKCOPY(uint8_t *out, uint8_t const *from, unsigned len) { /* Behave like chunkcopy, but avoid writing beyond of legal output. */ Z_INTERNAL uint8_t* CHUNKCOPY_SAFE(uint8_t *out, uint8_t const *from, unsigned len, uint8_t *safe) { len = MIN(len, safe - out + 1); - if (len < sizeof(chunk_t)) { -#if CHUNK_SIZE > 16 - if (len & 16) { - memcpy(out, from, 16); - out += 16; - from += 16; - } +#if CHUNK_SIZE >= 32 + while (len >= 32) { + memcpy(out, from, 32); + out += 32; + from += 32; + len -= 32; + } #endif -#if CHUNK_SIZE > 8 - if (len & 8) { - memcpy(out, from, 8); - out += 8; - from += 8; - } +#if CHUNK_SIZE >= 16 + while (len >= 16) { + memcpy(out, from, 16); + out += 16; + from += 16; + len -= 16; + } #endif - if (len & 4) { - memcpy(out, from, 4); - out += 4; - from += 4; - } - if (len & 2) { - memcpy(out, from, 2); - out += 2; - from += 2; - } - if (len & 1) { - *out++ = *from++; - } - return out; +#if CHUNK_SIZE >= 8 + while (len >= 8) { + memcpy(out, from, 8); + out += 8; + from += 8; + len -= 8; + } +#endif + if (len >= 4) { + memcpy(out, from, 4); + out += 4; + from += 4; + len -= 4; } - return CHUNKCOPY(out, from, len); + if (len >= 2) { + memcpy(out, from, 2); + out += 2; + from += 2; + len -= 2; + } + if (len == 1) { + *out++ = *from++; + } + return out; } /* Perform short copies until distance can be rewritten as being at least |