From e14828fce2e916315256dbb7a52c2298b948be9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20H=C3=B6gberg?= Date: Fri, 12 Apr 2019 10:06:20 +0200 Subject: [PATCH] fixup! Never expand the encode buffer; emit it and start anew --- c_src/encoder.c | 39 ++++++++++++++++++++++------------ test/jiffy_03_number_tests.erl | 1 + 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/c_src/encoder.c b/c_src/encoder.c index a6664db..3a7938c 100644 --- a/c_src/encoder.c +++ b/c_src/encoder.c @@ -158,12 +158,14 @@ enc_ensure(Encoder* e, size_t req) { size_t new_size = BIN_INC_SIZE; - if(req < (e->buffer.size - e->i)) { - return 1; - } + if(e->have_buffer) { + if(req < (e->buffer.size - e->i)) { + return 1; + } - if(!enc_flush(e)) { - return 0; + if(!enc_flush(e)) { + return 0; + } } for(new_size = BIN_INC_SIZE; new_size < req; new_size <<= 1); @@ -193,6 +195,23 @@ enc_literal(Encoder* e, const char* literal, size_t len) return 1; } +static inline int +enc_bignum(Encoder* e, ERL_NIF_TERM value) { + /* This is a bignum and we need to handle it up in Erlang code as + * the NIF API doesn't support them yet. + * + * Flush our current output and mark ourselves as needing a fixup + * after we return. */ + if(!enc_flush(e)) { + return 0; + } + + e->iolist = enif_make_list_cell(e->env, value, e->iolist); + e->partial_output = 1; + + return 1; +} + static inline int enc_special_character(Encoder* e, int val) { switch(val) { @@ -893,18 +912,10 @@ encode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) termstack_push(&stack, e->atoms->ref_array); termstack_push(&stack, item); } else if(enif_is_number(env, curr)) { - /* This is a bignum and we need to handle it up in Erlang code as - * the NIF API doesn't support them yet. - * - * Flush our current output and mark ourselves as needing a fixup - * after we return. */ - if(!enc_flush(e)) { + if(!enc_bignum(e, curr)) { ret = enc_error(e, "internal_error"); goto done; } - - e->iolist = enif_make_list_cell(e->env, curr, e->iolist); - e->partial_output = 1; } else { ret = enc_obj_error(e, "invalid_ejson", curr); goto done; diff --git a/test/jiffy_03_number_tests.erl b/test/jiffy_03_number_tests.erl index ac7b95f..4f8a336 100644 --- a/test/jiffy_03_number_tests.erl +++ b/test/jiffy_03_number_tests.erl @@ -48,6 +48,7 @@ cases(ok) -> {<<"1">>, 1}, {<<"12">>, 12}, {<<"-3">>, -3}, + {<<"{\"key\":9223372036854775808}">>,{[{<<"key">>,1 bsl 63}]}}, {<<"1234567890123456789012345">>, 1234567890123456789012345}, {<<"1310050760199">>, 1310050760199}, {