From 2ee9386361077d5172742059107e4a7abc206ca0 Mon Sep 17 00:00:00 2001 From: mbystryantsev Date: Mon, 16 Apr 2018 11:39:50 +0300 Subject: [PATCH] get rid of redundant length calculation on number generation --- src/yajl_gen.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/yajl_gen.c b/src/yajl_gen.c index 0f5c68e..d837a4a 100644 --- a/src/yajl_gen.c +++ b/src/yajl_gen.c @@ -209,9 +209,13 @@ yajl_gen_status yajl_gen_integer(yajl_gen g, long long int number) { char i[32]; + int len; ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE; - sprintf(i, "%lld", number); - g->print(g->ctx, i, (unsigned int)strlen(i)); + len = sprintf(i, "%lld", number); + if (len < 0) { + return yajl_gen_invalid_number; + } + g->print(g->ctx, i, (unsigned int)len); APPENDED_ATOM; FINAL_NEWLINE; return yajl_gen_status_ok; @@ -227,14 +231,18 @@ yajl_gen_status yajl_gen_double(yajl_gen g, double number) { char i[32]; + int len; ENSURE_VALID_STATE; ENSURE_NOT_KEY; if (isnan(number) || isinf(number)) return yajl_gen_invalid_number; INSERT_SEP; INSERT_WHITESPACE; - sprintf(i, "%.20g", number); - if (strspn(i, "0123456789-") == strlen(i)) { + len = sprintf(i, "%.20g", number); + if (len < 0) { + return yajl_gen_invalid_number; + } + if (strspn(i, "0123456789-") == (unsigned int)len) { strcat(i, ".0"); } - g->print(g->ctx, i, (unsigned int)strlen(i)); + g->print(g->ctx, i, (unsigned int)len); APPENDED_ATOM; FINAL_NEWLINE; return yajl_gen_status_ok;