From: Mika Lindqvist Date: Fri, 18 Jun 2021 23:08:20 +0000 (+0300) Subject: [chunkcopy_safe] Don't call chunkcopy(). X-Git-Tag: 2.1.0-beta1~558 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4521023854777be478bb187ffc051cbf0a1687cf;p=thirdparty%2Fzlib-ng.git [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. --- diff --git a/chunkset_tpl.h b/chunkset_tpl.h index 2026ff37c..256475a64 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