strncasecmp + Ethernet = very odd bug?

This bug has been driving me mad for two days now.

With no luck finding anything online, I thought I’d post to see if some wise person can find the solution.

The bug: On a second telnet connection to the Arduino Ethernet library, strncasecmp & strncasecmp_P stop working.

To produce the bug:

(I’m using ethermega board and PuTTy for telnet)

1)Upload the attached code.

2)Connect with telnet. (192.168.0.177)

3)type: help

4)See that it works

5)Close telnet

6)Connect with telnet.

7)type: help

8)See that the behavior has changed, the command is no longer recognised.

Iv’e used serial debug statements to show:

-strncasecmp_P is passed valid arguments

-strlen_P returns the correct value

-The string in PROGMEM is otherwise usable and prints correctly.

-copying the string to ram and using strncasecmp instead does not fix the issue.

-Before the bug strncasecmp_P can return [-7, 0 or 7]

-After the bug strncasecmp_P can only return 151.

Tested on two different ethermega boards, on two different machines, no USB hubs.

Any help very appreciated. [Even anyone willing to verify this on a non-mega arduino shield.]

-Wazza

#include <Vec.h>
#include <SPI.h>
#include <Ethernet.h>

//ethernet
byte mac[] = { 0x0E, 0x00, 0x11, 0x22, 0x33, 0x44 };
IPAddress ip(192,168,0, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
EthernetServer server(23);
EthernetClient client = NULL;
boolean alreadyConnected = false; // whether or not the client was connected previously

//command line buffer
#define COMMAND_LINE_BUFFER_SIZE 64
char commandLineBuffer[COMMAND_LINE_BUFFER_SIZE];
int commandLineBufferLen = 0;

//progmem data
const char  pmCmdHelp[]        PROGMEM = "help";

void setup() 
{
	Ethernet.begin(mac, ip, gateway, subnet);
	server.begin();
}


void loop() 
{
    EthernetClient client = server.available();
	if (client == true) 
	{
		if (!alreadyConnected) 
		{
			client.flush();
			alreadyConnected = true;
			server.println("Hello dude!");
		}

		if (client.available() > 0)
		{
			char c = client.read();
			switch (c)
			{
				case '\r':
				case '\n':
					if(commandLineBufferLen > 0)
					{
						doCmd(commandLineBuffer);
					}
					commandLineBuffer[0] = '\0';
					commandLineBufferLen = 0;
					
					break;
				default:
					if(commandLineBufferLen < (COMMAND_LINE_BUFFER_SIZE-1))
					{
						commandLineBuffer[commandLineBufferLen] = c;
						commandLineBuffer[commandLineBufferLen+1] = '\0';
						commandLineBufferLen++;
					}
					break;
			}
			
		}
	}

	delay(10);

}

bool doCmd(const char* cmd)
{
    server.print("\"");
    server.print(cmd);
    server.println("\"");
    
    int len = strlen_P(pmCmdHelp);
    if(strncasecmp_P(cmd, pmCmdHelp, len) == 0)
	{
		server.println("Help command!");
	}
	else
	{
		server.println("Unknown command!");
	}
}