From d345fbadd40b737f4e0a29e8abc333e2f4211453 Mon Sep 17 00:00:00 2001 From: Vyacheslav Egorov Date: Mon, 7 Oct 2024 10:53:42 +0000 Subject: [PATCH] [tools] Fix heapsnapshot console crashes `Console.cursorPosition` can return null when you are typing too fast because it is implemented by sending an escape code to the terminal and reading what it responds. This response is naturally intermingled with normal input and the implementation does nothing to guard against that. We do not really need to know the cursor position though we are simply interested in keeping cursor on the same row. But this can be achieved by sending _horizontal position absolute_ which only changes column rather than _cursor position_ which sets both row and column. R=kustermann@google.com TEST=manually tested Change-Id: Ie1a064e37b90bc4529ac4f5c1259642ad5680ca6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/388340 Commit-Queue: Slava Egorov Reviewed-by: Martin Kustermann --- .gitignore | 1 + runtime/tools/heapsnapshot/lib/src/console.dart | 13 ++++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 083421b4965d..2be1232147d2 100644 --- a/.gitignore +++ b/.gitignore @@ -112,3 +112,4 @@ logs/logs.json logs/results.json .dart_tool/bisect_dart/ doc/api/ +runtime/tools/heapsnapshot/.dart_tool diff --git a/runtime/tools/heapsnapshot/lib/src/console.dart b/runtime/tools/heapsnapshot/lib/src/console.dart index 2bcdc860a49c..2aca6abac8cc 100644 --- a/runtime/tools/heapsnapshot/lib/src/console.dart +++ b/runtime/tools/heapsnapshot/lib/src/console.dart @@ -21,12 +21,13 @@ class SmartConsole extends Console { ]; } + void moveCursorToColumn(int column) { + write('\x1b[${column + 1}`'); + } + ReadLineResult smartReadLine() { final buffer = LineEditBuffer(); - int promptRow = cursorPosition!.row; - - cursorPosition = Coordinate(promptRow, 0); drawPrompt(buffer); while (true) { @@ -67,7 +68,6 @@ class SmartConsole extends Console { } } - cursorPosition = Coordinate(promptRow, 0); drawPrompt(buffer); } } @@ -75,8 +75,7 @@ class SmartConsole extends Console { void drawPrompt(LineEditBuffer buffer) { const prefix = '(hsa) '; - final row = cursorPosition!.row; - + moveCursorToColumn(0); setForegroundColor(ConsoleColor.brightBlue); write(prefix); resetColorAttributes(); @@ -93,7 +92,7 @@ class SmartConsole extends Console { write(buffer.text); } - cursorPosition = Coordinate(row, prefix.length + buffer.index); + moveCursorToColumn(prefix.length + buffer.index); } }