Skip to content

nlohmann::json::parse exhaust memory in go binding #999

Description

@sjiekak

Bug Report

  • nlohmann::json::parse exhaust memory inside cgo

  • Code :

parse.hpp

#ifndef PARSE_DEMO_H_
#define PARSE_DEMO_H_

#ifdef __cplusplus
extern "C" {
#endif
    void parse(char* value);
#ifdef __cplusplus
}
#endif

#endif

parse.cpp

#include "parse.hpp"
#include "json.hpp"

extern "C" void parse(char* value)
{
    try
    {
        nlohmann::json::parse(value);
    }
    catch (std::exception&)
    {}
}

leaker.go

package main

import (
	"unsafe"
)

// #include <stdlib.h>
// #include "parse.hpp"
// #cgo LDFLAGS: -lparse -lstdc++
import "C"

func main() {
	input := "{\"hello\": \"world\"}";
	for ;; {
		cinput := C.CString(input);
		defer C.free(unsafe.Pointer(cinput))
		C.parse(cinput)
	}
}

run.sh

#!/bin/bash

set -eo pipefail

g++ -g -c -std=gnu++17 ./parse.cpp -o parse.o
ar -s -r libparse.a parse.o
rm parse.o
CGO_LDFLAGS=-L"$PWD" go run leaker.go

Dockerfile

FROM ubuntu:17.10
RUN apt-get update --yes && \
    apt-get install \
    curl \
    g++ \
    --yes

ENV GO_VERSION=1.9 \
    GO_SHA256=d70eadefce8e160638a9a6db97f7192d8463069ab33138893ad3bf31b0650a79

RUN curl https://storage.googleapis.com/golang/go${GO_VERSION}.linux-amd64.tar.gz -o /golang.tar.gz && \
    echo "${GO_SHA256} /golang.tar.gz" | sha256sum -c - && \
    tar -xzf /golang.tar.gz && \
    cd /usr/local/bin && ln -s /go/bin/* . && \
    go version && \
    rm /golang.tar.gz

RUN mkdir -p /usr/local/demo/
COPY /* /usr/local/demo/
WORKDIR /usr/local/demo
  • What is the expected behavior?

Constant memory usage

  • And what is the actual behavior instead?

Increasing memory usage

g++ (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0

  • Did you use a released version of the library or the version from the develop branch?

I used the latest release version to date

Activity

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

    solution: invalidthe issue is not related to the library

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions