commit 9439b99a9a4c551109d11217916f1d5118ca556d from: Stefan Sperling via: Thomas Adam date: Sun Oct 30 17:15:57 2022 UTC avoid looping over SHA1Update() in got-fetch-pack; suggested by millert@ commit - 4231e6b557d72b2ae13ede2aecdd6391f271f25e commit + 9439b99a9a4c551109d11217916f1d5118ca556d blob - 79f6e3091c9517ab7d3915a6f020ce41a11e0660 blob + 16b72acf9a498e271bc42310058798b44aa39782 --- libexec/got-fetch-pack/got-fetch-pack.c +++ libexec/got-fetch-pack/got-fetch-pack.c @@ -51,6 +51,10 @@ #include "got_lib_pkt.h" #include "got_lib_gitproto.h" #include "got_lib_ratelimit.h" + +#ifndef MIN +#define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b)) +#endif #ifndef nitems #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0])) @@ -685,12 +689,14 @@ fetch_pack(int fd, int packfd, uint8_t *pack_sha1, * amount of bytes out at the front to make * room, mixing those bytes into the checksum. */ - while (sha1_buf_len > 0 && + if (sha1_buf_len > 0 && sha1_buf_len + r > SHA1_DIGEST_LENGTH) { - SHA1Update(&sha1_ctx, sha1_buf, 1); - memmove(sha1_buf, sha1_buf + 1, - sha1_buf_len - 1); - sha1_buf_len--; + size_t nshift = MIN(sha1_buf_len + r - + SHA1_DIGEST_LENGTH, sha1_buf_len); + SHA1Update(&sha1_ctx, sha1_buf, nshift); + memmove(sha1_buf, sha1_buf + nshift, + sha1_buf_len - nshift); + sha1_buf_len -= nshift; } /* Buffer potential checksum bytes. */