You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
http://msdn.microsoft.com/en-us/library/1kt27hek%28v=vs.80%29.aspx
vsnprintf,_vsnprintf, and _vsnwprintf return the number of characters written if the number of characters to write is less than or equal to count; if the number of characters to write is greater than count, these functions return -1 indicating that output has been truncated. The return value does not include the terminating null, if one is written.
Test code:
include <stdio.h>
include <stdarg.h>
int mg_printf(const char *fmt, ...) {
char mem[10];
int len;
va_list ap;
// Print in a local buffer first, hoping that it is large enough to
// hold the whole message
va_start(ap, fmt);
len = vsnprintf(mem, sizeof(mem), fmt, ap);
va_end(ap);
printf("len = %d", len);
return len;
}
int main()
{
mg_printf(0,"%d - long stringlong stringlong stringlong string ",123);
return 0;
}
That is fixed in b589e0c
I've added alloc_vprintf() function. It looks like if the buffer is 0, windows snprintf() behaves correctly.
I assume this is the case for all versions of msvcrt.dll
http://msdn.microsoft.com/en-us/library/1kt27hek%28v=vs.80%29.aspx
vsnprintf,_vsnprintf, and _vsnwprintf return the number of characters written if the number of characters to write is less than or equal to count; if the number of characters to write is greater than count, these functions return -1 indicating that output has been truncated. The return value does not include the terminating null, if one is written.
Test code:
include <stdio.h>
include <stdarg.h>
int mg_printf(const char *fmt, ...) {
char mem[10];
int len;
va_list ap;
// Print in a local buffer first, hoping that it is large enough to
// hold the whole message
va_start(ap, fmt);
len = vsnprintf(mem, sizeof(mem), fmt, ap);
va_end(ap);
printf("len = %d", len);
return len;
}
int main()
{
mg_printf(0,"%d - long stringlong stringlong stringlong string ",123);
return 0;
}
stdout:
len = -1
Temp fix:
int mg_printf_dynamic(struct mg_connection *conn, const int len_helper, const char *fmt, ...)
{
va_list ap;
int len = -1;
int l_fly_size = len_helper;
char *l_fly_buf = (char *)malloc(l_fly_size);
if(l_fly_buf)
{
l_fly_buf[0] = 0;
va_start(ap, fmt);
while(_vsnprintf(l_fly_buf, l_fly_size, fmt, ap) == -1){
free(l_fly_buf);
l_fly_size *= 2;
l_fly_buf = (char *)malloc(l_fly_size);
if(!l_fly_buf)
return -1;
l_fly_buf[0] = 0;
}
va_end(ap);
len = mg_write(conn, l_fly_buf, (size_t) l_fly_size);
free(l_fly_buf);
}
return len;
}
The text was updated successfully, but these errors were encountered: