From 30d3ba5142311df568e8f62a374d83d0bdab42bf Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 23 May 2019 00:42:03 +0000 Subject: Bulletproof -fdiagnostics-format=json against bad locations (PR c++/90462) PR c++/90462 reports an ICE with -fdiagnostics-format=json when attempting to serialize a malformed location to JSON. The compound location_t in question has meaningful "caret" and "start" locations, but has UNKNOWN_LOCATION for its "finish" location, leading to a NULL pointer dereference when attempting to build a JSON string for the filename. This patch bulletproofs the JSON output so that attempts to write a JSON object for a location with a NULL file will lead to an object with no "file" key, and attempts to write a compound location with UNKNOWN_LOCATION for its start or finish will lead to the corresponding JSON child object being omitted. This patch also adds a json::object::get member function, for self-testing the above. gcc/ChangeLog: PR c++/90462 * diagnostic-format-json.cc: Include "selftest.h". (json_from_expanded_location): Only add "file" key for non-NULL file strings. (json_from_location_range): Don't add "start" and "finish" children if they are UNKNOWN_LOCATION. (selftest::test_unknown_location): New selftest. (selftest::test_bad_endpoints): New selftest. (selftest::diagnostic_format_json_cc_tests): New function. * json.cc (json::object::get): New function. (selftest::test_object_get): New selftest. (selftest::json_cc_tests): Call it. * json.h (json::object::get): New decl. * selftest-run-tests.c (selftest::run_tests): Call selftest::diagnostic_format_json_cc_tests. * selftest.h (selftest::diagnostic_format_json_cc_tests): New decl. gcc/testsuite/ChangeLog: PR c++/90462 * g++.dg/pr90462.C: New test. From-SVN: r271535 --- gcc/selftest.h | 1 + 1 file changed, 1 insertion(+) (limited to 'gcc/selftest.h') diff --git a/gcc/selftest.h b/gcc/selftest.h index 3e00e75..d278f0a 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -218,6 +218,7 @@ extern void bitmap_c_tests (); extern void cgraph_c_tests (); extern void convert_c_tests (); extern void diagnostic_c_tests (); +extern void diagnostic_format_json_cc_tests (); extern void diagnostic_show_locus_c_tests (); extern void dumpfile_c_tests (); extern void edit_context_c_tests (); -- cgit v1.1