Skip to content

Commit 98fb9f2

Browse files
committed
Review wxFAIL usage in the eD2k client code
Most of them have been changed to produce a bit more meaningful error message in the unlikely case of failing. Those that could be triggered by external conditions (bogus/malicious client sending broken packets) have been changed to simply log the error and ignore the packet.
1 parent 2cae039 commit 98fb9f2

File tree

2 files changed

+70
-70
lines changed

2 files changed

+70
-70
lines changed

src/BaseClient.cpp

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,7 @@ bool CUpDownClient::ProcessHelloTypePacket(const CMemFile& data)
723723

724724
bool CUpDownClient::SendHelloPacket()
725725
{
726-
if (m_socket == NULL) {
727-
wxFAIL;
728-
return true;
729-
}
726+
wxCHECK(m_socket != NULL, true);
730727

731728
// if IP is filtered, don't greet him but disconnect...
732729
if (theApp->ipfilter->IsFiltered(m_socket->GetPeerInt())) {
@@ -749,12 +746,9 @@ bool CUpDownClient::SendHelloPacket()
749746
return true;
750747
}
751748

752-
void CUpDownClient::SendMuleInfoPacket(bool bAnswer, bool OSInfo) {
753-
754-
if (m_socket == NULL){
755-
wxFAIL;
756-
return;
757-
}
749+
void CUpDownClient::SendMuleInfoPacket(bool bAnswer, bool OSInfo)
750+
{
751+
wxCHECK2(m_socket != NULL, return);
758752

759753
CPacket* packet = NULL;
760754
CMemFile data;
@@ -1015,10 +1009,7 @@ bool CUpDownClient::ProcessMuleInfoPacket(const byte* pachPacket, uint32 nSize)
10151009

10161010
void CUpDownClient::SendHelloAnswer()
10171011
{
1018-
if (m_socket == NULL){
1019-
wxFAIL;
1020-
return;
1021-
}
1012+
wxCHECK2(m_socket != NULL, return);
10221013

10231014
CMemFile data(128);
10241015
SendHelloTypePacket(&data);
@@ -2051,12 +2042,13 @@ bool CUpDownClient::SafeSendPacket(CPacket* packet)
20512042
}
20522043
}
20532044

2054-
void CUpDownClient::SendPublicKeyPacket(){
2045+
void CUpDownClient::SendPublicKeyPacket()
2046+
{
20552047
// send our public key to the client who requested it
2056-
if (m_socket == NULL || credits == NULL || m_SecureIdentState != IS_KEYANDSIGNEEDED){
2057-
wxFAIL;
2058-
return;
2059-
}
2048+
wxCHECK2(m_socket != NULL, return);
2049+
wxCHECK2(credits != NULL, return);
2050+
wxCHECK2(m_SecureIdentState == IS_KEYANDSIGNEEDED, return);
2051+
20602052
if (!theApp->CryptoAvailable())
20612053
return;
20622054

@@ -2072,12 +2064,12 @@ void CUpDownClient::SendPublicKeyPacket(){
20722064
}
20732065

20742066

2075-
void CUpDownClient::SendSignaturePacket(){
2067+
void CUpDownClient::SendSignaturePacket()
2068+
{
20762069
// signate the public key of this client and send it
2077-
if (m_socket == NULL || credits == NULL || m_SecureIdentState == 0){
2078-
wxFAIL;
2079-
return;
2080-
}
2070+
wxCHECK2(m_socket != NULL, return);
2071+
wxCHECK2(credits != NULL, return);
2072+
wxCHECK2(m_SecureIdentState != 0, return);
20812073

20822074
if (!theApp->CryptoAvailable()) {
20832075
return;
@@ -2114,10 +2106,8 @@ void CUpDownClient::SendSignaturePacket(){
21142106
byte achBuffer[250];
21152107

21162108
uint8 siglen = theApp->clientcredits->CreateSignature(credits, achBuffer, 250, ChallengeIP, byChaIPKind );
2117-
if (siglen == 0){
2118-
wxFAIL;
2119-
return;
2120-
}
2109+
wxCHECK2(siglen != 0, return);
2110+
21212111
CMemFile data;
21222112
data.WriteUInt8(siglen);
21232113
data.Write(achBuffer, siglen);
@@ -2138,9 +2128,18 @@ void CUpDownClient::ProcessPublicKeyPacket(const byte* pachPacket, uint32 nSize)
21382128
{
21392129
theApp->clientlist->AddTrackClient(this);
21402130

2141-
if (m_socket == NULL || credits == NULL || pachPacket[0] != nSize-1
2142-
|| nSize == 0 || nSize > 250){
2143-
wxFAIL;
2131+
wxCHECK2(m_socket != NULL, return);
2132+
wxCHECK2(credits != NULL, return);
2133+
if (pachPacket[0] != nSize - 1) {
2134+
AddDebugLogLineN(logClient, CFormat(wxT("Inconsistent packet size (%d != %d)")) % pachPacket[0] % (nSize - 1));
2135+
return;
2136+
}
2137+
if (nSize == 0) {
2138+
AddDebugLogLineN(logClient, wxT("Invalid packet size (0)"));
2139+
return;
2140+
}
2141+
if (nSize > 250) {
2142+
AddDebugLogLineN(logClient, CFormat(wxT("Invalid packet size (%d > 250)")) % nSize);
21442143
return;
21452144
}
21462145
if (!theApp->CryptoAvailable())
@@ -2165,8 +2164,14 @@ void CUpDownClient::ProcessSignaturePacket(const byte* pachPacket, uint32 nSize)
21652164
{
21662165
// here we spread the good guys from the bad ones ;)
21672166

2168-
if (m_socket == NULL || credits == NULL || nSize == 0 || nSize > 250){
2169-
wxFAIL;
2167+
wxCHECK2(m_socket != NULL, return);
2168+
wxCHECK2(credits != NULL, return);
2169+
if (nSize == 0) {
2170+
AddDebugLogLineN(logClient, wxT("Invalid packet size (0)"));
2171+
return;
2172+
}
2173+
if (nSize > 250) {
2174+
AddDebugLogLineN(logClient, CFormat(wxT("Invalid packet size (%d > 250)")) % nSize);
21702175
return;
21712176
}
21722177

@@ -2175,8 +2180,9 @@ void CUpDownClient::ProcessSignaturePacket(const byte* pachPacket, uint32 nSize)
21752180
byChaIPKind = 0;
21762181
else if (pachPacket[0] == nSize-2 && (m_bySupportSecIdent & 2) > 0) //v2
21772182
byChaIPKind = pachPacket[nSize-1];
2178-
else{
2179-
wxFAIL;
2183+
else {
2184+
// Unknown or invalid format
2185+
AddDebugLogLineN(logClient, wxT("Invalid or unknown challenge format - ignoring"));
21802186
return;
21812187
}
21822188

@@ -2210,36 +2216,35 @@ void CUpDownClient::ProcessSignaturePacket(const byte* pachPacket, uint32 nSize)
22102216
m_dwLastSignatureIP = GetIP();
22112217
}
22122218

2213-
void CUpDownClient::SendSecIdentStatePacket(){
2219+
void CUpDownClient::SendSecIdentStatePacket()
2220+
{
2221+
wxCHECK2(credits != NULL, return);
2222+
22142223
// check if we need public key and signature
2215-
if (credits){
2216-
uint8 nValue = 0;
2217-
if (theApp->CryptoAvailable()){
2218-
if (credits->GetSecIDKeyLen() == 0) {
2219-
nValue = IS_KEYANDSIGNEEDED;
2220-
} else if (m_dwLastSignatureIP != GetIP()) {
2221-
nValue = IS_SIGNATURENEEDED;
2222-
}
2224+
uint8 nValue = 0;
2225+
if (theApp->CryptoAvailable()){
2226+
if (credits->GetSecIDKeyLen() == 0) {
2227+
nValue = IS_KEYANDSIGNEEDED;
2228+
} else if (m_dwLastSignatureIP != GetIP()) {
2229+
nValue = IS_SIGNATURENEEDED;
22232230
}
2224-
if (nValue == 0){
2225-
AddDebugLogLineN( logClient, wxT("Not sending SecIdentState Packet, because State is Zero") );
2226-
return;
2227-
}
2228-
// crypt: send random data to sign
2229-
uint32 dwRandom = rand()+1;
2230-
credits->m_dwCryptRndChallengeFor = dwRandom;
2231+
}
2232+
if (nValue == 0){
2233+
AddDebugLogLineN( logClient, wxT("Not sending SecIdentState Packet, because State is Zero") );
2234+
return;
2235+
}
2236+
// crypt: send random data to sign
2237+
uint32 dwRandom = rand()+1;
2238+
credits->m_dwCryptRndChallengeFor = dwRandom;
22312239

2232-
CMemFile data;
2233-
data.WriteUInt8(nValue);
2234-
data.WriteUInt32(dwRandom);
2235-
CPacket* packet = new CPacket(data, OP_EMULEPROT, OP_SECIDENTSTATE);
2240+
CMemFile data;
2241+
data.WriteUInt8(nValue);
2242+
data.WriteUInt32(dwRandom);
2243+
CPacket* packet = new CPacket(data, OP_EMULEPROT, OP_SECIDENTSTATE);
22362244

2237-
theStats::AddUpOverheadOther(packet->GetPacketSize());
2238-
AddDebugLogLineN( logLocalClient, wxT("Local Client: OP_SECIDENTSTATE to ") + GetFullIP() );
2239-
SendPacket(packet,true,true);
2240-
} else {
2241-
wxFAIL;
2242-
}
2245+
theStats::AddUpOverheadOther(packet->GetPacketSize());
2246+
AddDebugLogLineN( logLocalClient, wxT("Local Client: OP_SECIDENTSTATE to ") + GetFullIP() );
2247+
SendPacket(packet,true,true);
22432248
}
22442249

22452250

@@ -2249,10 +2254,7 @@ void CUpDownClient::ProcessSecIdentStatePacket(const byte* pachPacket, uint32 nS
22492254
return;
22502255
}
22512256

2252-
if ( !credits ) {
2253-
wxASSERT( credits );
2254-
return;
2255-
}
2257+
wxCHECK2(credits, return);
22562258

22572259
CMemFile data(pachPacket,nSize);
22582260

src/DownloadClient.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,9 @@ void CUpDownClient::SendBlockRequests()
683683
pblock->fRecovered = 0;
684684
m_PendingBlocks_list.push_back(pblock);
685685
}
686-
} else {
686+
} else {
687687
// WTF, we just freed blocks.
688-
wxFAIL;
688+
wxFAIL_MSG(wxT("No free blocks to request after freeing some blocks"));
689689
return;
690690
}
691691
} else {
@@ -746,7 +746,6 @@ void CUpDownClient::SendBlockRequests()
746746
bHasLongBlocks = true;
747747
if (!SupportsLargeFiles()){
748748
// Requesting a large block from a client that doesn't support large files?
749-
wxFAIL;
750749
if (!GetSentCancelTransfer()){
751750
CPacket* cancel_packet = new CPacket(OP_CANCELTRANSFER, 0, OP_EDONKEYPROT);
752751
theStats::AddUpOverheadFileRequest(cancel_packet->GetPacketSize());
@@ -755,6 +754,7 @@ void CUpDownClient::SendBlockRequests()
755754
SetSentCancelTransfer(1);
756755
}
757756
SetDownloadState(DS_ERROR);
757+
return;
758758
}
759759
break;
760760
}
@@ -809,8 +809,6 @@ void CUpDownClient::SendBlockRequests()
809809
if (packet) {
810810
theStats::AddUpOverheadFileRequest(packet->GetPacketSize());
811811
SendPacket(packet, true, true);
812-
} else {
813-
wxFAIL;
814812
}
815813
}
816814

0 commit comments

Comments
 (0)