From 40e45b0112cfde72f6aa0093dc1df353dabcca83 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 21 Jul 2019 12:03:56 +0200 Subject: [PATCH] Fixed infinite loop in diagnostics --- src/Diagnostics/DiagnosticsHolder.cpp | 6 +++++- tests/integration/DiagnosticsTests.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Diagnostics/DiagnosticsHolder.cpp b/src/Diagnostics/DiagnosticsHolder.cpp index 9fa7e5f..2ada5cd 100644 --- a/src/Diagnostics/DiagnosticsHolder.cpp +++ b/src/Diagnostics/DiagnosticsHolder.cpp @@ -57,7 +57,11 @@ size_t DiagnosticsHolder::GetLineFromPosition(size_t i) { if (pos > i){ topLimit = half; } else if (pos < i){ - bottomLimit = half; + if (bottomLimit == half){ + bottomLimit = half + 1; + } else{ + bottomLimit = half; + } } else{ return half; } diff --git a/tests/integration/DiagnosticsTests.cpp b/tests/integration/DiagnosticsTests.cpp index 17f3818..52f2235 100644 --- a/tests/integration/DiagnosticsTests.cpp +++ b/tests/integration/DiagnosticsTests.cpp @@ -41,6 +41,18 @@ TEST_CASE( "Get diagnostic line", "[integration]" ) { delete script; } +TEST_CASE( "Get diagnostic line 2", "[integration]" ) { + auto script = Script::Create(u"\n'\\x'"); + REQUIRE(script->Diagnostics -> HasErrors()); + auto diags = script->Diagnostics -> GetDiagnostics(); + REQUIRE(diags.size() == 1); + CHECK(diags[0].GetCode() == Diagnostics::DiagnosticCode::InvalidStringControlCharacter); + CHECK(diags[0].GetStartPosition() == 2); + CHECK(diags[0].GetLength() == 2); + CHECK(script->Diagnostics->GetLineFromPosition(diags[0].GetStartPosition()) == 1); + delete script; +} + TEST_CASE( "Get full diagnostic message", "[integration]" ) { auto script = Script::Create(uR"( "\x"