From c0ab1ba53f9eb092d85afee6d1bf197ddabea05f Mon Sep 17 00:00:00 2001 From: kevin Date: Sat, 10 Sep 2022 10:58:30 -0400 Subject: [PATCH] cwUi.cpp : Fix _copy_msg_to_buffer(). --- cwUi.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cwUi.cpp b/cwUi.cpp index cc51de4..285fb4e 100644 --- a/cwUi.cpp +++ b/cwUi.cpp @@ -1051,17 +1051,16 @@ namespace cw rc_t _copy_msg_to_recv_buffer( ui_t* p, const void* msg, unsigned msgByteN ) { - rc_t rc = kOkRC; + if( msg == nullptr || msgByteN == 0) + return kOkRC; if( p->recvBufIdx + msgByteN > p->recvBufN ) - rc = cwLogError(kBufTooSmallRC,"The UI input buffer (%i) is too small.", p->recvBufN); - else - { - memcpy(p->recvBuf + p->recvBufIdx, msg, msgByteN ); - p->recvBufIdx += msgByteN; - } + return cwLogError(kBufTooSmallRC,"The UI input buffer (%i) is too small.", p->recvBufN); - return rc; + memcpy(p->recvBuf + p->recvBufIdx, msg, msgByteN ); + p->recvBufIdx += msgByteN; + + return kOkRC; } const char* _get_msg_from_recv_buffer( ui_t* p ) @@ -1072,8 +1071,12 @@ namespace cw // shift off the previous msg if( p->recvShiftN > 0 ) { + assert( p->recvBufIdx >= p->recvShiftN ); + memmove(p->recvBuf, p->recvBuf+p->recvShiftN, p->recvBufIdx - p->recvShiftN ); + p->recvBufIdx -= p->recvShiftN; p->recvShiftN = 0; + } // locate the end of the next msg.