[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Nano-devel] [PATCH 1/2] check stat's result and avoid calling stat
From: |
Benno Schulenberg |
Subject: |
Re: [Nano-devel] [PATCH 1/2] check stat's result and avoid calling stat on a NULL pointer |
Date: |
Sat, 06 Feb 2016 11:35:29 +0100 |
On Wed, Feb 3, 2016, at 12:32, Kamil Dudka wrote:
> diff --git a/trunk/nano/src/files.c b/trunk/nano/src/files.c
> index a7aa1c5..cbd81bf 100644
> --- a/trunk/nano/src/files.c
> +++ b/trunk/nano/src/files.c
> @@ -2214,8 +2226,10 @@ bool write_file(const char *name, FILE *f_open, bool
> tmp, append_type
> if (openfile->current_stat == NULL)
> openfile->current_stat =
> (struct stat *)nmalloc(sizeof(struct stat));
> - if (!openfile->mark_set)
> - stat(realname, openfile->current_stat);
> + if (!openfile->mark_set && 0 != stat(realname, openfile->current_stat))
> {
> + free(openfile->current_stat);
> + openfile->current_stat = NULL;
> + }
I don't get this part. Why (also in the original code) malloc the
stat struct when not actually going to stat()? Or asked another
way: why free the stat struct only when the mark is not set?
So I think this part should be:
- /* Update current_stat to reference the file as it is now. */
- if (openfile->current_stat == NULL)
- openfile->current_stat =
- (struct stat *)nmalloc(sizeof(struct stat));
- if (!openfile->mark_set)
- stat(realname, openfile->current_stat);
+ if (!openfile->mark_set) {
+ if (openfile->current_stat == NULL)
+ openfile->current_stat =
+ (struct stat *)nmalloc(sizeof(struct stat));
+
+ /* Update the stat info to reflect the file as it is now. */
+ if (stat(realname, openfile->current_stat) != 0) {
+ free(openfile->current_stat);
+ openfile->current_stat = NULL;
+ }
+ }
But still... as the comment says, it assumes that there is
already stat info there, so the malloc should be superfluous
in this case. But the whole routine is so convoluted that I
don't dare remove it.
Benno
--
http://www.fastmail.com - A fast, anti-spam email service.
ensure-stat-info-is-valid.patch
Description: Text Data
- [Nano-devel] [PATCH 1/2] check stat's result and avoid calling stat on a NULL pointer, Kamil Dudka, 2016/02/03
- [Nano-devel] [PATCH 2/2] use futimens() if available, instead of utime(), Kamil Dudka, 2016/02/03
- Re: [Nano-devel] [PATCH 2/2] use futimens() if available, instead of utime(), Mike Frysinger, 2016/02/03
- Re: [Nano-devel] [PATCH 2/2] use futimens() if available, instead of utime(), Kamil Dudka, 2016/02/03
- [Nano-devel] begin using gnulib?, Benno Schulenberg, 2016/02/04
- Re: [Nano-devel] begin using gnulib?, Mike Frysinger, 2016/02/04
- Re: [Nano-devel] begin using gnulib?, Chris Allegretta, 2016/02/05
- Re: [Nano-devel] begin using gnulib?, Kamil Dudka, 2016/02/05
Re: [Nano-devel] [PATCH 1/2] check stat's result and avoid calling stat on a NULL pointer,
Benno Schulenberg <=