From 01cefe462fe37d1235f0518b2011d4ffb1a623d7 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Tue, 15 Mar 2022 13:53:34 -0700 Subject: [PATCH] pbot-vm: fix occasional "illegal escape sequence" in guest-gdb --- applets/pbot-vm/guest/bin/guest-gdb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/applets/pbot-vm/guest/bin/guest-gdb b/applets/pbot-vm/guest/bin/guest-gdb index 4b03cc5f..a3ae00fc 100755 --- a/applets/pbot-vm/guest/bin/guest-gdb +++ b/applets/pbot-vm/guest/bin/guest-gdb @@ -142,9 +142,17 @@ sub gdb_read_result($out) { return $output; } -sub gdbmi_to_json($text, $makejson = 1) { +# convert gdb/mi text to hashtable, converting to JSON first if necessary +sub gdbmi_to_hash($text, $makejson = 1) { + # convert to JSON first if necessary (gdb/mi is nearly JSON already!) $text =~ s/([\w-]+)=/"$1":/g if $makejson; - $text =~ s/\\(\d{3})/$1 >= 0x20 ? chr oct $1 : "\\$1"/ge; + + # decode gdb octal escapes + # (sometimes \\123, othertimes \123 and I can't be arsed to investigate + # when and why the backslashes double up) + $text =~ s/\\+(\d{3})/$1 >= 0x20 ? chr oct $1 : "\\\\$1"/ge; + + # return hashtable decoded from json return decode_json("{$text}"); } @@ -165,7 +173,7 @@ sub parse_gdbmi_output($line) { $text = "\"_text\":$text"; } - my $output = gdbmi_to_json($text, $makejson); + my $output = gdbmi_to_hash($text, $makejson); if (exists $output->{_text}) { chomp $output->{_text};