Skip to content

Commit 360acd0

Browse files
committed
Replace all instances of atoi with strtol
atoi doesn't check if the conversion from string to int succeeded which doesn't allow us to do proper error handling. We also now make sure that the port argument is a valid port in addition to being properly converted
1 parent 9b7279a commit 360acd0

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

other/DHT_bootstrap.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* A simple DHT boostrap node for tox.
1010
*/
11+
#include <stdint.h>
1112
#include <stdio.h>
1213
#include <stdlib.h>
1314
#include <string.h>
@@ -193,7 +194,16 @@ int main(int argc, char *argv[])
193194

194195
if (argc > argvoffset + 3) {
195196
printf("Trying to bootstrap into the network...\n");
196-
uint16_t port = net_htons(atoi(argv[argvoffset + 2]));
197+
198+
const long int port_conv = strtol(argv[argvoffset + 2], nullptr, 10);
199+
200+
if (port_conv <= 0 || port_conv > UINT16_MAX) {
201+
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
202+
exit(1);
203+
}
204+
205+
const uint16_t port = net_htons((uint16_t)port_conv);
206+
197207
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
198208
int res = dht_bootstrap_from_address(dht, argv[argvoffset + 1],
199209
ipv6enabled, port, bootstrap_key);

other/fun/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ cc_binary(
4848
srcs = ["strkey.c"],
4949
copts = ["-w"],
5050
deps = [
51+
"//c-toxcore/toxcore",
52+
"//c-toxcore/toxcore:ccompat",
5153
"@libsodium",
5254
],
5355
)

other/fun/strkey.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
#include <sodium.h>
4343

44+
#include "../../toxcore/ccompat.h"
45+
4446
#define PRINT_TRIES_COUNT
4547

4648
static void print_key(unsigned char *key)
@@ -60,12 +62,18 @@ int main(int argc, char *argv[])
6062
{
6163
unsigned char public_key[crypto_box_PUBLICKEYBYTES]; // null terminator
6264
unsigned char secret_key[crypto_box_SECRETKEYBYTES];
63-
int offset = 0;
65+
long int offset = 0;
6466
size_t len;
6567
unsigned char desired_bin[crypto_box_PUBLICKEYBYTES]; // null terminator
6668

6769
if (argc == 3) {
68-
offset = atoi(argv[1]);
70+
offset = strtol(argv[1], nullptr, 10);
71+
72+
if (offset <= 0) {
73+
fprintf(stderr, "strtol() failed to convert \"%s\" into an integer\n", argv[1]);
74+
exit(1);
75+
}
76+
6977
char *desired_hex = argv[2];
7078
len = strlen(desired_hex);
7179

testing/DHT_test.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#endif
1919

2020
#include <assert.h>
21+
#include <stdint.h>
2122
#include <stdlib.h>
2223
#include <stdio.h>
2324
#include <string.h>
@@ -189,7 +190,14 @@ int main(int argc, char *argv[])
189190

190191
perror("Initialization");
191192

192-
uint16_t port = net_htons(atoi(argv[argvoffset + 2]));
193+
const long int port_conv = strtol(argv[argvoffset + 2], nullptr, 10);
194+
195+
if (port_conv <= 0 || port_conv > UINT16_MAX) {
196+
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
197+
return 1;
198+
}
199+
200+
const uint16_t port = net_htons((uint16_t)port_conv);
193201
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
194202
int res = dht_bootstrap_from_address(dht, argv[argvoffset + 1], ipv6enabled, port, binary_string);
195203
free(binary_string);

testing/Messenger_test.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*
2424
* EX: ./test Save.bak
2525
*/
26+
#include <stdint.h>
2627
#include <stdio.h>
2728
#include <stdlib.h>
2829
#include <string.h>
@@ -107,7 +108,14 @@ int main(int argc, char *argv[])
107108
}
108109

109110
if (argc == argvoffset + 4) {
110-
uint16_t port = net_htons(atoi(argv[argvoffset + 2]));
111+
const long int port_conv = strtol(argv[argvoffset + 2], nullptr, 10);
112+
113+
if (port_conv <= 0 || port_conv > UINT16_MAX) {
114+
printf("Failed to convert \"%s\" into a valid port. Exiting...\n", argv[argvoffset + 2]);
115+
exit(1);
116+
}
117+
118+
const uint16_t port = net_htons((uint16_t)port_conv);
111119
uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]);
112120
int res = dht_bootstrap_from_address(m->dht, argv[argvoffset + 1],
113121
ipv6enabled, port, bootstrap_key);

0 commit comments

Comments
 (0)