Skip to content

Commit 37ae2d5

Browse files
lacombarmichal42
authored andcommitted
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 <Reinhard.Tartler@informatik.uni-erlangen.de> Signed-off-by: Arnaud Lacombe <lacombar@gmail.com> Signed-off-by: Jean Sacren <sakiwit@gmail.com> Signed-off-by: Michal Marek <mmarek@suse.cz>
1 parent e2aef4d commit 37ae2d5

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

‎scripts/kconfig/expr.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
extern "C" {
1111
#endif
1212

13+
#include <assert.h>
1314
#include <stdio.h>
1415
#ifndef __cplusplus
1516
#include <stdbool.h>

‎scripts/kconfig/lkc.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ struct conf_printer {
9090
/* confdata.c and expr.c */
9191
static inline void xfwrite(const void *str, size_t len, size_t count, FILE *out)
9292
{
93-
if (fwrite(str, len, count, out) < count)
94-
fprintf(stderr, "\nError in writing or end of file.\n");
93+
assert(len != 0);
94+
95+
if (fwrite(str, len, count, out) != count)
96+
fprintf(stderr, "Error in writing or end of file.\n");
9597
}
9698

9799
/* menu.c */

0 commit comments

Comments
 (0)