sg_netChat: Use std::string to prevent crash with old strdup.

Thanks to Godspeed for noticing this code crashing with MSVC.
Using std::string should fix this.
This commit is contained in:
Thomas Geymayer 2013-03-03 01:07:35 +01:00
parent f21b0985cc
commit 33e60725b1
2 changed files with 18 additions and 25 deletions

View File

@ -25,21 +25,20 @@
#include <simgear/io/sg_netChat.hxx> #include <simgear/io/sg_netChat.hxx>
#include <cstring> // for strdup #include <cstring>
#include <cstdlib> #include <cstdlib>
namespace simgear { namespace simgear {
void void
NetChat::setTerminator (const char* t) NetChat::setTerminator(const std::string& t)
{ {
if (terminator) free(terminator); terminator = t;
terminator = strdup(t);
bytesToCollect = -1; bytesToCollect = -1;
} }
const char* const std::string&
NetChat::getTerminator (void) NetChat::getTerminator() const
{ {
return terminator; return terminator;
} }
@ -48,8 +47,7 @@ NetChat::getTerminator (void)
void void
NetChat::setByteCount(int count) NetChat::setByteCount(int count)
{ {
if (terminator) free(terminator); terminator.clear();
terminator = NULL;
bytesToCollect = count; bytesToCollect = count;
} }
@ -59,15 +57,15 @@ NetChat::setByteCount(int count)
#define MAX(a,b) (((a)>(b))?(a):(b)) #define MAX(a,b) (((a)>(b))?(a):(b))
static int static int
find_prefix_at_end (const NetBuffer& haystack, const char* needle) find_prefix_at_end(const NetBuffer& haystack, const std::string& needle)
{ {
const char* hd = haystack.getData(); const char* hd = haystack.getData();
int hl = haystack.getLength(); int hl = haystack.getLength();
int nl = strlen(needle); int nl = needle.length();
for (int i = MAX (nl-hl, 0); i < nl; i++) { for (int i = MAX (nl-hl, 0); i < nl; i++) {
//if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) { //if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) {
if (memcmp(needle, &hd[hl-(nl-i)], nl-i) == 0) { if (memcmp(needle.c_str(), &hd[hl-(nl-i)], nl-i) == 0) {
return (nl-i); return (nl-i);
} }
} }
@ -75,12 +73,12 @@ find_prefix_at_end (const NetBuffer& haystack, const char* needle)
} }
static int static int
find_terminator (const NetBuffer& haystack, const char* needle) find_terminator(const NetBuffer& haystack, const std::string& needle)
{ {
if (needle && *needle) if( !needle.empty() )
{ {
const char* data = haystack.getData(); const char* data = haystack.getData();
const char* ptr = strstr(data,needle); const char* ptr = strstr(data,needle.c_str());
if (ptr != NULL) if (ptr != NULL)
return(ptr-data); return(ptr-data);
} }
@ -102,7 +100,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
while (in_buffer.getLength()) { while (in_buffer.getLength()) {
// special case where we're not using a terminator // special case where we're not using a terminator
if (terminator == 0 || *terminator == 0) { if ( terminator.empty() ) {
if ( bytesToCollect > 0) { if ( bytesToCollect > 0) {
const int toRead = std::min(in_buffer.getLength(), bytesToCollect); const int toRead = std::min(in_buffer.getLength(), bytesToCollect);
collectIncomingData(in_buffer.getData(), toRead); collectIncomingData(in_buffer.getData(), toRead);
@ -119,8 +117,6 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
continue; continue;
} }
int terminator_len = strlen(terminator);
int index = find_terminator ( in_buffer, terminator ) ; int index = find_terminator ( in_buffer, terminator ) ;
// 3 cases: // 3 cases:
@ -134,7 +130,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer)
if (index != -1) { if (index != -1) {
// we found the terminator // we found the terminator
collectIncomingData ( in_buffer.getData(), index ) ; collectIncomingData ( in_buffer.getData(), index ) ;
in_buffer.remove (0, index + terminator_len); in_buffer.remove (0, index + terminator.length());
foundTerminator(); foundTerminator();
} else { } else {
// check for a prefix of the terminator // check for a prefix of the terminator

View File

@ -61,8 +61,6 @@
#ifndef SG_NET_CHAT_H #ifndef SG_NET_CHAT_H
#define SG_NET_CHAT_H #define SG_NET_CHAT_H
#include <memory>
#include <cstdlib>
#include <simgear/io/sg_netBuffer.hxx> #include <simgear/io/sg_netBuffer.hxx>
namespace simgear namespace simgear
@ -70,19 +68,18 @@ namespace simgear
class NetChat : public NetBufferChannel class NetChat : public NetBufferChannel
{ {
char* terminator; std::string terminator;
int bytesToCollect; int bytesToCollect;
virtual void handleBufferRead (NetBuffer& buffer) ; virtual void handleBufferRead (NetBuffer& buffer) ;
public: public:
NetChat () : NetChat () :
terminator (NULL),
bytesToCollect(-1) bytesToCollect(-1)
{} {}
void setTerminator (const char* t); void setTerminator(const std::string& t);
const char* getTerminator (void); const std::string& getTerminator() const;
/** /**
* set byte count to collect - 'foundTerminator' will be called once * set byte count to collect - 'foundTerminator' will be called once