Skip to contentSkip to conversation

kconfig: fix missing fclose() on error paths

PATCH In review Masahiro Yamada 2 participants 4 messages Feb 8, 2022 – Feb 12, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kconfig: fix missing fclose() on error paths

The file is not closed when ferror() fails.

Fixes 00d674cb3536 ("kconfig: refactor conf_write_dep()")
Fixes 57ddd07c4560 ("kconfig: refactor conf_write_autoconf()")

---

 scripts/kconfig/confdata.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/kconfig/confdata.c +6 -6
index 59717be31210..16897cb8cefd 100644
@@ -979,10 +979,10 @@ static int conf_write_autoconf_cmd(const char *autoconf_name)
 
 	fprintf(out, "\n$(deps_config): ;\n");
 
-	if (ferror(out)) /* error check for all fprintf() calls */
-		return -1;
-
+	ret = ferror(out); /* error check for all fprintf() calls */
 	fclose(out);
+	if (ret)
+		return -1;
 
 	if (rename(tmp, name)) {
 		perror("rename");
@@ -1093,10 +1093,10 @@ static int __conf_write_autoconf(const char *filename,
 			print_symbol(file, sym);
 
 	/* check possible errors in conf_write_heading() and print_symbol() */
-	if (ferror(file))
-		return -1;
-
+	ret = ferror(file);
 	fclose(file);
+	if (ret)
+		return -1;
 
 	if (rename(tmp, filename)) {
 		perror("rename");
Signature · 1 line
2.32.0
#1 lore raw
Quoted 8 lines · Masahiro Yamada

The file is not closed when ferror() fails.

Fixes 00d674cb3536 ("kconfig: refactor conf_write_dep()")
Fixes 57ddd07c4560 ("kconfig: refactor conf_write_autoconf()")

---

Applied to linux-kbuild/fixes.

Quoted 39 lines
 scripts/kconfig/confdata.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/kconfig/confdata.c +6 -7
index 59717be31210..16897cb8cefd 100644
@@ -979,10 +979,10 @@ static int conf_write_autoconf_cmd(const char *autoconf_name)
 
        fprintf(out, "\n$(deps_config): ;\n");
 
-       if (ferror(out)) /* error check for all fprintf() calls */
-               return -1;
-
+       ret = ferror(out); /* error check for all fprintf() calls */
        fclose(out);
+       if (ret)
+               return -1;
 
        if (rename(tmp, name)) {
                perror("rename");
@@ -1093,10 +1093,10 @@ static int __conf_write_autoconf(const char *filename,
                        print_symbol(file, sym);
 
        /* check possible errors in conf_write_heading() and print_symbol() */
-       if (ferror(file))
-               return -1;
-
+       ret = ferror(file);
        fclose(file);
+       if (ret)
+               return -1;
 
        if (rename(tmp, filename)) {
                perror("rename");
--

2.32.0

Signature · 2 lines
Best Regards
Masahiro Yamada
#2 lore raw

From: Masahiro Yamada

Quoted 26 lines

Sent: 10 February 2022 00:54

Quoted 8 lines

The file is not closed when ferror() fails.

Fixes 00d674cb3536 ("kconfig: refactor conf_write_dep()")
Fixes 57ddd07c4560 ("kconfig: refactor conf_write_autoconf()")

---

Applied to linux-kbuild/fixes.

Quoted 12 lines
 scripts/kconfig/confdata.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/kconfig/confdata.c +0 -0
index 59717be31210..16897cb8cefd 100644
@@ -979,10 +979,10 @@ static int conf_write_autoconf_cmd(const char *autoconf_name)
 
        fprintf(out, "\n$(deps_config): ;\n");
 
Quoted 7 lines

scripts/kconfig/confdata.c static int conf_write_autoconf_cmd(const char *autoconf_name) · line 985

Quoted 7 lines
scripts/kconfig/confdata.c +3 -3
-       if (ferror(out)) /* error check for all fprintf() calls */
-               return -1;
-
+       ret = ferror(out); /* error check for all fprintf() calls */
        fclose(out);
+       if (ret)
+               return -1;

There's not much point calling ferror() unless you call fflush() first.

Similarly there's about zero point in checking the return from fprintf(). At least I've never found any distro adding 'must_check' to fprintf(). ('must_not_check' would be more appropriate!)

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#3 lore raw
Quoted 38 lines · David Laight

From: Masahiro Yamada

Quoted 26 lines

Sent: 10 February 2022 00:54

Quoted 8 lines

The file is not closed when ferror() fails.

Fixes 00d674cb3536 ("kconfig: refactor conf_write_dep()")
Fixes 57ddd07c4560 ("kconfig: refactor conf_write_autoconf()")

---

Applied to linux-kbuild/fixes.

Quoted 12 lines
 scripts/kconfig/confdata.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
scripts/kconfig/confdata.c +0 -0
index 59717be31210..16897cb8cefd 100644
@@ -979,10 +979,10 @@ static int conf_write_autoconf_cmd(const char *autoconf_name)
 
        fprintf(out, "\n$(deps_config): ;\n");
 
Quoted 7 lines

scripts/kconfig/confdata.c static int conf_write_autoconf_cmd(const char *autoconf_name) · line 985

Quoted 7 lines
scripts/kconfig/confdata.c +3 -3
-       if (ferror(out)) /* error check for all fprintf() calls */
-               return -1;
-
+       ret = ferror(out); /* error check for all fprintf() calls */
        fclose(out);
+       if (ret)
+               return -1;

There's not much point calling ferror() unless you call fflush() first.

Quoted 3 lines

Similarly there's about zero point in checking the return from fprintf(). At least I've never found any distro adding 'must_check' to fprintf(). ('must_not_check' would be more appropriate!)

Thanks for pointing it out. You are right.

I assume you are not opposed to this patch itself.

I will insert fflush() before ferror()
as a separate patch.

Quoted 5 lines

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

Signature · 2 lines
Best Regards
Masahiro Yamada