From a3c6a6116940c70adcc2707a7b28f33941bc38c8 Mon Sep 17 00:00:00 2001 From: Raoul Wols Date: Wed, 23 Mar 2022 21:22:36 +0100 Subject: [PATCH] Guard for overflow of the column offset when applying edits (#1952) --- plugin/core/edit.py | 4 ++-- plugin/core/protocol.py | 2 ++ tests/test_single_document.py | 9 ++++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/plugin/core/edit.py b/plugin/core/edit.py index 71181d4e5..b1c833286 100644 --- a/plugin/core/edit.py +++ b/plugin/core/edit.py @@ -1,7 +1,7 @@ from .logging import debug from .open import open_file from .promise import Promise -from .protocol import TextEdit as LspTextEdit, Position +from .protocol import UINT_MAX, TextEdit as LspTextEdit, Position from .typing import List, Dict, Any, Optional, Tuple from functools import partial import sublime @@ -34,7 +34,7 @@ def parse_workspace_edit(workspace_edit: Dict[str, Any]) -> Dict[str, List[TextE def parse_range(range: Position) -> Tuple[int, int]: - return range['line'], range['character'] + return range['line'], min(UINT_MAX, range['character']) def parse_text_edit(text_edit: LspTextEdit, version: int = None) -> TextEditTuple: diff --git a/plugin/core/protocol.py b/plugin/core/protocol.py index ee4e5e381..99525a85c 100644 --- a/plugin/core/protocol.py +++ b/plugin/core/protocol.py @@ -3,6 +3,8 @@ import os import sublime +INT_MAX = 2**31 - 1 +UINT_MAX = INT_MAX TextDocumentSyncKindNone = 0 TextDocumentSyncKindFull = 1 diff --git a/tests/test_single_document.py b/tests/test_single_document.py index bbb5934cf..5e32b87a5 100644 --- a/tests/test_single_document.py +++ b/tests/test_single_document.py @@ -8,6 +8,8 @@ from setup import YieldPromise import os import sublime +import sys + try: from typing import Generator, Optional, Iterable, Tuple, List @@ -321,7 +323,12 @@ def test_rename(self) -> 'Generator': 'newText': 'bar' }, { - 'range': {'start': {'character': 0, 'line': 2}, 'end': {'character': 3, 'line': 2}}, + 'range': + { + 'start': {'character': 0, 'line': 2}, + # Check that lsp_apply_document_edit guards for overflow by using sys.maxsize + 1 + 'end': {'character': sys.maxsize + 1, 'line': 2} + }, 'newText': 'bar' } ]