From a5ffcd0a55800105d7b9c87de6bfbb3aeaae19fa Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sun, 28 Jan 2024 16:07:52 +0100 Subject: [PATCH] void Prettify(): fix a bug in a corner case, when a string ends with '\' A '\' in a string is encoded as \\. A '"' in a string is encoded as \". But prettify parser did not take in account a string encoded as "xx\\" (for xx\) So we need to verify if a sequence is xx\" or xx\\", i.e. if there are a odd count of backslashes before a '"' Fixes #16777 https://gitlab.com/kicad/code/kicad/-/issues/16777 --- common/io/kicad/kicad_io_utils.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/common/io/kicad/kicad_io_utils.cpp b/common/io/kicad/kicad_io_utils.cpp index a6648ed374..6bca79dc48 100644 --- a/common/io/kicad/kicad_io_utils.cpp +++ b/common/io/kicad/kicad_io_utils.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2023 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2024 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -90,6 +90,7 @@ void Prettify( std::string& aSource, char aQuoteChar ) bool inMultiLineList = false; bool inXY = false; int column = 0; + int backslashCount = 0; // Count of successive backslash read since any other char auto isWhitespace = []( const char aChar ) { @@ -207,12 +208,16 @@ void Prettify( std::string& aSource, char aQuoteChar ) } else { - // The output formatter escapes double-quotes - if( *cursor == aQuoteChar - && ( cursor == aSource.begin() || *( cursor - 1 ) != '\\' ) ) - { + // The output formatter escapes double-quotes (like \") + // But a corner case is a sequence like \\" + // therefore a '\' is attached to a '"' if a odd number of '\' is detected + if( *cursor == '\\' ) + backslashCount++; + else if( *cursor == aQuoteChar && ( backslashCount & 1 ) == 0 ) inQuote = !inQuote; - } + + if( *cursor != '\\' ) + backslashCount = 0; formatted.push_back( *cursor ); column++;