[PATCH 02/12] staging: dgnc: replaces generic struct from sizeof calls

Dan Carpenter dan.carpenter at oracle.com
Tue Sep 3 17:57:21 UTC 2013


On Tue, Sep 03, 2013 at 01:26:05PM -0400, Lidza Louina wrote:
> And:
> memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
> becomes
> memcpy(&ch->ch_digi, &new_digi, sizeof(*ch->ch_digi));

Nope.  "ch->ch_digi" is not a pointer so this will generate a compile
error.  That one should be:

	memcpy(&ch->ch_digi, &new_digi, sizeof(ch->ch_digi));

The theory of this sizeof() style is that the relationship between
the sizeof() and the variable is clear immediately.  If it's (struct
foo) then you maybe have to look up that it's the correct struct.  In
reality those bugs are very rare though.  (I have audited the kernel
for these).

Smatch would have found some of these bugs I think...

git://repo.or.cz/smatch.git
cd smatch
make
cd ~/kernel/src/
~/smatch/smatch_scripts/kchecker drivers/staging/dgnc/dgnc_tty.c

Smatch also gives you Sparse for free:

~/smatch/smatch_scripts/kchecker --sparse drivers/staging/dgnc/dgnc_tty.c

The Smatch version of Sparse might be a little out of date...

regards,
dan carpenter


More information about the devel mailing list