From 33e60725b175a449867a978dc40c6820293fe65d Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Sun, 3 Mar 2013 01:07:35 +0100 Subject: [PATCH] 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. --- simgear/io/sg_netChat.cxx | 32 ++++++++++++++------------------ simgear/io/sg_netChat.hxx | 11 ++++------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/simgear/io/sg_netChat.cxx b/simgear/io/sg_netChat.cxx index 4de77163..0559c09b 100644 --- a/simgear/io/sg_netChat.cxx +++ b/simgear/io/sg_netChat.cxx @@ -25,21 +25,20 @@ #include -#include // for strdup +#include #include namespace simgear { void -NetChat::setTerminator (const char* t) +NetChat::setTerminator(const std::string& t) { - if (terminator) free(terminator); - terminator = strdup(t); + terminator = t; bytesToCollect = -1; } -const char* -NetChat::getTerminator (void) +const std::string& +NetChat::getTerminator() const { return terminator; } @@ -48,8 +47,7 @@ NetChat::getTerminator (void) void NetChat::setByteCount(int count) { - if (terminator) free(terminator); - terminator = NULL; + terminator.clear(); bytesToCollect = count; } @@ -59,15 +57,15 @@ NetChat::setByteCount(int count) #define MAX(a,b) (((a)>(b))?(a):(b)) 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(); int hl = haystack.getLength(); - int nl = strlen(needle); + int nl = needle.length(); for (int i = MAX (nl-hl, 0); i < nl; i++) { //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); } } @@ -75,12 +73,12 @@ find_prefix_at_end (const NetBuffer& haystack, const char* needle) } 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* ptr = strstr(data,needle); + const char* ptr = strstr(data,needle.c_str()); if (ptr != NULL) return(ptr-data); } @@ -102,7 +100,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer) while (in_buffer.getLength()) { // special case where we're not using a terminator - if (terminator == 0 || *terminator == 0) { + if ( terminator.empty() ) { if ( bytesToCollect > 0) { const int toRead = std::min(in_buffer.getLength(), bytesToCollect); collectIncomingData(in_buffer.getData(), toRead); @@ -119,8 +117,6 @@ NetChat::handleBufferRead (NetBuffer& in_buffer) continue; } - int terminator_len = strlen(terminator); - int index = find_terminator ( in_buffer, terminator ) ; // 3 cases: @@ -134,7 +130,7 @@ NetChat::handleBufferRead (NetBuffer& in_buffer) if (index != -1) { // we found the terminator collectIncomingData ( in_buffer.getData(), index ) ; - in_buffer.remove (0, index + terminator_len); + in_buffer.remove (0, index + terminator.length()); foundTerminator(); } else { // check for a prefix of the terminator diff --git a/simgear/io/sg_netChat.hxx b/simgear/io/sg_netChat.hxx index 41097c0a..bc6ef656 100644 --- a/simgear/io/sg_netChat.hxx +++ b/simgear/io/sg_netChat.hxx @@ -61,8 +61,6 @@ #ifndef SG_NET_CHAT_H #define SG_NET_CHAT_H -#include -#include #include namespace simgear @@ -70,19 +68,18 @@ namespace simgear class NetChat : public NetBufferChannel { - char* terminator; + std::string terminator; int bytesToCollect; virtual void handleBufferRead (NetBuffer& buffer) ; public: - NetChat () : - terminator (NULL), + NetChat () : bytesToCollect(-1) {} - void setTerminator (const char* t); - const char* getTerminator (void); + void setTerminator(const std::string& t); + const std::string& getTerminator() const; /** * set byte count to collect - 'foundTerminator' will be called once -- 2.39.5