@@ -101,18 +101,22 @@ func (hp *HopPayload) Decode(r io.Reader) error {
101101
102102 var payloadSize uint16
103103
104- // If the HopPayload isn't guaranteed to be a TLV payload, we check the
105- // first byte to see if it is a legacy payload.
106- if hp .Type != PayloadTLV && isLegacyPayloadByte (peekByte [0 ]) {
104+ // If the HopPayload might be a legacy payload (indicated by the Type
105+ // being equal to the zero-value PayloadLegacy), we check the first
106+ // byte to see if it is a legacy payload.
107+ if hp .Type == PayloadLegacy && isLegacyPayloadByte (peekByte [0 ]) {
107108 payloadSize = legacyPayloadSize ()
108109 } else {
109- // If the first byte doesn't indicate a legacy payload, then it
110- // *must* be a TLV payload.
110+ // Either this is already known to be a TLV payload, or the
111+ // first byte indicates that this is a TLV payload (i.e., the
112+ // first byte is not 0x00).
111113 payloadSize , err = tlvPayloadSize (bufReader )
112114 if err != nil {
113115 return err
114116 }
115117
118+ // We still need to set the payload type in case it was the
119+ // zero-value, PayloadLegacy, AND the first byte was not 0x00.
116120 hp .Type = PayloadTLV
117121 }
118122
@@ -140,11 +144,15 @@ func readPayloadAndHMAC(hp *HopPayload, r io.Reader, payloadSize uint16) error {
140144 // Now that we know the payload size, we'll create a new buffer to read
141145 // it out in full.
142146 hp .Payload = make ([]byte , payloadSize )
143- if _ , err := io .ReadFull (r , hp .Payload [:]); err != nil {
144- return err
147+
148+ _ , err := io .ReadFull (r , hp .Payload )
149+ if err != nil {
150+ return fmt .Errorf ("%w : %w" , ErrIOReadFull , err )
145151 }
146- if _ , err := io .ReadFull (r , hp .HMAC [:]); err != nil {
147- return err
152+
153+ _ , err = io .ReadFull (r , hp .HMAC [:])
154+ if err != nil {
155+ return fmt .Errorf ("%w : %w" , ErrIOReadFull , err )
148156 }
149157
150158 return nil
0 commit comments