+2019-07-05 Bruno Haible <bruno@clisp.org>
+
+ getcwd-lgpl, getcwd: Don't call realloc when it is pointless.
+ * lib/getcwd-lgpl.c (rpl_getcwd): Don't call realloc if the result's
+ needed size is equal to the allocated size.
+ * lib/getcwd.c (__getcwd): Likewise.
+
2019-07-05 Bruno Haible <bruno@clisp.org>
xgetdomainname: Don't return an excessive memory allocation.
}
else
{
- /* Trim to fit, if possible. */
- result = realloc (buf, strlen (buf) + 1);
- if (!result)
- result = buf;
+ /* Here result == buf. */
+ /* Shrink result before returning it. */
+ size_t actual_size = strlen (result) + 1;
+ if (actual_size < size)
+ {
+ char *shrinked_result = realloc (result, actual_size);
+ if (shrinked_result != NULL)
+ result = shrinked_result;
+ }
}
return result;
}
if (size == 0)
/* Ensure that the buffer is only as large as necessary. */
- buf = realloc (dir, used);
+ buf = (used < allocated ? realloc (dir, used) : dir);
if (buf == NULL)
/* Either buf was NULL all along, or 'realloc' failed but