X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_socket.cxx;h=4ca13150170e6bfba5eb052c8115ce6e85b1f668;hb=8d93206dd33ed0079af6670a0ecd41a3b203d9a0;hp=14ae296937ee74c574a10dd39f16047f4255d689;hpb=6b3a05e23fc8f8121f24824711917e7255f17fa8;p=simgear.git diff --git a/simgear/io/sg_socket.cxx b/simgear/io/sg_socket.cxx index 14ae2969..4ca13150 100644 --- a/simgear/io/sg_socket.cxx +++ b/simgear/io/sg_socket.cxx @@ -28,12 +28,15 @@ #include #include +#include +#include // for atoi #include #include "sg_socket.hxx" bool SGSocket::init = false; +using std::string; SGSocket::SGSocket( const string& host, const string& port_, const string& style ) : @@ -82,7 +85,7 @@ SGSocket::make_server_socket() return false; } - if (sock.bind( "", port ) < 0) + if (sock.bind( hostname.c_str(), port ) < 0) { SG_LOG( SG_IO, SG_ALERT, "Error: bind() failed in make_server_socket()" ); @@ -294,7 +297,7 @@ SGSocket::readline( char *buf, int length ) int i; for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i ) ; - if ( save_buf[i] == '\n' ) { + if (( i < save_len ) && ( save_buf[i] == '\n' )) { result = i + 1; } else { // no end of line yet @@ -303,9 +306,16 @@ SGSocket::readline( char *buf, int length ) // we found an end of line + // check buffer size + int copy_length = result; + if (copy_length >= length) { + SG_LOG( SG_IO, SG_ALERT, + "Alert: readline() has line exceeding the buffer size." ); + copy_length = length-1; + } // copy to external buffer - strncpy( buf, save_buf, result ); - buf[result] = '\0'; + strncpy( buf, save_buf, copy_length ); + buf[copy_length] = '\0'; // shift save buffer //memmove( save_buf+, save_buf+, ? );