From 2e850bc3b64f8dfa82d13afab0e0e53e5f0acb0d Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 3 Jan 2024 23:13:27 -0500 Subject: [PATCH] Fix out-of-bound index access --- lib/debug/variable_inspector.rb | 4 +++- test/protocol/hover_raw_dap_test.rb | 26 +++++++++----------------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/debug/variable_inspector.rb b/lib/debug/variable_inspector.rb index 15c255013..f74e170db 100644 --- a/lib/debug/variable_inspector.rb +++ b/lib/debug/variable_inspector.rb @@ -7,7 +7,9 @@ class VariableInspector def indexed_members_of(obj, start:, count:) return [] if start > (obj.length - 1) - (start...(start + count)).map do |i| + capped_count = [count, obj.length - start].min + + (start...(start + capped_count)).map do |i| Variable.new(name: i.to_s, value: obj[i]) end end diff --git a/test/protocol/hover_raw_dap_test.rb b/test/protocol/hover_raw_dap_test.rb index 47c02405b..35af47b22 100644 --- a/test/protocol/hover_raw_dap_test.rb +++ b/test/protocol/hover_raw_dap_test.rb @@ -619,14 +619,6 @@ def test_hover_works_correctly variablesReference: 30, namedVariables: /\d+/ }, - { - # This is an extra entry caused by an out-of-bounds index access in server_dap.rb - name: "9", - value: nil, - type: NilClass, - variablesReference: 31, - namedVariables: /\d+/ - } ] } }, @@ -649,7 +641,7 @@ def test_hover_works_correctly message: "Success", body: { type: "Integer", - variablesReference: 32, + variablesReference: 31, namedVariables: /\d+/, result: "3" } @@ -658,7 +650,7 @@ def test_hover_works_correctly seq: 23, command: "variables", arguments: { - variablesReference: 32 + variablesReference: 31 }, type: "request" }, @@ -675,7 +667,7 @@ def test_hover_works_correctly name: "#class", value: "Integer", type: "Class", - variablesReference: 33, + variablesReference: 32, namedVariables: /\d+/ } ] @@ -700,7 +692,7 @@ def test_hover_works_correctly message: "Success", body: { type: "Integer", - variablesReference: 34, + variablesReference: 33, namedVariables: /\d+/, result: "2" } @@ -709,7 +701,7 @@ def test_hover_works_correctly seq: 25, command: "variables", arguments: { - variablesReference: 34 + variablesReference: 33 }, type: "request" }, @@ -726,7 +718,7 @@ def test_hover_works_correctly name: "#class", value: "Integer", type: "Class", - variablesReference: 35, + variablesReference: 34, namedVariables: /\d+/ } ] @@ -751,7 +743,7 @@ def test_hover_works_correctly message: "Success", body: { type: "Integer", - variablesReference: 36, + variablesReference: 35, namedVariables: /\d+/, result: "1" } @@ -760,7 +752,7 @@ def test_hover_works_correctly seq: 27, command: "variables", arguments: { - variablesReference: 36 + variablesReference: 35 }, type: "request" }, @@ -777,7 +769,7 @@ def test_hover_works_correctly name: "#class", value: "Integer", type: "Class", - variablesReference: 37, + variablesReference: 36, namedVariables: /\d+/ } ]