Skip to content

Commit

Permalink
Use Endian >> ifBig:ifLittle: more consistently
Browse files Browse the repository at this point in the history
This fixes potential subtle bugs, like the one in `#read32At:` which did

    processorDescription endian == #little ifTrue: [ answer := answer byteSwap32 ].

This naturally stopped working when ArchC moved towards use of `Endian`,
causing silent heap corruption since no system was considered as LE.

Using `#ifBig:ifLittle:` also protect us against mixed-endian systems
should we ever come across them.
  • Loading branch information
janvrany committed Jun 30, 2023
1 parent 454d4ee commit d09c414
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
17 changes: 6 additions & 11 deletions src/GDB/RemoteGDB.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ RemoteGDB >> read32At: addr [
addr printStringHex,
',4'.
answer := Integer readFrom: answer radix: 16.
processorDescription endian == #little ifTrue: [ answer := answer byteSwap32 ].
processorDescription endian ifLittle: [ answer := answer byteSwap32 ].
^answer
]

Expand Down Expand Up @@ -240,21 +240,16 @@ RemoteGDB >> writeBytes: aByteArray toAddr: addr [

{ #category : #memory }
RemoteGDB >> writeInt32: int toAddr: addr [
| textualAddr answer data |
data := int printStringBase: 16 length: 8 padded: true.
processorDescription endian == #little ifTrue: [ data := data reverseInt32Endianness ].
textualAddr := addr printStringBase: 16 length: 8 padded: true.
answer := rsp
q: 'M', textualAddr, ',4:', data.
answer = 'OK' ifFalse: [ self error: answer ].


self writeInt32s: { int } toAddr: addr
]

{ #category : #memory }
RemoteGDB >> writeInt32s: arrayOfInt32s toAddr: addr [
| adjusted buffer textualAddr answer textualSize |
adjusted := processorDescription endian ifBig: [ arrayOfInt32s ] ifLittle: [ arrayOfInt32s collect: #byteSwap32 ].
adjusted := processorDescription endian
ifBig: [ arrayOfInt32s ]
ifLittle: [ arrayOfInt32s collect: #byteSwap32 ].

buffer := WriteStream on: ''.
adjusted do: [ :anInt32 |
| data |
Expand Down
10 changes: 5 additions & 5 deletions src/GDB/RemoteRAM.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ RemoteRAM >> read: n bytesAtAddr: addr [
^answer
]

{ #category : #'remote endian' }
{ #category : #reading }
RemoteRAM >> readInt32fromAddr: addr [
"Read, using the REMOTE TARGET endianness."
^self unsignedLongAtAddr: addr bigEndian: self endianness class = BigEndian
^self unsignedLongAtAddr: addr
]

{ #category : #reading }
Expand Down Expand Up @@ -243,8 +243,8 @@ RemoteRAM >> unsignedLongAt: byteIndex put: int [
]

{ #category : #reading }
RemoteRAM >> unsignedLongAtAddr: byteIndex [
^self unsignedLongAtAddr: byteIndex endian: self endianness
RemoteRAM >> unsignedLongAtAddr: addr [
^self unsignedLongAtAddr: addr bigEndian: (self endianness ifBig:[true] ifLittle:[false])
]

{ #category : #reading }
Expand Down Expand Up @@ -325,7 +325,7 @@ RemoteRAM >> writeBytesHex: aString toAddrHex: addr hexSize: s [
{ #category : #writing }
RemoteRAM >> writeInt32: int toAddr: addr [
"Use the REMOTE TARGET's endianness."
self longAtAddr: addr put: int bigEndian: self endianness class = BigEndian
self longAtAddr: addr put: int bigEndian: (self endianness ifBig:[true] ifLittle:[false])
]

{ #category : #writing }
Expand Down

0 comments on commit d09c414

Please sign in to comment.