Skip to content

Commit

Permalink
Return the actual number of bytes written to through command buffer
Browse files Browse the repository at this point in the history
The `Write` function returned the number of bytes written to the
internal command response buffer in the simulator. Depending on the
type of command executed against the TPM, this would usually result
in a different number of bytes being written than the original input.

The fix is useful if one wants to wrap the io.ReadWriterCloser
functions with implementations that perform strict checks. An example
is using an io.MultiWriter to capture the bytes sent to the TPM for
debugging purposes, which will fail if the number of bytes written
does not equal the original length of the input bytes.
  • Loading branch information
hslatman committed Feb 22, 2023
1 parent 51031c1 commit d9008ec
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion simulator/simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ func (s *Simulator) Write(commandBuffer []byte) (int, error) {
if err != nil {
return 0, err
}
return s.buf.Write(resp)
n, err := s.buf.Write(resp)
if err != nil {
return 0, err
}
if n != len(resp) {
return 0, fmt.Errorf("expected %d bytes to be written to command response buffer, but actual number is %d", n, len(resp))
}
return len(commandBuffer), nil
}

// Read gets the response of a command previously issued by calling Write().
Expand Down

0 comments on commit d9008ec

Please sign in to comment.