]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
[chunkcopy_safe] Don't call chunkcopy().
authorMika Lindqvist <postmaster@raasu.org>
Fri, 18 Jun 2021 23:08:20 +0000 (02:08 +0300)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Mon, 21 Jun 2021 09:01:32 +0000 (11:01 +0200)
* chunkcopy() can read or write more than the safe length if the length is not multiple of chunk size.

chunkset_tpl.h

index 2026ff37ccd41fb9adf8b1a2845d308c80540d08..256475a641a29fa010eb08131e940ae09ca59a65 100644 (file)
@@ -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