@@ -62,12 +62,7 @@ bool IsMarkerMatched(void *header, const void *marker, size_t len)
6262 return memcmp (header , marker , len ) == 0 ;
6363}
6464
65- void ShiftBufferToLeft (void * buffer , uint32_t len )
66- {
67- memmove ((uint8_t * )buffer , ((uint8_t * )buffer + 1 ), len - 1 );
68- }
69-
70- void SyncToMessageStart ()
65+ bool SyncToMessageStart ()
7166{
7267 uint32_t len ;
7368
@@ -88,16 +83,33 @@ void SyncToMessageStart()
8883 break ;
8984 }
9085
91- ShiftBufferToLeft (& _inboundMessage .m_header , len );
86+ // Calculate the source and destination pointers
87+ uint8_t * src = (uint8_t * )& _inboundMessage .m_header + 1 ;
88+ uint8_t * dst = (uint8_t * )& _inboundMessage .m_header ;
89+ size_t moveLength = len - 1 ;
9290
93- // update pointer and expected size
91+ // Ensure that the memory regions do not exceed allocated bounds
92+ if ((src + moveLength >= (uint8_t * )& _inboundMessage + sizeof (_inboundMessage )) ||
93+ (dst + moveLength >= (uint8_t * )& _inboundMessage + sizeof (_inboundMessage )))
94+ {
95+ return false;
96+ }
97+
98+ // Perform the memory move
99+ memmove (dst , src , moveLength );
100+
101+ // Update pointer and expected size
94102 _pos -- ;
95103 _size ++ ;
96104
97- // sanity checks
98- _ASSERTE (_size <= sizeof (_inboundMessage .m_header ));
99- _ASSERTE (_pos >= (uint8_t * )& (_inboundMessage .m_header ));
105+ // Sanity checks
106+ if (_size > sizeof (_inboundMessage .m_header ) || _pos < (uint8_t * )& _inboundMessage .m_header )
107+ {
108+ return false;
109+ }
100110 }
111+
112+ return true;
101113}
102114
103115void WP_ReplyToCommand (WP_Message * message , uint8_t fSuccess , uint8_t fCritical , void * ptr , uint32_t size )
@@ -357,7 +369,16 @@ void WP_Message_Process()
357369 }
358370 }
359371
360- SyncToMessageStart ();
372+ if (!SyncToMessageStart ())
373+ {
374+ // something went wrong
375+ TRACE0 (TRACE_ERRORS , "RxError: Failed to sync to message start\n" );
376+
377+ RestartStateMachine ();
378+
379+ // exit the loop to allow other RTOS threads to run
380+ return ;
381+ }
361382
362383 if (len >= sizeof (_inboundMessage .m_header .m_signature ))
363384 {
0 commit comments