Skip to contentSkip to conversation

kconfig: Fix checking return value of 'fwrite'

PATCH In review Reinhard Tartler 5 participants 10 messages Oct 6, 2011 – Jan 14, 2012

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

PATCH 1/1 kconfig: Fix checking return value of 'fwrite'

fwrite indicates '1' written member if a zero-length string is written.

---
 scripts/kconfig/lkc.h |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/kconfig/lkc.h +1 -1
index b633bdb..727d265 100644
@@ -90,7 +90,7 @@ struct conf_printer {
 /* confdata.c and expr.c */
 static inline void xfwrite(const void *str, size_t len, size_t count, FILE *out)
 {
-	if (fwrite(str, len, count, out) < count)
+	if (len > 0 && fwrite(str, len, count, out) < count)
 		fprintf(stderr, "\nError in writing or end of file.\n");
 }
 
Signature · 6 lines
1.7.0.4

-- 
Reinhard Tartler                     Department of Computer Science IV
Martensstr 1, 91058 Erlangen Germany, University of Erlangen-Nuremberg
            http://www4.informatik.uni-erlangen.de/~tartler
Acked-by on v1 1/1
#1 lore raw
Quoted 1 line · Reinhard Tartler

fwrite indicates '1' written member if a zero-length string is written.

For completeness, xfwrite() is called like this,

        xfwrite(str, strlen(str), 1, data);

so if strlen() returns 0, xfwrite() will print out the error message, which is wrong, no error in this case.

Thanks!

#2 lore raw
In reply to Reinhard Tartler

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

you forgot the "Signed-off-by: " part :)

- Arnaud

Quoted 29 lines

---
 scripts/kconfig/lkc.h |    2 +-

 1 files changed, 1 insertions(+), 1 deletions(-)
scripts/kconfig/lkc.h +1 -2
index b633bdb..727d265 100644
@@ -90,7 +90,7 @@ struct conf_printer {
  /* confdata.c and expr.c */
  static inline void xfwrite(const void *str, size_t len, size_t count, FILE *out)
  {
-       if (fwrite(str, len, count, out) < count)
+       if (len > 0 && fwrite(str, len, count, out) < count)
                fprintf(stderr, "\nError in writing or end of file.\n");
  }
 
--

1.7.0.4

--
Reinhard Tartler                     Department of Computer Science IV Martensstr 1, 91058 Erlangen Germany, University of Erlangen-Nuremberg
           http://www4.informatik.uni-erlangen.de/~tartler
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 5 lines · Arnaud Lacombe

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

you forgot the "Signed-off-by: " part :)

Reinhard, can I assume

Signed-off-by: Reinhard Tartler
<[email protected]>

? The patch is otherwise correct.

Michal

#4 lore raw
Quoted 5 lines · Michal Marek
Quoted 4 lines · Arnaud Lacombe

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

Quoted 8 lines
Quoted 1 line

you forgot the "Signed-off-by: " part :)

Reinhard, can I assume

Signed-off-by: Reinhard Tartler
<[email protected]>

? The patch is otherwise correct.

I merely forgot to add that part. I fully agree with the (legal) implications.

Cheers,
Reinhard,

Signature · 3 lines
Reinhard Tartler                     Department of Computer Science IV
Martensstr 1, 91058 Erlangen Germany, University of Erlangen-Nuremberg
            http://www4.informatik.uni-erlangen.de/~tartler
#5 lore raw
In reply to Michal Marek

From: Michal Marek <[email protected]>
Date: Sun, 20 Nov 2011 14:24:35 +0100

Quoted 6 lines · Arnaud Lacombe
Quoted 4 lines

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

Quoted 8 lines
Quoted 1 line

you forgot the "Signed-off-by: " part :)

Reinhard, can I assume

Signed-off-by: Reinhard Tartler
<[email protected]>

? The patch is otherwise correct.

I have two reasons to oppose this patch.

1. If 'len' value is zero, there is an issue already and it should be taken care of _before_ calling fwrite().

2. xfwrite() doesn't fix anything except for the compiler warning. It assumes this world is perfect and it's definitely not a place to take care of the zero-length string.

Signature · 1 line
Jean Sacren
#6 lore raw
Quoted 15 lines · Jean Sacren

From: Michal Marek <[email protected]>
Date: Sun, 20 Nov 2011 14:24:35 +0100

Quoted 13 lines · Arnaud Lacombe
Quoted 5 lines

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

you forgot the "Signed-off-by: " part :)

Reinhard, can I assume

Signed-off-by: Reinhard Tartler
<[email protected]>

Quoted 6 lines
Quoted 1 line

? The patch is otherwise correct.

I have two reasons to oppose this patch.

1. If 'len' value is zero, there is an issue already and it should be taken care of _before_ calling fwrite().

So you're saying the function assumes a non-empty string? Why? It is a simple "writing-helper" and this seems a perfectly valid corner case to me. Can you perhaps elaborate why you consider this corner case invalid and require (possibly out-of-tree) callers to check this for themselves?

Quoted 3 lines

2. xfwrite() doesn't fix anything except for the compiler warning. It assumes this world is perfect and it's definitely not a place to take care of the zero-length string.

It seems to me that you rather oppose to the introduction of xfwrite() than to the patch here at hand. Is that correct?

Cheers,
Reinhard

Signature · 3 lines
Reinhard Tartler                     Department of Computer Science IV
Martensstr 1, 91058 Erlangen Germany, University of Erlangen-Nuremberg
            http://www4.informatik.uni-erlangen.de/~tartler
#7 lore raw

Hi,

2011/11/23 Reinhard Tartler <[email protected]>:

