From 0aa71f890777d2aaddeca53384b94742e4b2678b Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Fri, 18 Sep 2020 16:48:53 -0400 Subject: util: Make the initial allocation bigger for xreadlinkat() Most symlinks are more than 1 character long, so rather than wasting time with 1, 2, 4, ... when we don't have a good guess for the length, start with a bigger one. Credit to https://twitter.com/RichFelker/status/1306321019108556806 for reminding me of this. --- util.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/util.c b/util.c index 0ac1f7a..715396c 100644 --- a/util.c +++ b/util.c @@ -59,10 +59,15 @@ int xreaddir(DIR *dir, struct dirent **de) { } char *xreadlinkat(int fd, const char *path, size_t size) { - ++size; // NUL-terminator ssize_t len; char *name = NULL; + if (size == 0) { + size = 64; + } else { + ++size; // NUL terminator + } + while (true) { char *new_name = realloc(name, size); if (!new_name) { -- cgit v1.2.3