Skip to content

Commit bcfe020

Browse files
committed
add support for -fmemcpy-max-count
By default, sparse will warn if memcpy() (or memset(), copy_from_user(), copy_to_user()) is called with a very large static byte-count. But the limit is currently fixed at 100000, which may be fine for some uses but not for others. For example, this value is too low for sparse to be used on the git tree where, for example, some array used to sort the index is cleared with memset(). Change this by making the limit configurable via a new flag: -fmemcpy-max-count. Signed-off-by: Luc Van Oostenryck <[email protected]>
1 parent 6081052 commit bcfe020

File tree

5 files changed

+30
-3
lines changed

5 files changed

+30
-3
lines changed

cgcc

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ sub check_only_option {
103103
my ($arg) = @_;
104104
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)$/;
106-
return 1 if $arg =~ /^-f(dump-linearize)(=\S*)?$/;
106+
return 1 if $arg =~ /^-f(dump-linearize|memcpy-max-count)(=\S*)?$/;
107107
return 0;
108108
}
109109

lib.c

+18
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ int dbg_dead = 0;
257257

258258
int fmem_report = 0;
259259
int fdump_linearize;
260+
unsigned long long fmemcpy_max_count = 100000;
260261

261262
int preprocess_only;
262263

@@ -671,6 +672,21 @@ static char **handle_switch_O(char *arg, char **next)
671672
return next;
672673
}
673674

675+
static char **handle_switch_fmemcpy_max_count(char *arg, char **next)
676+
{
677+
unsigned long long val;
678+
char *end;
679+
680+
val = strtoull(arg, &end, 0);
681+
if (*end != '\0' || end == arg)
682+
die("error: missing argument to \"-fmemcpy-max-count=\"");
683+
684+
if (val == 0)
685+
val = ~0ULL;
686+
fmemcpy_max_count = val;
687+
return next;
688+
}
689+
674690
static char **handle_switch_ftabstop(char *arg, char **next)
675691
{
676692
char *end;
@@ -714,6 +730,8 @@ static char **handle_switch_f(char *arg, char **next)
714730
return handle_switch_ftabstop(arg+8, next);
715731
if (!strncmp(arg, "dump-", 5))
716732
return handle_switch_fdump(arg+5, next);
733+
if (!strncmp(arg, "memcpy-max-count=", 17))
734+
return handle_switch_fmemcpy_max_count(arg+17, next);
717735

718736
/* handle switches w/ arguments above, boolean and only boolean below */
719737
if (handle_simple_switch(arg, "mem-report", &fmem_report))

lib.h

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ extern int dbg_dead;
147147

148148
extern int fmem_report;
149149
extern int fdump_linearize;
150+
extern unsigned long long fmemcpy_max_count;
150151

151152
extern int arch_m64;
152153

sparse.1

+9
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ Warn about call of \fBmemcpy()\fR, \fBmemset()\fR, \fBcopy_from_user()\fR, or
216216

217217
Sparse issues these warnings by default. To turn them off, use
218218
\fB\-Wno\-memcpy\-max\-count\fR.
219+
220+
The limit can be changed with \fB\-fmemcpy\-max\-count=COUNT\fR,
221+
the default being \fB100000\fR.
219222
.
220223
.TP
221224
.B \-Wnon\-pointer\-null
@@ -364,6 +367,12 @@ Report some statistics about memory allocation used by the tool.
364367
.
365368
.SH OTHER OPTIONS
366369
.TP
370+
.B \-fmemcpy-max-count=COUNT
371+
Set the limit for the warnings given by \fB-Wmemcpy-max-count\fR.
372+
A COUNT of 0, useless in itself, will effectively disable the warning.
373+
The default limit is 100000.
374+
.
375+
.TP
367376
.B \-ftabstop=WIDTH
368377
Set the distance between tab stops. This helps sparse report correct
369378
column numbers in warnings or errors. If the value is less than 1 or

sparse.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ 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 (Wmemcpy_max_count && val > 100000ULL)
157-
156+
if (Wmemcpy_max_count && val > fmemcpy_max_count)
158157
warning(insn->pos, "%s with byte count of %llu",
159158
show_ident(insn->func->sym->ident), val);
160159
return;

0 commit comments

Comments
 (0)