Fix errors found by cppcheck

[disk.c:206]: (error) Resource leak: f
[modules.c:62]: (error) Memory leak: buffer
[modules.c:68]: (error) Common realloc mistake: 'buffer' nulled but not freed upon failure
[stage1.c:101]: (error) va_list 'args' was opened but not closed by va_end().
[url.c:297]: (error) Resource leak: dataSocket
This commit is contained in:
Gleb Fotengauer-Malinovskiy 2017-05-11 17:46:17 +03:00
parent e88e097f15
commit c96ab958b2
4 changed files with 17 additions and 3 deletions

2
disk.c
View File

@ -202,6 +202,8 @@ static enum return_type try_with_device(char *dev_name)
if (!(f = fopen("/proc/partitions", "rb")) || !fgets(buf, sizeof(buf), f) || !fgets(buf, sizeof(buf), f)) {
log_perror(dev_name);
if (f)
fclose(f);
stg1_error_message("Could not read partitions information.");
return RETURN_ERROR;
}

View File

@ -55,17 +55,27 @@ static void *grab_file(const char *filename, unsigned long *size)
{
unsigned int max = 16384;
int ret, fd;
void *buffer = malloc(max);
void *buffer;
fd = open(filename, O_RDONLY, 0);
if (fd < 0)
return NULL;
buffer = malloc(max);
if (!buffer)
return NULL;
*size = 0;
while ((ret = read(fd, buffer + *size, max - *size)) > 0) {
*size += ret;
if (*size == max)
buffer = realloc(buffer, max *= 2);
if (*size == max) {
void *nbuffer = realloc(buffer, max *= 2);
if (!nbuffer) {
free(buffer);
return NULL;
}
buffer = nbuffer;
}
}
if (ret < 0) {
free(buffer);

View File

@ -98,6 +98,7 @@ void stg1_info_message(char *msg, ...)
va_start(args, msg);
if (IS_AUTOMATIC) {
vlog_message(msg, args);
va_end(args);
return;
}
vinfo_message(msg, args);

1
url.c
View File

@ -294,6 +294,7 @@ int ftp_data_command(int sock, char * command, char * param)
i = strlen(retrCommand);
if (write(sock, retrCommand, i) != i) {
close(dataSocket);
return FTPERR_SERVER_IO_ERROR;
}