[PATCH] kexec/kexec.c: Add missing fclose()/close() call

Eric W. Biederman ebiederm at xmission.com
Tue Aug 25 08:04:57 EDT 2020


Youling Tang <tangyouling at loongson.cn> writes:

> Add missing fclose()/close() call.

The program exits immediately and that exit closes all of the files.
What is the point of adding an explicit close?

>
> Signed-off-by: Youling Tang <tangyouling at loongson.cn>
> ---
>  kexec/kexec.c | 28 ++++++++++++++++++++++------
>  1 file changed, 22 insertions(+), 6 deletions(-)
>
> diff --git a/kexec/kexec.c b/kexec/kexec.c
> index a62b362..f970b13 100644
> --- a/kexec/kexec.c
> +++ b/kexec/kexec.c
> @@ -542,6 +542,7 @@ static char *slurp_file_generic(const char *filename, off_t *r_size,
>  	}
>  	result = fstat(fd, &stats);
>  	if (result < 0) {
> +		close(fd);
>  		die("Cannot stat: %s: %s\n",
>  			filename, strerror(errno));
>  	}
> @@ -553,20 +554,26 @@ static char *slurp_file_generic(const char *filename, off_t *r_size,
>  	if (S_ISCHR(stats.st_mode)) {
>  
>  		size = lseek(fd, 0, SEEK_END);
> -		if (size < 0)
> +		if (size < 0) {
> +			close(fd);
>  			die("Can not seek file %s: %s\n", filename,
>  					strerror(errno));
> +		}
>  
>  		err = lseek(fd, 0, SEEK_SET);
> -		if (err < 0)
> +		if (err < 0) {
> +			close(fd);
>  			die("Can not seek to the begin of file %s: %s\n",
>  					filename, strerror(errno));
> +		}
>  		buf = slurp_fd(fd, filename, size, &nread);
>  	} else if (S_ISBLK(stats.st_mode)) {
>  		err = ioctl(fd, BLKGETSIZE64, &size);
> -		if (err < 0)
> +		if (err < 0) {
> +			close(fd);
>  			die("Can't retrieve size of block device %s: %s\n",
>  				filename, strerror(errno));
> +		}
>  		buf = slurp_fd(fd, filename, size, &nread);
>  	} else {
>  		size = stats.st_size;
> @@ -578,13 +585,18 @@ static char *slurp_file_generic(const char *filename, off_t *r_size,
>  			buf = slurp_fd(fd, filename, size, &nread);
>  		}
>  	}
> -	if ((use_mmap && (buf == MAP_FAILED)) || (!use_mmap && (buf == NULL)))
> +	if ((use_mmap && (buf == MAP_FAILED)) || (!use_mmap && (buf == NULL))) {
> +		close(fd);
>  		die("Cannot read %s", filename);
> +	}
>  
> -	if (nread != size)
> +	if (nread != size) {
> +		close(fd);
>  		die("Read on %s ended before stat said it should\n", filename);
> +	}
>  
>  	*r_size = size;
> +	close(fd);
>  	return buf;
>  }
>  
> @@ -1131,8 +1143,10 @@ char *get_command_line(void)
>  	if (!fp)
>  		die("Could not open /proc/cmdline.");
>  
> -	if (fgets(line, sizeof_line, fp) == NULL)
> +	if (fgets(line, sizeof_line, fp) == NULL) {
> +		fclose(fp);
>  		die("Can't read /proc/cmdline.");
> +	}
>  
>  	fclose(fp);
>  
> @@ -1257,12 +1271,14 @@ static int do_kexec_file_load(int fileind, int argc, char **argv,
>  	if (i == file_types) {
>  		fprintf(stderr, "Cannot determine the file type " "of %s\n",
>  				kernel);
> +		close(kernel_fd);
>  		return EFAILED;
>  	}
>  
>  	ret = file_type[i].load(argc, argv, kernel_buf, kernel_size, &info);
>  	if (ret < 0) {
>  		fprintf(stderr, "Cannot load %s\n", kernel);
> +		close(kernel_fd);
>  		return ret;
>  	}



More information about the kexec mailing list