summaryrefslogtreecommitdiff
path: root/src/snprintf.c
diff options
context:
space:
mode:
authorLauri Aalto <laalto@iki.fi>2012-05-23 12:39:27 +0300
committerLauri Aalto <laalto@iki.fi>2012-05-23 12:39:27 +0300
commit391e09a5acddaf3ed63005bec880804896ca860e (patch)
tree84faf02d234f446a694750592fe44e67a1dcc634 /src/snprintf.c
parent0a5d28856469dd9003887631a139919de232ce58 (diff)
downloadraptor-391e09a5acddaf3ed63005bec880804896ca860e.tar.gz
(raptor_vsnprintf2): Fix uninitialized variable problems in non-c99 variant. Thanks to John Emmas for reporting.
* Compute len internally using int, not size_t as standard vsnprintf() returns ints anyway. This fixes for negative value comparison. Cast from size_t to int required for strlen() return value. * Initialize len earlier * Use passed in size if a buffer is given
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 0147402b..d67cd8e3 100644
--- a/src/snprintf.c
+++ b/src/snprintf.c
@@ -81,15 +81,15 @@ vsnprintf_is_c99(void)
#define VSNPRINTF_C99_BLOCK(len, buffer, size, format, arguments) \
do { \
- len = RAPTOR_GOOD_CAST(size_t, vsnprintf(buffer, size, format, arguments)); \
+ len = vsnprintf(buffer, size, format, arguments); \
} while(0)
#define VSNPRINTF_NOT_C99_BLOCK(len, buffer, size, format, arguments) \
do { \
+ len = -1; \
if(!buffer || !size) { \
/* This vsnprintf doesn't return number of bytes required */ \
size = 2 + strlen(format); \
- len = -1; \
while(1) { \
va_list args_copy; \
char* tmp_buffer = RAPTOR_MALLOC(char*, size + 1); \
@@ -110,7 +110,7 @@ vsnprintf_is_c99(void)
* space is allocated and the while() loop retries. \
*/ \
if((len >= 0) && (tmp_buffer[len] == '\0')) { \
- len = strlen(tmp_buffer); \
+ len = RAPTOR_BAD_CAST(int, strlen(tmp_buffer)); \
break; \
} \
RAPTOR_FREE(char*, tmp_buffer); \
@@ -119,7 +119,7 @@ vsnprintf_is_c99(void)
} \
\
if(buffer) \
- vsnprintf(buffer, len, format, arguments); \
+ vsnprintf(buffer, size, format, arguments); \
} while(0)
/**
@@ -143,7 +143,7 @@ int
raptor_vsnprintf2(char *buffer, size_t size,
const char *format, va_list arguments)
{
- size_t len;
+ int len;
RAPTOR_ASSERT_OBJECT_POINTER_RETURN_VALUE(format, char*, 0);
@@ -164,7 +164,7 @@ raptor_vsnprintf2(char *buffer, size_t size,
#endif
- return RAPTOR_BAD_CAST(int, len);
+ return len;
}