From e8d3d784ab061c9fc4b7ee19baaabaffcde01ecf Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 10 Dec 2018 22:21:03 -0500 Subject: [PATCH] More fixing. --- benchmark/minifiercompetition.cpp | 2 +- benchmark/parse.cpp | 1 + benchmark/parsingcompetition.cpp | 1 + include/simdjson/jsonioutil.h | 3 ++- src/jsonioutil.cpp | 7 +++++-- tests/allparserscheckfile.cpp | 2 +- tests/jsoncheck.cpp | 1 + tests/numberparsingcheck.cpp | 1 + tests/stringparsingcheck.cpp | 1 + 9 files changed, 14 insertions(+), 5 deletions(-) diff --git a/benchmark/minifiercompetition.cpp b/benchmark/minifiercompetition.cpp index 52d36364f..ac9985f0d 100644 --- a/benchmark/minifiercompetition.cpp +++ b/benchmark/minifiercompetition.cpp @@ -143,7 +143,7 @@ int main(int argc, char *argv[]) { } BEST_TIME("json_parse despaced", json_parse((const u8*)buffer, minisize, pj2), true, memcpy(buffer, minibuffer, p.size()), repeat, volume, true); - + free((void*)p.data()); free(buffer); free(ast_buffer); free(minibuffer); diff --git a/benchmark/parse.cpp b/benchmark/parse.cpp index e9a6ffc56..8ed56de8f 100644 --- a/benchmark/parse.cpp +++ b/benchmark/parse.cpp @@ -229,6 +229,7 @@ int main(int argc, char *argv[]) { if(dump) { isok = isok && pj.dump_raw_tape(std::cout); } + free((void*)p.data()); if (!isok) { printf(" Parsing failed. \n "); return EXIT_FAILURE; diff --git a/benchmark/parsingcompetition.cpp b/benchmark/parsingcompetition.cpp index 6b6db2cda..684e45630 100644 --- a/benchmark/parsingcompetition.cpp +++ b/benchmark/parsingcompetition.cpp @@ -121,6 +121,7 @@ int main(int argc, char *argv[]) { void *state; if(all) BEST_TIME("ultrajson ", (UJDecode(buffer, p.size(), NULL, &state) == NULL), false, memcpy(buffer, p.data(), p.size()), repeat, volume, true); BEST_TIME("memcpy ", (memcpy(buffer, p.data(), p.size()) == buffer), true, , repeat, volume, true); + free((void*)p.data()); free(ast_buffer); free(buffer); } diff --git a/include/simdjson/jsonioutil.h b/include/simdjson/jsonioutil.h index 175ee7e97..da88cc48d 100644 --- a/include/simdjson/jsonioutil.h +++ b/include/simdjson/jsonioutil.h @@ -26,12 +26,13 @@ char * allocate_padded_buffer(size_t length); // throws exceptions in case of failure // first element of the pair is a string (null terminated) // whereas the second element is the length. -// caller is responsible to free (free std::pair.first) +// caller is responsible to free (free((void*)result.data()))) // // throws an exception if the file cannot be opened, use try/catch // try { // p = get_corpus(filename); // } catch (const std::exception& e) { +// free((void*)p.data()); // std::cout << "Could not load the file " << filename << std::endl; // } std::string_view get_corpus(std::string filename); diff --git a/src/jsonioutil.cpp b/src/jsonioutil.cpp index 03d3f2efd..8a7aa738a 100644 --- a/src/jsonioutil.cpp +++ b/src/jsonioutil.cpp @@ -25,9 +25,12 @@ std::string_view get_corpus(std::string filename) { throw std::runtime_error("could not allocate memory"); } std::rewind(fp); - std::fread(buf, 1, len, fp); - //buf[len] = '\0';// no need + size_t readb = std::fread(buf, 1, len, fp); std::fclose(fp); + if(readb != len) { + free(buf); + throw std::runtime_error("could not read the data"); + } return std::string_view(buf,len); } throw std::runtime_error("could not load corpus"); diff --git a/tests/allparserscheckfile.cpp b/tests/allparserscheckfile.cpp index 61a94c2b7..cc915a83e 100644 --- a/tests/allparserscheckfile.cpp +++ b/tests/allparserscheckfile.cpp @@ -108,7 +108,7 @@ int main(int argc, char *argv[]) { printf("fastjson : %s \n", fastjson_correct ? "correct":"invalid"); printf("gason : %s \n", gason_correct ? "correct":"invalid"); printf("ultrajson : %s \n", ultrajson_correct ? "correct":"invalid"); - + free((void*)p.data()); free(buffer); return EXIT_SUCCESS; } diff --git a/tests/jsoncheck.cpp b/tests/jsoncheck.cpp index dad01687b..e89bee226 100644 --- a/tests/jsoncheck.cpp +++ b/tests/jsoncheck.cpp @@ -75,6 +75,7 @@ bool validate(const char *dirname) { } ++howmany; bool isok = json_parse(p, pj); + free((void*)p.data()); printf("%s\n", isok ? "ok" : "invalid"); if(contains("EXCLUDE",name)) { // skipping diff --git a/tests/numberparsingcheck.cpp b/tests/numberparsingcheck.cpp index 49f1e5c87..48acfa210 100644 --- a/tests/numberparsingcheck.cpp +++ b/tests/numberparsingcheck.cpp @@ -154,6 +154,7 @@ bool validate(const char *dirname) { float_count, invalid_count, int_count + float_count + invalid_count); } + free((void*)p.data()); free(fullpath); } } diff --git a/tests/stringparsingcheck.cpp b/tests/stringparsingcheck.cpp index 7011ffe0a..c6d50e673 100644 --- a/tests/stringparsingcheck.cpp +++ b/tests/stringparsingcheck.cpp @@ -343,6 +343,7 @@ bool validate(const char *dirname) { total_string_length = 0; empty_string = 0; bool isok = json_parse(p, pj); + free((void*)p.data()); if (good_string > 0) { printf("File %40s %s --- bad strings: %10zu \tgood strings: %10zu\t " "empty strings: %10zu "