[Spread-users] [PATCH] Java SpreadConnection.connect() differed from C API

Daniel Rall dlr at finemaltcoding.com
Wed Aug 1 23:32:43 EDT 2001


[This bug report and fix applies to version 3.16.0]

>From the man page of the SP_connect() function in the C client library:

       The private_group should be a  pointer  to  a  string  big
       enough  to hold at least MAX_GROUP_NAME characters.  After
       the Connect call returns it will contain the private group
       name  of  this connection.  This group name can be used to
       send unicast messages to this connection and  no  one  can
       join this special group.

The man page also states that when a NULL pointer is passed to the
private_name parameter of SP_connect(), the upon return of the
function the Spread daemon will have filled in the private_group
parameter with a unique, random name.

Passing a null to the privateName parameter of the SpreadConnection's
connect() method of the Java API currently results in a
NullPointerException on line 789, where String's substring() method is
called on privateName.

I found no reason why the Java client code couldn't take advantage of
this server-offered feature (and I wanted it! ;), so here's the patch:


--- SpreadConnection.java-ORIG	Wed Jun 27 06:10:31 2001
+++ SpreadConnection.java	Wed Aug  1 20:12:23 2001
@@ -153,10 +153,6 @@
 	/////////////////////
 	private int port;
 	
-	// This connection's private name.
-	//////////////////////////////////
-	private String privateName;
-	
 	// Is this a priority connection?
 	/////////////////////////////////
 	private boolean priority;
@@ -358,13 +354,16 @@
 	
 	// Sends the initial connect message.
 	/////////////////////////////////////
-	private void sendConnect() throws SpreadException
+	private void sendConnect(String privateName) throws SpreadException
 	{
-		// Check if the private name is too long.
-		/////////////////////////////////////////
-		int len = privateName.length();
+		// Check the private name for validity.
+		///////////////////////////////////////
+		int len = (privateName == null ? 0 : privateName.length());
 		if(len > MAX_PRIVATE_NAME)
+		{
+			privateName = privateName.substring(0, MAX_PRIVATE_NAME);
 			len = MAX_PRIVATE_NAME;
+		}
 		
 		// Allocate the buffer.
 		///////////////////////
@@ -398,12 +397,15 @@
 		////////////////////
 		buffer[4] = (byte)len;
 		
-		// Write the private name.
-		//////////////////////////
-		byte nameBytes[] = privateName.getBytes();
-		for(int src = 0, dest = 5 ; src < len ; src++, dest++)
+		if(len > 0)
 		{
-			buffer[dest] = nameBytes[src];
+			// Write the private name.
+			//////////////////////////
+			byte nameBytes[] = privateName.getBytes();
+			for(int src = 0, dest = 5 ; src < len ; src++, dest++)
+			{
+				buffer[dest] = nameBytes[src];
+			}
 		}
 		
 		// Send the connection message.
@@ -784,16 +786,6 @@
 		this.port = port;
 		this.priority = priority;
 		this.groupMembership = groupMembership;
-		try
-		{
-			this.privateName = privateName.substring(0, MAX_PRIVATE_NAME);
-		}
-		catch(IndexOutOfBoundsException e)
-		{
-			// Nothing to shorten.
-			//////////////////////
-			this.privateName = privateName;
-		}
 		
 		// Check if no address was specified.
 		/////////////////////////////////////
@@ -856,7 +848,7 @@
 		
 		// Send the connect message.
 		////////////////////////////
-		sendConnect();
+		sendConnect(privateName);
 		
 		// Recv the authentication method list
 		//////////////////////////////////////





More information about the Spread-users mailing list