Navigation Menu

Skip to content

Commit

Permalink
windows grn_text_vprintf: support auto buffer expansion on Windows
Browse files Browse the repository at this point in the history
vsnprintf() on Windows doesn't return "will be written size" when buffer
size is shorter. We need to guess enough size when buffer is shorter on
Windows.
  • Loading branch information
kou committed Apr 17, 2015
1 parent 2fd113b commit f1da757
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions lib/str.c
Expand Up @@ -2512,19 +2512,55 @@ grn_text_printf(grn_ctx *ctx, grn_obj *bulk, const char *format, ...)
grn_rc
grn_text_vprintf(grn_ctx *ctx, grn_obj *bulk, const char *format, va_list args)
{
int rest_size, written_size;
grn_bool is_written = GRN_FALSE;
int written_size;

{
int rest_size;
va_list copied_args;

rest_size = GRN_BULK_REST(bulk);
va_copy(copied_args, args);
written_size = vsnprintf(GRN_BULK_CURR(bulk), rest_size,
format, copied_args);
va_end(copied_args);

if (written_size < rest_size) {
is_written = GRN_TRUE;
}
}

if (written_size >= rest_size) {
#ifdef WIN32
if (written_size == -1 && errno == ERANGE) {
# define N_NEW_SIZES 3
int i;
int new_sizes[N_NEW_SIZES];

new_sizes[0] = GRN_BULK_REST(bulk) + strlen(format) * 2;
new_sizes[1] = new_sizes[0] + 4096;
new_sizes[2] = new_sizes[0] + 65536;

for (i = 0; i < N_NEW_SIZES; i++) {
grn_rc rc;
int new_size = new_sizes[i];
va_list copied_args;

rc = grn_bulk_reserve(ctx, bulk, GRN_BULK_VSIZE(bulk) + new_size);
if (rc) {
return rc;
}
va_copy(copied_args, args);
written_size = vsnprintf(GRN_BULK_CURR(bulk), new_size,
format, copied_args);
va_end(copied_args);
if (written_size != -1) {
break;
}
}
# undef N_NEW_SIZES
}
#else /* WIN32 */
if (!is_written) {
grn_rc rc;
int required_size = written_size + 1; /* "+ 1" for terminate '\0'. */

Expand All @@ -2535,6 +2571,7 @@ grn_text_vprintf(grn_ctx *ctx, grn_obj *bulk, const char *format, va_list args)
written_size = vsnprintf(GRN_BULK_CURR(bulk), required_size,
format, args);
}
#endif /* WIN32 */

if (written_size < 0) {
return GRN_INVALID_ARGUMENT;
Expand Down

0 comments on commit f1da757

Please sign in to comment.