Skip to content

valgrind leak error on parsing invalid json using RAPIDJSON_USE_MEMBERSMAP #2409

Open

Description

@gvollant

Just save the code below on file demo_bug_rapidjson.cpp

// g++  -DRAPIDJSON_USE_MEMBERSMAP=1 demo_bug_rapidjson.cpp -I./exterieur/rapidjson/include/ -g -O0 -o demo_bug_rapidjson
// valgrind --track-origins=yes --show-reachable=yes --leak-check=full --show-leak-kinds=all --verbose --num-callers=40 --log-file=demovgr0.txt --show-error-list=yes --gen-suppressions=all ./demo_bug_rapidjson

// build from https://github.com/Tencent/rapidjson/blob/master/example/tutorial/tutorial.cpp

#include "rapidjson/document.h"     // rapidjson's DOM-style API
#include <cstdio>

using namespace rapidjson;
using namespace std;

int main(int, char*[]) {
    ////////////////////////////////////////////////////////////////////////////
    // Parse a broken JSON text string to a document.

    const char json[] = "[{\"a\":\"\"},{\"b\"";

    printf("Original JSON:\n %s\n", json);

    Document document;

    // In-situ parsing, decode strings directly in the source string. Source must be string.
    //char buffer[sizeof(json)];
    char* buffer=(char*)malloc(strlen(json)+1);
    memcpy(buffer, json, strlen(json)+1);
    if (document.ParseInsitu(buffer).HasParseError()) {
        printf("\nParse Error !!\n");
        free(buffer);
        return 1;
    }

    free(buffer);
    return 0;
}

then run these command

g++ -DRAPIDJSON_USE_MEMBERSMAP=1 demo_bug_rapidjson.cpp -I./exterieur/rapidjson/include/ -g -O0 -o demo_bug_rapidjson
valgrind --track-origins=yes --show-reachable=yes --leak-check=full --show-leak-kinds=all --verbose --num-callers=40 --log-file=demovgr0.txt --show-error-list=yes --gen-suppressions=all ./demo_bug_rapidjson
grep lost demovgr0.txt 
grep SUMMARY demovgr0.txt

The end of valgrind report is

==24673== LEAK SUMMARY:
==24673==    definitely lost: 56 bytes in 1 blocks
==24673==    indirectly lost: 65,561 bytes in 2 blocks
==24673==      possibly lost: 0 bytes in 0 blocks
==24673==    still reachable: 0 bytes in 0 blocks
==24673==         suppressed: 0 bytes in 0 blocks
==24673==
==24673== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Note : an AI report suggest this modification on rapidjson/include/rapidjson/document.h.
Advice from anyone who known well rapidjson source code is welcome @miloyip

     void ClearStack() {
-        if (Allocator::kNeedFree)
+        if (Allocator::kNeedFree || (RAPIDJSON_USE_MEMBERSMAP+0 &&
+                                     internal::IsRefCounted<Allocator>::Value))
             while (stack_.GetSize() > 0)    // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects)
                 (stack_.template Pop<ValueType>(1))->~ValueType();

Activity

  1. durgaprasadudurga25-hub commented on Oct 30, 2026

    @durgaprasadudurga25-hub

    git clone --recursive https://github.com/prophetl33t/ShodanCPP.git
    git submodule update --init --recursive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions