[Question] Unnecessary negative check on unsigned variable in Bluetooth sockets?

From: Ingyu Jang <hidden>
Date: 2026-01-31 16:36:47

Hi,

I noticed that in the Bluetooth subsystem, there are multiple places
where an unsigned __u32 variable is checked with "<= 0", which makes
the negative check part unreachable.

The pattern appears in three files:

1. net/bluetooth/hidp/sock.c at line 163:
   if (cl.cnum <= 0)
       return -EINVAL;

2. net/bluetooth/cmtp/sock.c at line 159:
   if (cl.cnum <= 0)
       return -EINVAL;

3. net/bluetooth/bnep/sock.c at line 157:
   if (cl.cnum <= 0)
       return -EINVAL;

All three use the same structure pattern where cnum is defined as __u32
(unsigned 32-bit):

  struct hidp_connlist_req { __u32 cnum; ... };
  struct cmtp_connlist_req { __u32 cnum; ... };
  struct bnep_connlist_req { __u32 cnum; ... };

Since __u32 is unsigned, the "< 0" part of the condition is always
false. The checks could be simplified to just "== 0" or "!cl.cnum".

Is this intentional defensive coding, or would it be cleaner to
simplify these to "if (!cl.cnum)" or "if (cl.cnum == 0)"?

Thanks,
Ingyu Jang
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help