diff options
author | Jacob Bandes-Storch <jacob@bandes-stor.ch> | 2017-08-10 00:15:31 +0000 |
---|---|---|
committer | Jacob Bandes-Storch <jacob@bandes-stor.ch> | 2017-08-10 00:15:31 +0000 |
commit | bfca5df30168f318bab4dec75ac070d6f2ee5369 (patch) | |
tree | 098c71198995ed9e3e2e00cb18cb43974fc1fd04 /lib/Format/WhitespaceManager.cpp | |
parent | a34df13f82bd40f6f3c682defe88d5169ece7620 (diff) |
clang-format: Fix bug with ENAS_DontAlign and empty lines
This fixes a bug in `ENAS_DontAlign` (introduced in D32733) where blank lines had an EscapedNewlineColumn of 0, causing a subtraction to overflow when converted back to unsigned and leading to runaway memory allocation.
Differential Revision: https://reviews.llvm.org/D36019
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310539 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/WhitespaceManager.cpp')
-rw-r--r-- | lib/Format/WhitespaceManager.cpp | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index 4b4fd13145..e41a0767f5 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -603,8 +603,9 @@ void WhitespaceManager::generateChanges() { if (C.CreateReplacement) { std::string ReplacementText = C.PreviousLinePostfix; if (C.ContinuesPPDirective) - appendNewlineText(ReplacementText, C.NewlinesBefore, - C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn); + appendEscapedNewlineText(ReplacementText, C.NewlinesBefore, + C.PreviousEndOfTokenColumn, + C.EscapedNewlineColumn); else appendNewlineText(ReplacementText, C.NewlinesBefore); appendIndentText(ReplacementText, C.Tok->IndentLevel, @@ -640,16 +641,17 @@ void WhitespaceManager::appendNewlineText(std::string &Text, Text.append(UseCRLF ? "\r\n" : "\n"); } -void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines, - unsigned PreviousEndOfTokenColumn, - unsigned EscapedNewlineColumn) { +void WhitespaceManager::appendEscapedNewlineText(std::string &Text, + unsigned Newlines, + unsigned PreviousEndOfTokenColumn, + unsigned EscapedNewlineColumn) { if (Newlines > 0) { - unsigned Offset = - std::min<int>(EscapedNewlineColumn - 2, PreviousEndOfTokenColumn); + unsigned Spaces = + std::max<int>(1, EscapedNewlineColumn - PreviousEndOfTokenColumn - 1); for (unsigned i = 0; i < Newlines; ++i) { - Text.append(EscapedNewlineColumn - Offset - 1, ' '); + Text.append(Spaces, ' '); Text.append(UseCRLF ? "\\\r\n" : "\\\n"); - Offset = 0; + Spaces = std::max<int>(0, EscapedNewlineColumn - 1); } } } |