Page MenuHomeFreeBSD

Use NULL/'\0' instead of 0; explicitly test for them
AbandonedPublic

Authored by ngie on Apr 28 2016, 6:59 PM.
Tags
None
Referenced Files
Unknown Object (File)
Oct 24 2024, 9:47 PM
Unknown Object (File)
Oct 19 2024, 11:42 PM
Unknown Object (File)
Oct 18 2024, 9:02 AM
Unknown Object (File)
Oct 1 2024, 1:56 PM
Unknown Object (File)
Oct 1 2024, 11:25 AM
Unknown Object (File)
Sep 27 2024, 10:21 PM
Unknown Object (File)
Sep 22 2024, 5:31 PM
Unknown Object (File)
Sep 22 2024, 4:32 AM

Details

Reviewers
None
Summary

Use NULL/'\0' instead of 0; explicitly test for them

This also fixes 2 dereferences after NULL.

MFC after: 3 days
Reported by: Coverity
CID: 1018898, 1018899
Sponsored by: EMC / Isilon Storage Division

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 3500
Build 3540: arc lint + arc unit

Event Timeline

ngie retitled this revision from to Use NULL/'\0' instead of 0; explicitly test for them.
ngie updated this object.
ngie edited the test plan for this revision. (Show Details)
ngie added subscribers: araujo, eccramer_gmail.com, pfg and 2 others.

Handle a missed dereference after NULL in do_buff_decode(..)

I'm for NULL instead of 0 as a pointer value, but I don't really understand the need to spell (char)0 as the longer '\0'. Char is just an integer type like short, int, long โ€” we spell the zero constant for those other integers as just 0.

Is that a style(9) suggestion? Looking at style(9), I do see 3 references to using '\0' as a constant.

References to '\0' date to r14966, FWIW, as a suggestion to do that instead of treating a char NUL value as an implicit bool false.

In D6142#130696, @cem wrote:

I'm for NULL instead of 0 as a pointer value, but I don't really understand the need to spell (char)0 as the longer '\0'. Char is just an integer type like short, int, long โ€” we spell the zero constant for those other integers as just 0.

Is that a style(9) suggestion? Looking at style(9), I do see 3 references to using '\0' as a constant.

It's spelled out here implicitly as these aren't boolean types:

Do not use ! for tests unless it is a boolean, e.g. use:

if (*p == '\0')

not:

if (!*p)

I think style(9) should say "thou shalt always initialize (char)0 as '\0'" (sic).

Right, that's the blurb that was added in r14966 I mentioned earlier.

I'm not sure the intent is to suggest '\0', though. It's not like libcam is assigning false to a char variable.

The value '\0' literally is just a zero with some extra boilerplate. I don't see a big advantage over just using ordinary, unambiguous 0.

In D6142#130702, @cem wrote:

Right, that's the blurb that was added in r14966 I mentioned earlier.

I'm not sure the intent is to suggest '\0', though. It's not like libcam is assigning false to a char variable.

The value '\0' literally is just a zero with some extra boilerplate. I don't see a big advantage over just using ordinary, unambiguous 0.

Yeah... I do '\0' for explicitness so I don't have to go back and figure out what the type is. A lot of the older code (e.g. hexdump) uses 0 instead of '\0'.

lib/libcam/scsi_cmdparse.c
175

This is a semantic change, as well as a cosmetic one.
It's likely the right change, but I'm not sure....

391

Ditto.

ngie marked 2 inline comments as done.Apr 29 2016, 1:12 AM
ngie added inline comments.
lib/libcam/scsi_cmdparse.c
175

Yes. This is the issue that Coverity pointed out.

391

Ditto too ;).

ngie marked 4 inline comments as done.Apr 29 2016, 1:12 AM