Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sjiekak opened this issue Mar 6, 2018 · 2 comments
Closed

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

sjiekak opened this issue Mar 6, 2018 · 2 comments
Labels
solution: invalid the issue is not related to the library

Comments

@sjiekak
Copy link

sjiekak commented Mar 6, 2018

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

@sjiekak sjiekak closed this as completed Mar 6, 2018
@sjiekak
Copy link
Author

sjiekak commented Mar 6, 2018

Error unrelated to the nlohmann library

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2018

Thanks for reporting back!

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Mar 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants