@@ -21,6 +21,9 @@ class BadAckError < RuntimeError; end
21
21
# thrown when a response is incorrect
22
22
class BadResponseError < RuntimeError ; end
23
23
24
+ # thrown when a checksum is invalid
25
+ class BadChecksumError < RuntimeError ; end
26
+
24
27
# Default list of supported GDB features to send the to the target
25
28
GDB_FEATURES = 'qSupported:multiprocess+;qRelocInsn+;qvCont+;'
26
29
@@ -58,12 +61,15 @@ def send_cmd(cmd)
58
61
# Reads (and possibly decodes) from the socket and sends an ACK to verify receipt
59
62
# @param opts [Hash] the options hash
60
63
# @option opts :decode [Boolean] rle decoding should be applied to the response
64
+ # @option opts :verify [Boolean] verify the response's checksum
61
65
# @return [String] the response
62
66
# @raise [BadResponseError] if the expected response is missing
67
+ # @raise [BadChecksumError] if the checksum is invalid
63
68
def read_response ( opts = { } )
64
- decode = opts . fetch ( :decode , false )
69
+ decode , verify = opts . fetch ( :decode , false ) , opts . fetch ( :verify , true )
65
70
res = sock . get_once
66
71
raise BadResponseError if res . nil?
72
+ raise BadChecksumError if ( verify && !verify_checksum ( res ) )
67
73
res = decode_rle ( res ) if decode
68
74
vprint_status ( 'Result: ' +res )
69
75
send_ack
@@ -86,12 +92,20 @@ def decode_rle(msg)
86
92
87
93
# The two-digit checksum is computed as the modulo 256 sum of all characters
88
94
# between the leading ‘$’ and the trailing ‘#’ (an eight bit unsigned checksum).
89
- # @param [String] str the string to calculate the checksum of
95
+ # @param str [String] the string to calculate the checksum of
90
96
# @return [String] hex string containing checksum
91
97
def checksum ( str )
92
98
"%02x" % str . bytes . inject ( 0 ) { |b , sum | ( sum +b ) %256 }
93
99
end
94
100
101
+ # Verifies a response's checksum
102
+ # @param res [String] the response to check
103
+ # @return [Boolean] whether the checksum is valid
104
+ def verify_checksum ( res )
105
+ msg , chksum = res . match ( /^\$ (.*)#(\h {2})$/ ) [ 1 ..2 ]
106
+ checksum ( msg ) == chksum
107
+ end
108
+
95
109
# Writes the buffer +buf+ to the address +addr+ in the remote process's memory
96
110
# @param buf [String] the buffer to write
97
111
# @param addr [String] the hex-encoded address to write to
0 commit comments