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.
This commit is contained in:
Scott Giese
2020-12-17 18:01:49 -06:00
parent e031ea7f7c
commit e8f715c255
5 changed files with 107 additions and 122 deletions

View File

@@ -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"

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;