From 5c59b3a7754279a0946a2f650122348b00d066d8 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Fri, 7 Feb 2020 10:29:15 -0500 Subject: [PATCH] Fixing memory leaks. (Minor issue.) --- benchmark/parseandstatcompetition.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/benchmark/parseandstatcompetition.cpp b/benchmark/parseandstatcompetition.cpp index a21add02..29270743 100644 --- a/benchmark/parseandstatcompetition.cpp +++ b/benchmark/parseandstatcompetition.cpp @@ -153,11 +153,15 @@ __attribute__((noinline)) stat_t sasjon_compute_stats(const simdjson::padded_string &p) { stat_t answer; char *buffer = (char *)malloc(p.size()); + if(buffer == nullptr) { + return answer; + } memcpy(buffer, p.data(), p.size()); auto d = sajson::parse(sajson::dynamic_allocation(), sajson::mutable_string_view(p.size(), buffer)); answer.valid = d.is_valid(); if (!answer.valid) { + free(buffer); return answer; } answer.number_count = 0; @@ -211,12 +215,16 @@ __attribute__((noinline)) stat_t rapid_compute_stats(const simdjson::padded_string &p) { stat_t answer; char *buffer = (char *)malloc(p.size() + 1); + if(buffer == nullptr) { + return answer; + } memcpy(buffer, p.data(), p.size()); buffer[p.size()] = '\0'; rapidjson::Document d; d.ParseInsitu(buffer); answer.valid = !d.HasParseError(); if (!answer.valid) { + free(buffer); return answer; } answer.number_count = 0;