From 219d0c399d7dac030d28e1b11f4e2874a3d5e2e0 Mon Sep 17 00:00:00 2001 From: Alex Shvartzkop Date: Sun, 3 Mar 2024 21:20:14 +0300 Subject: [PATCH] EasyEDA Pro import: make sure to use UTF-8 for project.json and device.json. Also catch exceptions. Fixes https://gitlab.com/kicad/code/kicad/-/issues/17248 --- .../io/easyedapro/easyedapro_import_utils.cpp | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/common/io/easyedapro/easyedapro_import_utils.cpp b/common/io/easyedapro/easyedapro_import_utils.cpp index 8bb13e6a87..0d530cb6c6 100644 --- a/common/io/easyedapro/easyedapro_import_utils.cpp +++ b/common/io/easyedapro/easyedapro_import_utils.cpp @@ -150,14 +150,28 @@ nlohmann::json EASYEDAPRO::ReadProjectOrDeviceFile( const wxString& aZipFileName { wxString name = entry->GetName(); - if( name == wxS( "project.json" ) || name == wxS( "device.json" ) ) + try { - wxMemoryOutputStream memos; - memos << zip; - wxStreamBuffer* buf = memos.GetOutputStreamBuffer(); - wxString str( (char*) buf->GetBufferStart(), buf->GetBufferSize() ); + if( name == wxS( "project.json" ) || name == wxS( "device.json" ) ) + { + wxMemoryOutputStream memos; + memos << zip; + wxStreamBuffer* buf = memos.GetOutputStreamBuffer(); - return nlohmann::json::parse( str ); + wxString str = + wxString::FromUTF8( (char*) buf->GetBufferStart(), buf->GetBufferSize() ); + + return nlohmann::json::parse( str ); + } + } + catch( nlohmann::json::exception& e ) + { + THROW_IO_ERROR( + wxString::Format( _( "JSON error reading '%s': %s" ), name, e.what() ) ); + } + catch( std::exception& e ) + { + THROW_IO_ERROR( wxString::Format( _( "Error reading '%s': %s" ), name, e.what() ) ); } }