diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2008-01-29 08:50:25 +0000 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2008-02-15 13:50:41 +0000 |
commit | 5efc88e9108df2331772cc22dc52ef0a9cc93869 (patch) | |
tree | f007cc0ce8ecdacea5192cf23afea81c4c285156 /boilerplate | |
parent | 1faf208093a8cce77d2f7d6b248bc1eb1bd19a8a (diff) |
[xmalloc] Hide valgrind warning.
Allocate the string to the next integer boundary to hide a valgrind
warning.
Diffstat (limited to 'boilerplate')
-rw-r--r-- | boilerplate/xmalloc.c | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/boilerplate/xmalloc.c b/boilerplate/xmalloc.c index c2cbe2321..dfd494636 100644 --- a/boilerplate/xmalloc.c +++ b/boilerplate/xmalloc.c @@ -89,10 +89,10 @@ xasprintf (char **strp, const char *fmt, ...) #define BUF_SIZE 1024 va_list va; char buffer[BUF_SIZE]; - int ret; + int ret, len; va_start (va, fmt); - ret = vsnprintf (buffer, sizeof(buffer), fmt, va); + ret = vsnprintf (buffer, sizeof (buffer), fmt, va); va_end (va); if (ret < 0) { @@ -100,15 +100,26 @@ xasprintf (char **strp, const char *fmt, ...) exit (1); } - if (strlen (buffer) == sizeof(buffer) - 1) { - CAIRO_BOILERPLATE_LOG ("Overflowed fixed buffer\n"); + len = (ret + sizeof (int)) & -sizeof (int); + *strp = malloc (len); + if (*strp == NULL) { + CAIRO_BOILERPLATE_LOG ("Out of memory\n"); exit (1); } - *strp = strdup (buffer); - if (!*strp) { - CAIRO_BOILERPLATE_LOG ("Out of memory\n"); - exit (1); + if ((unsigned) ret < sizeof (buffer)) { + memcpy (*strp, buffer, ret); + } else { + va_start (va, fmt); + ret = vsnprintf (*strp, len, fmt, va); + va_end (va); + + if (ret >= len) { + free (*strp); + CAIRO_BOILERPLATE_LOG ("Overflowed dynamic buffer\n"); + exit (1); + } } + memset (*strp + ret, 0, len-ret); #endif /* !HAVE_VASNPRINTF */ } |