summaryrefslogtreecommitdiff
path: root/source4/heimdal/lib/roken/strpool.c
diff options
context:
space:
mode:
authorStefan Metzmacher <metze@samba.org>2021-12-24 01:52:32 +0100
committerJoseph Sutton <jsutton@samba.org>2022-01-19 20:50:35 +0000
commit40b65c840e03bd5eb7f3b02fe80144650c63c005 (patch)
treed11b9bf5bcf1c71c0696d2153489447d47d03a0e /source4/heimdal/lib/roken/strpool.c
parentd2a3016a9c59f93f89cf4bb86d40938d56400453 (diff)
downloadsamba-40b65c840e03bd5eb7f3b02fe80144650c63c005.tar.gz
s4:heimdal: import lorikeet-heimdal-202201172009 (commit 5a0b45cd723628b3690ea848548b05771c40f14e)
See https://git.samba.org/?p=lorikeet-heimdal.git;a=shortlog;h=refs/heads/lorikeet-heimdal-202201172009 or https://gitlab.com/samba-team/devel/lorikeet-heimdal/-/tree/lorikeet-heimdal-202201172009 NOTE: THIS COMMIT WON'T COMPILE/WORK ON ITS OWN! Pair-Programmed-With: Joseph Sutton <josephsutton@catalyst.net.nz> Signed-off-by: Stefan Metzmacher <metze@samba.org> Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Diffstat (limited to 'source4/heimdal/lib/roken/strpool.c')
-rw-r--r--source4/heimdal/lib/roken/strpool.c42
1 files changed, 23 insertions, 19 deletions
diff --git a/source4/heimdal/lib/roken/strpool.c b/source4/heimdal/lib/roken/strpool.c
index 6e6a737bc63..032bae1dd23 100644
--- a/source4/heimdal/lib/roken/strpool.c
+++ b/source4/heimdal/lib/roken/strpool.c
@@ -39,7 +39,8 @@
struct rk_strpool {
char *str;
- size_t len;
+ size_t len; /* strlen() of str */
+ size_t sz; /* Allocated size */
};
/*
@@ -49,7 +50,7 @@ struct rk_strpool {
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
rk_strpoolfree(struct rk_strpool *p)
{
- if (p->str) {
+ if (p && p->str) {
free(p->str);
p->str = NULL;
}
@@ -64,29 +65,32 @@ ROKEN_LIB_FUNCTION struct rk_strpool * ROKEN_LIB_CALL
rk_strpoolprintf(struct rk_strpool *p, const char *fmt, ...)
{
va_list ap;
- char *str, *str2;
+ char *str;
int len;
- if (p == NULL) {
- p = malloc(sizeof(*p));
- if (p == NULL)
- return NULL;
- p->str = NULL;
- p->len = 0;
- }
va_start(ap, fmt);
len = vasprintf(&str, fmt, ap);
va_end(ap);
- if (str == NULL) {
- rk_strpoolfree(p);
- return NULL;
- }
- str2 = realloc(p->str, len + p->len + 1);
- if (str2 == NULL) {
- rk_strpoolfree(p);
- return NULL;
+ if (str == NULL)
+ return rk_strpoolfree(p), NULL;
+
+ if (p == NULL) {
+ if ((p = malloc(sizeof(*p))) == NULL)
+ return free(str), NULL;
+ p->str = str;
+ p->len = p->sz = len;
+ return p;
+ } /* else grow the buffer and append `str', but don't grow too fast */
+
+ if (len + p->len + 1 > p->sz) {
+ size_t sz = p->len + len + 9 + (p->sz >> 2);
+ char *str2;
+
+ if ((str2 = realloc(p->str, sz)) == NULL)
+ return rk_strpoolfree(p), NULL;
+ p->str = str2;
+ p->sz = sz;
}
- p->str = str2;
memcpy(p->str + p->len, str, len + 1);
p->len += len;
free(str);