Fix resource leaks identified by cppcheck

* Where possible rearrange error checks to avoid branching
* Otherwise add missing `fclose`, `close`, `px4_close` calls before return
This commit is contained in:
Peter Duerr
2017-08-07 22:05:17 +02:00
committed by Lorenz Meier
parent 0a61e9b279
commit 5be23060e7
6 changed files with 14 additions and 6 deletions
@@ -851,6 +851,7 @@ tone_alarm_main(int argc, char *argv[])
if (buffer == nullptr) { if (buffer == nullptr) {
PX4_WARN("not enough memory memory"); PX4_WARN("not enough memory memory");
fclose(fd);
return 1; return 1;
} }
+6 -5
View File
@@ -118,6 +118,11 @@ sd_bench_main(int argc, char *argv[])
} }
} }
if (block_size <= 0 || num_runs <= 0) {
PX4_ERR("invalid argument");
return -1;
}
int bench_fd = open(BENCHMARK_FILE, O_CREAT | O_WRONLY | O_TRUNC, PX4_O_MODE_666); int bench_fd = open(BENCHMARK_FILE, O_CREAT | O_WRONLY | O_TRUNC, PX4_O_MODE_666);
if (bench_fd < 0) { if (bench_fd < 0) {
@@ -125,16 +130,12 @@ sd_bench_main(int argc, char *argv[])
return -1; return -1;
} }
if (block_size <= 0 || num_runs <= 0) {
PX4_ERR("invalid argument");
return -1;
}
//create some data block //create some data block
uint8_t *block = (uint8_t *)malloc(block_size); uint8_t *block = (uint8_t *)malloc(block_size);
if (!block) { if (!block) {
PX4_ERR("Failed to allocate memory block"); PX4_ERR("Failed to allocate memory block");
close(bench_fd);
return -1; return -1;
} }
+2
View File
@@ -133,12 +133,14 @@ static void test_corruption(const char *filename, uint32_t write_chunk, uint32_t
if (read(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) { if (read(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) {
printf("read failed at offset %u\n", ofs); printf("read failed at offset %u\n", ofs);
close(fd);
return; return;
} }
for (uint16_t j = 0; j < write_chunk; j++) { for (uint16_t j = 0; j < write_chunk; j++) {
if (buffer[j] != get_value(ofs)) { if (buffer[j] != get_value(ofs)) {
printf("corruption at ofs=%u got %u\n", ofs, buffer[j]); printf("corruption at ofs=%u got %u\n", ofs, buffer[j]);
close(fd);
return; return;
} }
+1
View File
@@ -158,6 +158,7 @@ test_mount(int argc, char *argv[])
} }
if (it_left_abort == 0) { if (it_left_abort == 0) {
px4_close(cmd_fd);
(void)unlink(cmd_filename); (void)unlink(cmd_filename);
return 0; return 0;
} }
+2
View File
@@ -79,6 +79,8 @@ int test_ppm_loopback(int argc, char *argv[])
if (result != OK) { if (result != OK) {
warnx("PWM_SERVO_GET_COUNT"); warnx("PWM_SERVO_GET_COUNT");
(void)close(servo_fd);
return ERROR; return ERROR;
} }
+2 -1
View File
@@ -68,13 +68,14 @@ int test_uart_loopback(int argc, char *argv[])
int stdout_fd = 1; int stdout_fd = 1;
int uart2 = open("/dev/ttyS1", O_RDWR | O_NONBLOCK | O_NOCTTY); int uart2 = open("/dev/ttyS1", O_RDWR | O_NONBLOCK | O_NOCTTY);
int uart5 = open("/dev/ttyS2", O_RDWR | O_NONBLOCK | O_NOCTTY);
if (uart2 < 0) { if (uart2 < 0) {
printf("ERROR opening UART2, aborting..\n"); printf("ERROR opening UART2, aborting..\n");
return uart2; return uart2;
} }
int uart5 = open("/dev/ttyS2", O_RDWR | O_NONBLOCK | O_NOCTTY);
if (uart5 < 0) { if (uart5 < 0) {
if (uart2 >= 0) { if (uart2 >= 0) {
close(uart2); close(uart2);