Skip to content

Commit 6081052

Browse files
committed
add support for -Wmemcpy-max-count
sparse will warn if memcpy() (or memset(), copy_from_user(), copy_to_user()) is called with a very large static byte-count. But this warning is given unconditionaly while there are projects where this warning may not be not desired. Change this by making this warning conditional on a new warning flag: -W[no-]memcpy-max-count Signed-off-by: Luc Van Oostenryck <[email protected]>
1 parent d33fdf2 commit 6081052

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

cgcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ exit 0;
101101

102102
sub check_only_option {
103103
my ($arg) = @_;
104-
return 1 if $arg =~ /^-W(no-?)?(address-space|bitwise|cast-to-as|cast-truncate|context|decl|default-bitfield-sign|designated-init|do-while|enum-mismatch|init-cstring|non-pointer-null|old-initializer|one-bit-signed-bitfield|override-init-all|paren-string|ptr-subtraction-blows|return-void|sizeof-bool|sparse-all|sparse-error|transparent-union|typesign|undef|unknown-attribute)$/;
104+
return 1 if $arg =~ /^-W(no-?)?(address-space|bitwise|cast-to-as|cast-truncate|context|decl|default-bitfield-sign|designated-init|do-while|enum-mismatch|init-cstring|memcpy-max-count|non-pointer-null|old-initializer|one-bit-signed-bitfield|override-init-all|paren-string|ptr-subtraction-blows|return-void|sizeof-bool|sparse-all|sparse-error|transparent-union|typesign|undef|unknown-attribute)$/;
105105
return 1 if $arg =~ /^-v(no-?)?(entry|dead)$/;
106106
return 1 if $arg =~ /^-f(dump-linearize)(=\S*)?$/;
107107
return 0;

lib.c

+2
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ int Wdo_while = 0;
230230
int Winit_cstring = 0;
231231
int Wenum_mismatch = 1;
232232
int Wsparse_error = 0;
233+
int Wmemcpy_max_count = 1;
233234
int Wnon_pointer_null = 1;
234235
int Wold_initializer = 1;
235236
int Wone_bit_signed_bitfield = 1;
@@ -506,6 +507,7 @@ static const struct warning {
506507
{ "do-while", &Wdo_while },
507508
{ "enum-mismatch", &Wenum_mismatch },
508509
{ "init-cstring", &Winit_cstring },
510+
{ "memcpy-max-count", &Wmemcpy_max_count },
509511
{ "non-pointer-null", &Wnon_pointer_null },
510512
{ "old-initializer", &Wold_initializer },
511513
{ "one-bit-signed-bitfield", &Wone_bit_signed_bitfield },

lib.h

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ extern int Wdo_while;
120120
extern int Wenum_mismatch;
121121
extern int Wsparse_error;
122122
extern int Winit_cstring;
123+
extern int Wmemcpy_max_count;
123124
extern int Wnon_pointer_null;
124125
extern int Wold_initializer;
125126
extern int Wone_bit_signed_bitfield;

sparse.1

+8
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ trouble.
210210
Sparse does not issue these warnings by default.
211211
.
212212
.TP
213+
.B \-Wmemcpy\-max\-count
214+
Warn about call of \fBmemcpy()\fR, \fBmemset()\fR, \fBcopy_from_user()\fR, or
215+
\fBcopy_to_user()\fR with a large compile-time byte count.
216+
217+
Sparse issues these warnings by default. To turn them off, use
218+
\fB\-Wno\-memcpy\-max\-count\fR.
219+
.
220+
.TP
213221
.B \-Wnon\-pointer\-null
214222
Warn about the use of 0 as a NULL pointer.
215223

sparse.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ static void check_byte_count(struct instruction *insn, pseudo_t count)
153153
return;
154154
if (count->type == PSEUDO_VAL) {
155155
unsigned long long val = count->value;
156-
if (val > 100000ULL)
156+
if (Wmemcpy_max_count && val > 100000ULL)
157+
157158
warning(insn->pos, "%s with byte count of %llu",
158159
show_ident(insn->func->sym->ident), val);
159160
return;

0 commit comments

Comments
 (0)