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

Segmentation fault when parsing invalid json file #633

Closed
bvnp44 opened this issue Jun 22, 2017 · 5 comments
Closed

Segmentation fault when parsing invalid json file #633

bvnp44 opened this issue Jun 22, 2017 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@bvnp44
Copy link

bvnp44 commented Jun 22, 2017

std::ifstream file("test.json");
nlohmann::json root;
file >> root;

forgetting, for example comma, in json file gives:

Segmentation fault (core dumped)

gcc 6.3.0 ubuntu 14.04

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 6.3.0-18ubuntu2~14.04' --with-bugurl=file:///usr/share/doc/gcc-6/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-6 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=gcc4-compatible --disable-libstdcxx-dual-abi --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-6-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-6-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-6-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 6.3.0 20170519 (Ubuntu/Linaro 6.3.0-18ubuntu2~14.04) 

I'm expecting it will throw std::exception

@nlohmann
Copy link
Owner

This is expected behavior in case of parse errors. The description of the exception includes a description of the error message and (in case of the develop branch) also a location of the error.

In #623, several issues are linked that deal with a no-throw version of the parser, but this is not finished yet.

@bvnp44
Copy link
Author

bvnp44 commented Jun 22, 2017

Is there a fast way to fix this on my side? Cause i need ability to continue app working if json file is invalid. There is no any exception thrown

catch (...)

gives nothing for me

@nlohmann
Copy link
Owner

You can catch the exception. Something like:

std::ifstream file("test.json");
nlohmann::json root;

try {
    file >> root
} catch (...) {
    std::cerr << "error parsing file" << std::endl;
}

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jun 22, 2017
@bvnp44
Copy link
Author

bvnp44 commented Jun 22, 2017

seems there was some issue with boost::format with e.what() in my catch block, closing issue.

std::string msg = (boost::format("Can't parse config file %s: %s") % fileName % e.what()).str();

this had some crash, e - exception from your json lib

@bvnp44 bvnp44 closed this as completed Jun 22, 2017
@nlohmann
Copy link
Owner

Thanks for checking back. It's strange that there may be an issue with boost::format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants