]> git.ipfire.org Git - thirdparty/git.git/commit
test-delta: use strbufs to hold input files
authorJeff King <peff@peff.net>
Thu, 24 Jul 2025 00:02:11 +0000 (20:02 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 24 Jul 2025 18:28:09 +0000 (11:28 -0700)
commit760dd804bb40b613396d25a0905db7fe4a52b00f
tree169db5f739eebc6ae2b612d9640fe5aacd2f42d7
parentbc235a68c87d92dc15e2d656a004e9b20042405f
test-delta: use strbufs to hold input files

We want to read the whole contents of two files into memory. If we
switch from raw ptr/len pairs to strbufs, we can use strbuf_read_file()
to shorten the code.

This incidentally fixes two small bugs:

  1. We stat() the files and allocate our buffers based on st.st_size.
     But that is an off_t which may be larger than the size_t we'd use
     to allocate. We should use xsize_t() to do a checked conversion.
     Otherwise integer truncation (on a file >4GB) could cause us to
     under-allocate (though in practice this does not result in a buffer
     overflow because the same truncation happens when read_in_full()
     also takes a size_t).

  2. We get the size from st.st_size, and then try to read_in_full()
     that many bytes. But it may return fewer bytes than expected (if
     the file changed racily and we get an early EOF), leading us to
     read uninitialized bytes in the allocated buffer. We don't notice
     because we only check the value for error, not that we got the
     expected number of bytes.

The strbuf code doesn't run into this, because it just reads to EOF,
expanding the buffer dynamically as necessary. Neither bug is a big deal
for a test helper, but fixing them is a nice bonus on top of simplifying
the code.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/helper/test-delta.c