diff options
| author | Patrick Steinhardt <ps@pks.im> | 2019-09-19 12:46:37 +0200 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2019-09-21 15:20:28 +0200 |
| commit | 208f1d7a4d9603ec6781ed18c10b240c7c5d2e33 (patch) | |
| tree | 5d2457771c5b49defed1a098cc96766ff9e17312 /tests/diff/parse.c | |
| parent | 3e8a17b01b30a5aca5e10ef6f02635111315724e (diff) | |
| download | libgit2-208f1d7a4d9603ec6781ed18c10b240c7c5d2e33.tar.gz | |
buffer: fix infinite loop when growing buffers
When growing buffers, we repeatedly multiply the currently allocated
number of bytes by 1.5 until it exceeds the requested number of bytes.
This has two major problems:
1. If the current number of bytes is tiny and one wishes to resize
to a comparatively huge number of bytes, then we may need to loop
thousands of times.
2. If resizing to a value close to `SIZE_MAX` (which would fail
anyway), then we probably hit an infinite loop as multiplying the
current amount of bytes will repeatedly result in integer
overflows.
When reallocating buffers, one typically chooses values close to 1.5 to
enable re-use of resulting memory holes in later reallocations. But
because of this, it really only makes sense to use a factor of 1.5
_once_, but not looping until we finally are able to fit it. Thus, we
can completely avoid the loop and just opt for the much simpler
algorithm of multiplying with 1.5 once and, if the result doesn't fit,
just use the target size. This avoids both problems of looping
extensively and hitting overflows.
This commit also adds a test that would've previously resulted in an
infinite loop.
Diffstat (limited to 'tests/diff/parse.c')
0 files changed, 0 insertions, 0 deletions
