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 15:31:27 +02:00 committed by Lorenz Meier
parent 0a61e9b279
commit 5be23060e7
6 changed files with 14 additions and 6 deletions

View File

@ -851,6 +851,7 @@ tone_alarm_main(int argc, char *argv[])
if (buffer == nullptr) {
PX4_WARN("not enough memory memory");
fclose(fd);
return 1;
}

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);
if (bench_fd < 0) {
@ -125,16 +130,12 @@ sd_bench_main(int argc, char *argv[])
return -1;
}
if (block_size <= 0 || num_runs <= 0) {
PX4_ERR("invalid argument");
return -1;
}
//create some data block
uint8_t *block = (uint8_t *)malloc(block_size);
if (!block) {
PX4_ERR("Failed to allocate memory block");
close(bench_fd);
return -1;
}

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)) {
printf("read failed at offset %u\n", ofs);
close(fd);
return;
}
for (uint16_t j = 0; j < write_chunk; j++) {
if (buffer[j] != get_value(ofs)) {
printf("corruption at ofs=%u got %u\n", ofs, buffer[j]);
close(fd);
return;
}

View File

@ -158,6 +158,7 @@ test_mount(int argc, char *argv[])
}
if (it_left_abort == 0) {
px4_close(cmd_fd);
(void)unlink(cmd_filename);
return 0;
}

View File

@ -79,6 +79,8 @@ int test_ppm_loopback(int argc, char *argv[])
if (result != OK) {
warnx("PWM_SERVO_GET_COUNT");
(void)close(servo_fd);
return ERROR;
}

View File

@ -68,13 +68,14 @@ int test_uart_loopback(int argc, char *argv[])
int stdout_fd = 1;
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) {
printf("ERROR opening UART2, aborting..\n");
return uart2;
}
int uart5 = open("/dev/ttyS2", O_RDWR | O_NONBLOCK | O_NOCTTY);
if (uart5 < 0) {
if (uart2 >= 0) {
close(uart2);