From 3c29e68992d00ca1320bb7e514b9a9dc5782efe4 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 8 Oct 2021 13:33:07 -0700 Subject: [PATCH] Use wxFFileInputStream to avoid dangling pointer Cases where fp was left open could lead to dangling files until KiCad is closed. Stack-based file stream automatically closes after parsing and on exception Fixes https://gitlab.com/kicad/code/kicad/issues/9336 --- common/settings/json_settings.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp index 6b7c1967a4..20c8f53189 100644 --- a/common/settings/json_settings.cpp +++ b/common/settings/json_settings.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include const wxChar* const traceSettings = wxT( "KICAD_SETTINGS" ); @@ -262,12 +263,13 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory ) try { - FILE* fp = wxFopen( path.GetFullPath(), wxT( "rt" ) ); + wxFFileInputStream fp( path.GetFullPath(), wxT( "rt" ) ); + wxStdInputStream fstream( fp ); - if( fp ) + if( fp.IsOk() ) { *static_cast( m_internals.get() ) = - nlohmann::json::parse( fp, nullptr, + nlohmann::json::parse( fstream, nullptr, /* allow_exceptions = */ true, /* ignore_comments = */ true );