summaryrefslogtreecommitdiff
path: root/src/snprintf.c
diff options
context:
space:
mode:
authorDave Beckett <dave@dajobe.org>2011-08-30 18:34:05 -0700
committerDave Beckett <dave@dajobe.org>2011-08-30 18:34:15 -0700
commit2a9ebae70e1ff179fe68ba0ab4242382093f9771 (patch)
tree84bd06ce284af51b24c118a95eff661a8b63254f /src/snprintf.c
parentdc93287a3a6479a66b3e0c590918d0638d912f78 (diff)
downloadraptor-2a9ebae70e1ff179fe68ba0ab4242382093f9771.tar.gz
snprintf return code and size guessing fixes
vsnprintf_is_c99 macro: start guessing length from len strlen(format) and grow size by 50% each loop to hopefully get big enough faster. (raptor_vsnprintf): Error out when raptor_vasprintf result < 0 (raptor_snprintf): Note error is < 0 response (raptor_vasprintf): Error out when raptor_vsnprintf2 result is < 0
Diffstat (limited to 'src/snprintf.c')
-rw-r--r--src/snprintf.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/src/snprintf.c b/src/snprintf.c
index 7f90b815..eaa647f6 100644
--- a/src/snprintf.c
+++ b/src/snprintf.c
@@ -210,7 +210,7 @@ vsnprintf_is_c99(void)
do { \
if(!buffer || !size) { \
/* This vsnprintf doesn't return number of bytes required */ \
- size = 2; \
+ size = 2 + strlen(format); \
len = -1; \
while(1) { \
va_list args_copy; \
@@ -236,7 +236,7 @@ vsnprintf_is_c99(void)
break; \
} \
RAPTOR_FREE(char*, tmp_buffer); \
- size += 4; \
+ size += (size >> 1); \
} \
} \
\
@@ -259,7 +259,7 @@ vsnprintf_is_c99(void)
* If @buffer is NULL or size is 0 or the buffer size is too small,
* returns the number of bytes that would be needed for buffer
*
- * Return value: number of bytes allocated (excluding NUL) or 0 on failure
+ * Return value: number of bytes allocated (excluding NUL) or <0 on failure
**/
int
raptor_vsnprintf2(char *buffer, size_t size,
@@ -312,7 +312,7 @@ raptor_vsnprintf(const char *format, va_list arguments)
RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(format, char*, NULL);
len = raptor_vasprintf(&buffer, format, arguments);
- if(!len)
+ if(len < 0)
return NULL;
return buffer;
@@ -363,7 +363,7 @@ raptor_snprintf(char *buffer, size_t size, const char *format, ...)
* This is a wrapper around the (GNU) vasprintf function that is not
* always avaiable.
*
- * Return value: number of bytes allocated (excluding NUL) or <= 0 on failure
+ * Return value: number of bytes allocated (excluding NUL) or < 0 on failure
**/
int
raptor_vasprintf(char **ret, const char *format, va_list arguments)
@@ -377,7 +377,7 @@ raptor_vasprintf(char **ret, const char *format, va_list arguments)
length = vasprintf(ret, format, arguments);
#else
length = raptor_vsnprintf2(NULL, 0, format, arguments);
- if(length <= 0) {
+ if(length < 0) {
*ret = NULL;
return length;
}