Skip to content

Commit

Permalink
Fix bug where file descriptor 0 (aka stdin) was accidentally closed (#63
Browse files Browse the repository at this point in the history
)
  • Loading branch information
graebm authored Dec 31, 2021
1 parent 7b64917 commit f850f5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 6 additions & 4 deletions source/linux/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ int get_network_config_and_transfer(struct aws_iotdevice_network_ifconfig *ifcon
return AWS_OP_ERR;
}
int result = AWS_OP_ERR;
int fd = 0;
int fd = -1;
struct ifaddrs *address_info = NULL;
if (getifaddrs(&address_info)) {
AWS_LOGF_ERROR(
Expand Down Expand Up @@ -408,8 +408,10 @@ int get_network_config_and_transfer(struct aws_iotdevice_network_ifconfig *ifcon
goto cleanup;
}
next_interface:
close(fd);
fd = 0;
if (fd != -1) {
close(fd);
fd = -1;
}
address = address->ifa_next;
} /* while */
result = AWS_OP_SUCCESS;
Expand All @@ -421,7 +423,7 @@ int get_network_config_and_transfer(struct aws_iotdevice_network_ifconfig *ifcon
if (address_info) {
freeifaddrs(address_info);
}
if (fd) {
if (fd != -1) {
close(fd);
}
return result;
Expand Down
13 changes: 13 additions & 0 deletions tests/metrics_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#include <aws/mqtt/private/mqtt_client_test_helper.h>
#include <aws/testing/aws_test_harness.h>

#ifdef AWS_OS_LINUX
# include <errno.h>
# include <fcntl.h>
#endif /* AWS_OS_LINUX */

const char *TM_NUMBER = "TestMetricNumber";
const char *TM_NUMBER_LIST = "TestMetricNumberList";
const char *TM_STRING_LIST = "TestMetricStringList";
Expand Down Expand Up @@ -338,6 +343,14 @@ static int s_devicedefender_get_network_connections(struct aws_allocator *alloca
AWS_ZERO_STRUCT(ifconfig);
ASSERT_SUCCESS(get_network_config_and_transfer(&ifconfig, allocator));

#ifdef AWS_OS_LINUX
/* Regression test: Check that get_network_config_and_transfer didn't
* accidentally close file descriptor 0 (aka stdin) */
errno = 0;
bool file_descriptor_0_is_closed = (fcntl(0, F_GETFD) == -1) && (errno != 0);
ASSERT_FALSE(file_descriptor_0_is_closed);
#endif /* AWS_OS_LINUX */

struct aws_array_list net_conns;
AWS_ZERO_STRUCT(net_conns);
aws_array_list_init_dynamic(&net_conns, allocator, 5, sizeof(struct aws_iotdevice_metric_net_connection));
Expand Down

0 comments on commit f850f5b

Please sign in to comment.