Skip to content

Commit 92e3647

Browse files
committed
Fix wc_MXC_TPU_SHA_Copy to deep copy src msg buffer instead of freed dst pointer
1 parent 5b3750c commit 92e3647

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

wolfcrypt/src/port/maxim/max3266x.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,23 +808,26 @@ int wc_MXC_TPU_SHA_Copy(void* src, void* dst, word32 ctxSz,
808808
byte** dstMsg, word32* dstUsed, word32* dstLen,
809809
void* dstHeap, void* srcHeap)
810810
{
811-
byte* srcBuf;
811+
byte* srcBuf = NULL;
812812

813813
if (src == NULL || dst == NULL || dstMsg == NULL ||
814814
dstUsed == NULL || dstLen == NULL || ctxSz == 0) {
815815
return BAD_FUNC_ARG;
816816
}
817817

818-
srcBuf = *dstMsg;
819-
820818
/* Free existing dst msg buffer using dst's original heap */
821819
wc_MXC_TPU_SHA_Free(dstMsg, dstUsed, dstLen, dstHeap);
822820

823821
/* Shallow copy the full context struct */
824822
XMEMCPY(dst, src, ctxSz);
825823

826-
/* Deep copy src msg buffer if present, allocate using src's heap */
827-
if (srcBuf != NULL) {
824+
/* Deep copy src msg buffer if present. Since dstMsg points into the dst
825+
* struct, the XMEMCPY above overwrites it with the src's msg pointer.
826+
* Save that pointer, allocate a new buffer for dst, and copy the data.
827+
* Do NOT move srcBuf assignment before XMEMCPY - it must capture the
828+
* src msg pointer that lands in *dstMsg after the shallow copy. */
829+
if (*dstMsg != NULL) {
830+
srcBuf = *dstMsg;
828831
*dstMsg = (byte*)XMALLOC(*dstLen, srcHeap, DYNAMIC_TYPE_TMP_BUFFER);
829832
if (*dstMsg == NULL) {
830833
return MEMORY_E;

0 commit comments

Comments
 (0)