Quoted 17 lines · Jean Sacren
Quoted 16 lines

From: Michal Marek <[email protected]>
Date: Sun, 20 Nov 2011 14:24:35 +0100

Quoted 13 lines · Arnaud Lacombe
Quoted 5 lines

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

you forgot the "Signed-off-by: " part :)

Reinhard, can I assume

Signed-off-by: Reinhard Tartler
<[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

? The patch is otherwise correct.

I have two reasons to oppose this patch.

1. If 'len' value is zero, there is an issue already and it should be taken care of _before_ calling fwrite().

So you're saying the function assumes a non-empty string? Why?

AFAIK, fwrite(3) is currently used:
1) in comment printers. Empty comment are not allowed.
2) in a callback passed to expr_print(), where the string printed is
non-empty[0]
2) in the lexer, auto-generated, and unused

So Jean's point is valid, but for this comment to be pedantic, I would not weakly test for len > 0', but enforce assumptions above with an assertion, by both converting the existing direct call to xfwrite() and add assert(len > 0)' before calling fwrite(3).

- Arnaud

[0]: symbol's name is either NULL or a non-empty string.

#8 lore raw

PATCH 1/1 kbuild: Fix compiler warning with assertion when calling 'fwrite'

From: Arnaud Lacombe <[email protected]>
Date: Wed, 23 Nov 2011 13:05:53 -0500

Quoted 35 lines

Hi,

2011/11/23 Reinhard Tartler <[email protected]>:

Quoted 17 lines · Jean Sacren
Quoted 16 lines

From: Michal Marek <[email protected]>
Date: Sun, 20 Nov 2011 14:24:35 +0100

Quoted 13 lines · Arnaud Lacombe
Quoted 5 lines

Hi,

2011/10/6 Reinhard Tartler <[email protected]>:

Quoted 1 line

fwrite indicates '1' written member if a zero-length string is written.

you forgot the "Signed-off-by: " part :)

Reinhard, can I assume

Signed-off-by: Reinhard Tartler
<[email protected]>

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

? The patch is otherwise correct.

I have two reasons to oppose this patch.

1. If 'len' value is zero, there is an issue already and it should be taken care of _before_ calling fwrite().

So you're saying the function assumes a non-empty string? Why?

AFAIK, fwrite(3) is currently used:
1) in comment printers. Empty comment are not allowed.
2) in a callback passed to expr_print(), where the string printed is
non-empty[0]
2) in the lexer, auto-generated, and unused

Quoted 4 lines

So Jean's point is valid, but for this comment to be pedantic, I would not weakly test for len > 0', but enforce assumptions above with an assertion, by both converting the existing direct call to xfwrite() and add assert(len > 0)' before calling fwrite(3).

I prepared a patch to take care of this corner case. Review is appreciated. Thanks.

Signature · 30 lines
Jean Sacren

-->8

[PATCH] kbuild: Fix compiler warning with assertion when calling 'fwrite'

Reinhard Tartler discovered a corner case of calling xfwrite() where the
length of the string is zero.

Arnaud Lacombe suggested to use assertion for the corner case, as
fwrite(3) is currently used:

 1) in comment printers. Empty comment are not allowed.
 2) in a callback passed to expr_print(), where the string printed is
    either NULL OR non-empty.
 3) in the lexer, auto-generated, and unused.

I feel using assertion is a good solution:

 1) It cleanly takes care of the above-mentioned corner case.
 2) It can be easily disabled by defining NDEBUG.
 3) It asserts xfwrite() is simply a wrapper for fwrite().

Reported-by: Reinhard Tartler <[email protected]>
Signed-off-by: Arnaud Lacombe <[email protected]>
Signed-off-by: Jean Sacren <[email protected]>
---
 scripts/kconfig/expr.h |    1 +
 scripts/kconfig/lkc.h  |    6 ++++--
 2 files changed, 5 insertions(+), 2 deletions(-)
scripts/kconfig/expr.h +1 -0
index 80fce57..d4ecce8 100644
@@ -10,6 +10,7 @@
 extern "C" {
 #endif
 
+#include <assert.h>
 #include <stdio.h>
 #ifndef __cplusplus
 #include <stdbool.h>
scripts/kconfig/lkc.h +4 -2
index b633bdb..c18f2bd 100644
@@ -90,8 +90,10 @@ struct conf_printer {
 /* confdata.c and expr.c */
 static inline void xfwrite(const void *str, size_t len, size_t count, FILE *out)
 {
-	if (fwrite(str, len, count, out) < count)
-		fprintf(stderr, "\nError in writing or end of file.\n");
+	assert(len != 0);
+
+	if (fwrite(str, len, count, out) != count)
+		fprintf(stderr, "Error in writing or end of file.\n");
 }
 
 /* menu.c */
#9 lore raw

Re: [PATCH] kbuild: Fix compiler warning with assertion when calling 'fwrite'

Quoted 13 lines · Jean Sacren

Reinhard Tartler discovered a corner case of calling xfwrite() where the length of the string is zero.

Arnaud Lacombe suggested to use assertion for the corner case, as fwrite(3) is currently used:

1) in comment printers. Empty comment are not allowed.
2) in a callback passed to expr_print(), where the string printed is either NULL OR non-empty.
3) in the lexer, auto-generated, and unused.

I feel using assertion is a good solution:

Quoted 7 lines

1) It cleanly takes care of the above-mentioned corner case.
2) It can be easily disabled by defining NDEBUG.
3) It asserts xfwrite() is simply a wrapper for fwrite().

Applied to kbuild.git#kconfig.

Michal