aboutsummaryrefslogtreecommitdiffstats
path: root/main/musl/0022-fix-overly-pessimistic-realloc-strategy-in-getdelim.patch
blob: c5aec3cc54b9f05fae67877559a040c007ec430f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
From c673158d91ad995ed59dd910777cd6464f61fe8e Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Sun, 20 Dec 2015 00:32:46 -0500
Subject: [PATCH] fix overly pessimistic realloc strategy in getdelim

previously, getdelim was allocating twice the space needed every time
it expanded its buffer to implement exponential buffer growth (in
order to avoid quadratic run time). however, this doubling was
performed even when the final buffer length needed was already known,
which is the common case that occurs whenever the delimiter is in the
FILE's buffer.

this patch makes two changes to remedy the situation:

1. over-allocation is no longer performed if the delimiter has already
been found when realloc is needed.

2. growth factor is reduced from 2x to 1.5x to reduce the relative
excess allocation in cases where the delimiter is not initially in the
buffer, including unbuffered streams.

in theory these changes could lead to quadratic time if the same
buffer is reused to process a sequence of lines successively
increasing in length, but once this length exceeds the stdio buffer
size, the delimiter will not be found in the buffer right away and
exponential growth will still kick in.
---
 src/stdio/getdelim.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/stdio/getdelim.c b/src/stdio/getdelim.c
index 813b09f..1ccd802 100644
--- a/src/stdio/getdelim.c
+++ b/src/stdio/getdelim.c
@@ -30,7 +30,7 @@ ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric
 		if (i+k+1 >= *n) {
 			if (k >= SIZE_MAX/2-i) goto oom;
 			size_t m = i+k+2;
-			if (m < SIZE_MAX/4) m *= 2;
+			if (!z && m < SIZE_MAX/4) m += m/2;
 			tmp = realloc(*s, m);
 			if (!tmp) {
 				m = i+k+2;
-- 
2.7.0