]> git.mxchange.org Git - simgear.git/blobdiff - simgear/io/sg_socket.cxx
Patch to avoid the problem of the socket resource not yet being available
[simgear.git] / simgear / io / sg_socket.cxx
index 14ae296937ee74c574a10dd39f16047f4255d689..770bb51dcbbbf274e84e996bc3fb12a1e04a6315 100644 (file)
@@ -28,6 +28,8 @@
 #include <simgear/compiler.h>
 
 #include <iostream>
+#include <cstring>
+#include <cstdlib> // for atoi
 
 #include <simgear/debug/logstream.hxx>
 
@@ -294,7 +296,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 +305,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+, ? );