X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Fsg_socket.cxx;h=4ca13150170e6bfba5eb052c8115ce6e85b1f668;hb=598b64fa9569c16878c904e344e2e2775e210c42;hp=636ff3452b19c8719640ccf7eb36ad4a93add1a5;hpb=e3d1fa268686f1d8b621241963665c2919a35aa2;p=simgear.git diff --git a/simgear/io/sg_socket.cxx b/simgear/io/sg_socket.cxx index 636ff345..4ca13150 100644 --- a/simgear/io/sg_socket.cxx +++ b/simgear/io/sg_socket.cxx @@ -36,6 +36,7 @@ #include "sg_socket.hxx" bool SGSocket::init = false; +using std::string; SGSocket::SGSocket( const string& host, const string& port_, const string& style ) : @@ -84,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()" ); @@ -296,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 @@ -305,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+, ? );