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
This commit is contained in:
jean-pierre charras 2024-01-28 16:07:52 +01:00
parent 071d8af5d7
commit a5ffcd0a55
1 changed files with 11 additions and 6 deletions

View File

@ -1,7 +1,7 @@
/* /*
* This program source code file is part of KiCad, a free EDA CAD application. * 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 * 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 * 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 inMultiLineList = false;
bool inXY = false; bool inXY = false;
int column = 0; int column = 0;
int backslashCount = 0; // Count of successive backslash read since any other char
auto isWhitespace = []( const char aChar ) auto isWhitespace = []( const char aChar )
{ {
@ -207,12 +208,16 @@ void Prettify( std::string& aSource, char aQuoteChar )
} }
else else
{ {
// The output formatter escapes double-quotes // The output formatter escapes double-quotes (like \")
if( *cursor == aQuoteChar // But a corner case is a sequence like \\"
&& ( cursor == aSource.begin() || *( cursor - 1 ) != '\\' ) ) // 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; inQuote = !inQuote;
}
if( *cursor != '\\' )
backslashCount = 0;
formatted.push_back( *cursor ); formatted.push_back( *cursor );
column++; column++;