From e8f715c25541d2263a7def86ac5dd6c6c770c753 Mon Sep 17 00:00:00 2001 From: Scott Giese Date: Thu, 17 Dec 2020 18:01:49 -0600 Subject: [PATCH] Maintenance: convert atoi to modern equivalence Out of range results in undefined behavior in the C library. Coverting to C++ library to avoid this risk. --- simgear/debug/logdelta.cxx | 4 +- simgear/io/sg_serial.cxx | 12 ++-- simgear/io/sg_socket.cxx | 131 ++++++++++++++++------------------- simgear/io/sg_socket_udp.cxx | 69 +++++++++--------- simgear/timing/timezone.cxx | 13 ++-- 5 files changed, 107 insertions(+), 122 deletions(-) diff --git a/simgear/debug/logdelta.cxx b/simgear/debug/logdelta.cxx index ebe470d1..a1ae6da4 100644 --- a/simgear/debug/logdelta.cxx +++ b/simgear/debug/logdelta.cxx @@ -88,7 +88,7 @@ struct logDelta << "'\n"; continue; } - int delta2 = atoi(delta.c_str()); + int delta2 = std::stoi(delta); std::string file = next_subitem(ffl, ":"); std::string function = next_subitem(ffl, ":"); @@ -110,7 +110,7 @@ struct logDelta << "'\n"; continue; } - line2 = atoi(line.c_str()); + line2 = std::stoi(line); } if (1) std::cerr << __FILE__ << ":" << __LINE__ << ": " << "adding" diff --git a/simgear/io/sg_serial.cxx b/simgear/io/sg_serial.cxx index 2352114f..00027598 100644 --- a/simgear/io/sg_serial.cxx +++ b/simgear/io/sg_serial.cxx @@ -53,16 +53,16 @@ SGSerial::~SGSerial() { bool SGSerial::open( const SGProtocolDir d ) { set_dir( d ); - if ( ! port.open_port( device ) ) { - SG_LOG( SG_IO, SG_ALERT, "Error opening device: " << device ); - return false; + if (!port.open_port(device)) { + SG_LOG(SG_IO, SG_ALERT, "Error opening device: " << device); + return false; } // cout << "fd = " << port.fd << endl; - if ( ! port.set_baud( std::atoi( baud.c_str() ) ) ) { - SG_LOG( SG_IO, SG_ALERT, "Error setting baud: " << baud ); - return false; + if (!port.set_baud(std::stoi(baud.c_str()))) { + SG_LOG(SG_IO, SG_ALERT, "Error setting baud: " << baud); + return false; } return true; diff --git a/simgear/io/sg_socket.cxx b/simgear/io/sg_socket.cxx index 4ca13150..c18bab8d 100644 --- a/simgear/io/sg_socket.cxx +++ b/simgear/io/sg_socket.cxx @@ -124,85 +124,70 @@ SGSocket::make_client_socket() bool SGSocket::open( SGProtocolDir direction ) { - set_dir( direction ); + set_dir(direction); - is_server = is_tcp && - (direction == SG_IO_IN || direction == SG_IO_BI); + is_server = is_tcp && (direction == SG_IO_IN || direction == SG_IO_BI); - if ( port_str == "" || port_str == "any" ) { - port = 0; + if (port_str == "" || port_str == "any") { + port = 0; + } else { + port = std::stoul(port_str); + } + + if (direction == SG_IO_IN) { + // this means server for now + + // Setup socket to listen on. Set "port" before making this + // call. A port of "0" indicates that we want to let the os + // pick any available port. + if (!make_server_socket()) { + SG_LOG(SG_IO, SG_ALERT, "SG_IO_IN socket creation failed"); + return false; + } + + if (!is_tcp) { + // Non-blocking UDP + nonblock(); } else { - port = atoi( port_str.c_str() ); - } - - if (direction == SG_IO_IN) - { - // this means server for now - - // Setup socket to listen on. Set "port" before making this - // call. A port of "0" indicates that we want to let the os - // pick any available port. - if (!make_server_socket()) - { - SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" ); - return false; - } - - if ( !is_tcp ) - { - // Non-blocking UDP - nonblock(); - } - else - { - // Blocking TCP - // Specify the maximum length of the connection queue - sock.listen( SG_MAX_SOCKET_QUEUE ); - } - - } - else if (direction == SG_IO_OUT) - { - // this means client for now - - if (!make_client_socket()) - { - SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" ); - return false; - } - - if ( !is_tcp ) - { - // Non-blocking UDP - nonblock(); - } - } - else if (direction == SG_IO_BI && is_tcp) - { - // this means server for TCP sockets - - // Setup socket to listen on. Set "port" before making this - // call. A port of "0" indicates that we want to let the os - // pick any available port. - if (!make_server_socket()) - { - SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" ); - return false; - } - // Blocking TCP - // Specify the maximum length of the connection queue - sock.listen( SG_MAX_SOCKET_QUEUE ); - } - else - { - SG_LOG( SG_IO, SG_ALERT, - "Error: bidirection mode not available for UDP sockets." ); - return false; + // Blocking TCP + // Specify the maximum length of the connection queue + sock.listen(SG_MAX_SOCKET_QUEUE); } - first_read = false; + } else if (direction == SG_IO_OUT) { + // this means client for now - return true; + if (!make_client_socket()) { + SG_LOG(SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed"); + return false; + } + + if (!is_tcp) { + // Non-blocking UDP + nonblock(); + } + } else if (direction == SG_IO_BI && is_tcp) { + // this means server for TCP sockets + + // Setup socket to listen on. Set "port" before making this + // call. A port of "0" indicates that we want to let the os + // pick any available port. + if (!make_server_socket()) { + SG_LOG(SG_IO, SG_ALERT, "SG_IO_BI socket creation failed"); + return false; + } + // Blocking TCP + // Specify the maximum length of the connection queue + sock.listen(SG_MAX_SOCKET_QUEUE); + } else { + SG_LOG(SG_IO, SG_ALERT, + "Error: bidirection mode not available for UDP sockets."); + return false; + } + + first_read = false; + + return true; } diff --git a/simgear/io/sg_socket_udp.cxx b/simgear/io/sg_socket_udp.cxx index 6e43eb96..f19faf89 100644 --- a/simgear/io/sg_socket_udp.cxx +++ b/simgear/io/sg_socket_udp.cxx @@ -53,47 +53,46 @@ SGSocketUDP::~SGSocketUDP() { // listening socket. If specified as a client (out direction), open a // connection to a server. bool SGSocketUDP::open( const SGProtocolDir d ) { - set_dir( d ); + set_dir(d); - if ( ! sock.open( false ) ) { // open a UDP socket - SG_LOG( SG_IO, SG_ALERT, "error opening socket" ); - return false; + if (!sock.open(false)) { // open a UDP socket + SG_LOG(SG_IO, SG_ALERT, "error opening socket"); + return false; + } + + if (port_str == "" || port_str == "any") { + port = 0; + } else { + port = std::stoul(port_str.c_str()); + } + + // client_connections.clear(); + + if (get_dir() == SG_IO_IN) { + // this means server + + // bind ... + if (sock.bind(hostname.c_str(), port) == -1) { + SG_LOG(SG_IO, SG_ALERT, "error binding to port" << port_str); + return false; } + } else if (get_dir() == SG_IO_OUT) { + // this means client - if ( port_str == "" || port_str == "any" ) { - port = 0; - } else { - port = atoi( port_str.c_str() ); + // connect ... + if (sock.connect(hostname.c_str(), port) == -1) { + SG_LOG(SG_IO, SG_ALERT, "error connecting to " << hostname << port_str); + return false; } - - // client_connections.clear(); + } else { + SG_LOG(SG_IO, SG_ALERT, + "Error: bidirection mode not available for UDP sockets."); + return false; + } - if ( get_dir() == SG_IO_IN ) { - // this means server + set_valid(true); - // bind ... - if ( sock.bind( hostname.c_str(), port ) == -1 ) { - SG_LOG( SG_IO, SG_ALERT, "error binding to port" << port_str ); - return false; - } - } else if ( get_dir() == SG_IO_OUT ) { - // this means client - - // connect ... - if ( sock.connect( hostname.c_str(), port ) == -1 ) { - SG_LOG( SG_IO, SG_ALERT, - "error connecting to " << hostname << port_str ); - return false; - } - } else { - SG_LOG( SG_IO, SG_ALERT, - "Error: bidirection mode not available for UDP sockets." ); - return false; - } - - set_valid( true ); - - return true; + return true; } diff --git a/simgear/timing/timezone.cxx b/simgear/timing/timezone.cxx index b1fc6344..5608ee0e 100755 --- a/simgear/timing/timezone.cxx +++ b/simgear/timing/timezone.cxx @@ -69,19 +69,20 @@ SGTimeZone::SGTimeZone(const char *infoString) strncpy(latlon, (&infoString[start]), size); latlon[size] = 0; char sign; + char* double_end; sign = latlon[0]; strncpy(buffer, &latlon[1], 2); buffer[2] = 0; - lat = atof(buffer); + lat = std::strtod(buffer, &double_end); strncpy(buffer, &latlon[3], 2); buffer[2] = 0; - lat += (atof(buffer) / 60); + lat += (std::strtod(buffer, &double_end) / 60); int nextPos; if (strlen(latlon) > 12) { nextPos = 7; strncpy(buffer, &latlon[5], 2); buffer[2] = 0; - lat += (atof(buffer) / 3600.0); + lat += (std::strtod(buffer, &double_end) / 3600.0); } else { nextPos = 5; } @@ -93,17 +94,17 @@ SGTimeZone::SGTimeZone(const char *infoString) nextPos++; strncpy(buffer, &latlon[nextPos], 3); buffer[3] = 0; - lon = atof(buffer); + lon = std::strtod(buffer, &double_end); nextPos += 3; strncpy(buffer, &latlon[nextPos], 2); buffer[2] = 0; - lon += (atof(buffer) / 60); + lon += (std::strtod(buffer, &double_end) / 60); if (strlen(latlon) > 12) { nextPos += 2; strncpy(buffer, &latlon[nextPos], 2); buffer[2] = 0; - lon += (atof (buffer) / 3600.00); + lon += (std::strtod(buffer, &double_end) / 3600.00); } if (sign == '-') { lon = -lon;