summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5tools_str.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/lib/h5tools_str.c')
-rw-r--r--tools/lib/h5tools_str.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/tools/lib/h5tools_str.c b/tools/lib/h5tools_str.c
index ae0646a..5166a80 100644
--- a/tools/lib/h5tools_str.c
+++ b/tools/lib/h5tools_str.c
@@ -132,6 +132,7 @@ char *
h5tools_str_append(h5tools_str_t *str/*in,out*/, const char *fmt, ...)
{
va_list ap;
+ hbool_t isReallocated = FALSE;
/* Make sure we have some memory into which to print */
if (!str->s || str->nalloc <= 0) {
@@ -157,12 +158,23 @@ h5tools_str_append(h5tools_str_t *str/*in,out*/, const char *fmt, ...)
nchars = HDvsnprintf(str->s + str->len, avail, fmt, ap);
va_end(ap);
- if (nchars < 0) {
+ /* Note: HDvsnprintf() behaves differently on Windows as Unix, when
+ * buffer is smaller than source string. On Unix, this function
+ * returns length of the source string and copy string upto the
+ * buffer size with NULL at the end of the buffer. However on
+ * Windows with the same condition, this function returns -1 and
+ * doesn't add NULL at the end of the buffer.
+ * Because of this different return results, isReallocated variable
+ * is used to handle when HDvsnprintf() returns -1 on Windows due
+ * to lack of buffer size, so try one more time after realloc more
+ * buffer size before return NULL.
+ */
+ if (nchars < 0 && isReallocated == TRUE) {
/* failure, such as bad format */
return NULL;
}
- if ((size_t) nchars >= avail || (0 == nchars && (strcmp(fmt, "%s")))) {
+ if (nchars < 0 || (size_t) nchars >= avail || (0 == nchars && (strcmp(fmt, "%s")))) {
/* Truncation return value as documented by C99, or zero return value with either of the
* following conditions, each of which indicates that the proper C99 return value probably
* should have been positive when the format string is
@@ -174,6 +186,7 @@ h5tools_str_append(h5tools_str_t *str/*in,out*/, const char *fmt, ...)
str->s = realloc(str->s, newsize);
assert(str->s);
str->nalloc = newsize;
+ isReallocated = TRUE;
}
else {
/* Success */