[vtkusers] Patch suggestion (bugfix) for vtkSocketCommunicator.cxx

Markus Neff Markus_Neff at t-online.de
Sun Jan 12 15:55:36 EST 2003


Dear mailinglist,

as I posted some weeks ago, I implemented a preliminary
"vtkServerSocketCommunicator" class, but I don't have
the time to thoroughly test it at the moment so it will take
some time before I can give it away.
However I think that during my work on the new class, I found a
potential bug in "vtkSocketCommunicator.cxx":
The "recv()" function call might - at least on Windows - return
"0" if the other end of the communication channel closes its
socket. I am not sure if this is true for all Unix-like systems
or if "send()" also might return "0" in some cases, but
checking for "0" in addition to checking for "-1" as a return
value would not hurt. I attached a patch against the CVS "HEAD"
revision of "vtkSocketCommunicator.cxx".

Best regards,
Markus


patch below:
==========================================================================

--- VTK_orig/Parallel/vtkSocketCommunicator.cxx	2003-01-02 12:56:45.000000000 +0000
+++ VTK_my/Parallel/vtkSocketCommunicator.cxx	2003-01-02 13:23:30.000000000 +0000
@@ -44,14 +44,14 @@
     } 
 
 #define vtkSCSendError \
-  if (sent == -1) \
+  if (sent == -1 || sent == 0) \
     { \
     vtkGenericWarningMacro("Could not send message."); \
     return 0; \
     } 
 
 #define vtkSCReceiveError \
-  if (received == -1) \
+  if (received == -1 || received == 0) \
     { \
     vtkErrorMacro("Could not receive message."); \
     return 0; \
@@ -79,6 +79,7 @@
   if (this->IsConnected)
     {
     vtkCloseSocketMacro(this->Socket);
+    this->Socket = -1;
     }
 
   if ( this->TraceFile )
@@ -123,7 +124,7 @@
   int sent, total = 0;
 
   total = send(sock, (char*)&tag, sizeof(int), 0);
-  if (total == -1)
+  if (total == -1 || total == 0)
     {
     vtkGenericWarningMacro("Could not send tag.");
     return 0;
@@ -136,7 +137,7 @@
     }
 
   total = send(sock, data, length, 0);
-  if (total == -1)
+  if (total == -1 || total == 0)
     {
     vtkGenericWarningMacro("Could not send message.");
     return 0;
@@ -274,7 +275,7 @@
 
   total = recv( this->Socket, charTag, sizeof(int), MSG_PEEK );
 
-  if ( total == -1 )
+  if ( total == -1 || total == 0 )
     {
     vtkErrorMacro("Could not receive tag.");
     if ( this->TraceFile )
@@ -307,7 +308,8 @@
   
   // Since we've already peeked at the entire tag, it must all be here, so
   // we should be able to get all of it in one try.
-  if (recv( this->Socket, charTag, sizeof(int), 0 ) == -1)
+  received = recv( this->Socket, charTag, sizeof(int), 0 );
+  if ( received == -1 || received == 0 )
     {
     if ( this->TraceFile )
       {
@@ -318,7 +320,7 @@
     }
 
   total = recv( this->Socket, data, totalLength, 0 );
-  if (total == -1)
+  if (total == -1 || total == 0)
     {
     if ( this->TraceFile )
       {
@@ -381,7 +383,7 @@
     }
 #endif
 
-  if ( *length < 0 )
+  if ( *length <= 0 )
     {
     return VTK_ERROR;
     }
@@ -542,7 +544,8 @@
     return 0;
     }
   vtkCloseSocketMacro(sock);
-    
+  sock = -1;
+  
   this->IsConnected = 1;
 
   if ( this->PerformHandshake )
@@ -580,6 +583,7 @@
   if ( this->IsConnected )
     {
     vtkCloseSocketMacro(this->Socket);
+    this->Socket = -1;
     this->IsConnected = 0;
     }
 }




More information about the vtkusers mailing list