From 9e791b1bb92433b7444ca34934bfbf5873e6b697 Mon Sep 17 00:00:00 2001 From: Herman Slatman Date: Tue, 14 Mar 2023 01:53:49 +0100 Subject: [PATCH] Return the actual number of bytes written to through command buffer (#287) 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. --- simulator/simulator.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/simulator/simulator.go b/simulator/simulator.go index eed7e2ab2..e1230faaa 100644 --- a/simulator/simulator.go +++ b/simulator/simulator.go @@ -109,7 +109,9 @@ func (s *Simulator) Write(commandBuffer []byte) (int, error) { if err != nil { return 0, err } - return s.buf.Write(resp) + // write response to the internal response buffer. + _, _ = s.buf.Write(resp) + return len(commandBuffer), nil } // Read gets the response of a command previously issued by calling Write().