Skip to content

Commit

Permalink
Fix undefined behaviour warning in kstring
Browse files Browse the repository at this point in the history
Fixes a "runtime error: applying zero offset to null pointer"
warning from clang 10.0.1 undefined behaviour sanitizer.

This happened when the storage for the string had not been
allocated before calling kvsprintf().  Fix by making it allocate
a buffer if this is the case, so vsnprintf() has something to
write into.
  • Loading branch information
daviesrob authored and jkbonfield committed Feb 9, 2021
1 parent dbac2d1 commit 222387d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion kstring.c
@@ -1,7 +1,7 @@
/* The MIT License
Copyright (C) 2011 by Attractive Chaos <attractor@live.co.uk>
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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 222387d

Please sign in to comment.