diff --git a/kstring.c b/kstring.c index 55626745d..9b2d60c1f 100644 --- a/kstring.c +++ b/kstring.c @@ -1,7 +1,7 @@ /* The MIT License Copyright (C) 2011 by Attractive Chaos - Copyright (C) 2013-2018, 2020 Genome Research Ltd. + Copyright (C) 2013-2018, 2020-2021 Genome Research Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -153,6 +153,15 @@ int kvsprintf(kstring_t *s, const char *fmt, va_list ap) return l; } + if (!s->s) { + const size_t sz = 64; + s->s = malloc(sz); + if (!s->s) + return -1; + s->m = sz; + s->l = 0; + } + l = vsnprintf(s->s + s->l, s->m - s->l, fmt, args); // This line does not work with glibc 2.0. See `man snprintf'. va_end(args); if (l + 1 > s->m - s->l) {