Skip to contentSkip to conversation

Bluetooth: hidp: using strlcpy or strcpy instead of strncpy

PATCH In review Chen Gang 6 participants 15 messages May 7, 2013 – May 21, 2013 7 messages not in the archive

Subsystems (4)
lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 Bluetooth: hidp: using strlcpy or strcpy instead of strncpy

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

---
 net/bluetooth/hidp/core.c |   10 +++-------
 1 files changed, 3 insertions(+), 7 deletions(-)
net/bluetooth/hidp/core.c +3 -7
index 940f5ac..9a8ae63 100644
@@ -76,25 +76,21 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
 	ci->flags = session->flags;
 	ci->state = BT_CONNECTED;
 
-	ci->vendor  = 0x0000;
-	ci->product = 0x0000;
-	ci->version = 0x0000;
-
 	if (session->input) {
 		ci->vendor  = session->input->id.vendor;
 		ci->product = session->input->id.product;
 		ci->version = session->input->id.version;
 		if (session->input->name)
-			strncpy(ci->name, session->input->name, 128);
+			strlcpy(ci->name, session->input->name, 128);
 		else
-			strncpy(ci->name, "HID Boot Device", 128);
+			strcpy(ci->name, "HID Boot Device");
 	}
 
 	if (session->hid) {
 		ci->vendor  = session->hid->vendor;
 		ci->product = session->hid->product;
 		ci->version = session->hid->version;
-		strncpy(ci->name, session->hid->name, 128);
+		strlcpy(ci->name, session->hid->name, 128);
 	}
 }
 
Signature · 1 line
1.7.7.6
#1 lore raw

[Suggestion] Bluetooth: hidp: redundant initialization or issue for function hidp_copy_session

Hello Maintainers:

In net/bluetooth/hidp/core.c, for hidp_copy_session(), the 'session->input' and 'session->hid' are conflict with each other.

And excuse me, I do not quit know the details, but I think we have 2 choices for fixing it:

one is ''if (session->input) { } else if (session->hid) { };'' the other is ''if (seesion->hid) { } else if (session->input) { };''

The first choice assumes the original code has a logical issue; the second choice assumes the original code has redundant initialization.

Please help check.

Thanks.

  71 static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
  72 {
  73         memset(ci, 0, sizeof(*ci));
  74         bacpy(&ci->bdaddr, &session->bdaddr);
  75 
  76         ci->flags = session->flags;
  77         ci->state = BT_CONNECTED;
  78 
  79         ci->vendor  = 0x0000;
  80         ci->product = 0x0000;
  81         ci->version = 0x0000;
  82 
  83         if (session->input) {
  84                 ci->vendor  = session->input->id.vendor;
  85                 ci->product = session->input->id.product;
  86                 ci->version = session->input->id.version;
  87                 if (session->input->name)
  88                         strncpy(ci->name, session->input->name, 128);
  89                 else
  90                         strncpy(ci->name, "HID Boot Device", 128);
  91         }
  92 
  93         if (session->hid) {
  94                 ci->vendor  = session->hid->vendor;
  95                 ci->product = session->hid->product;
  96                 ci->version = session->hid->version;
  97                 strncpy(ci->name, session->hid->name, 128);
  98         }
  99 }

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

Re: [Suggestion] Bluetooth: hidp: redundant initialization or issue for function hidp_copy_session

Hi

Quoted 5 lines · Chen Gang

Hello Maintainers:

In net/bluetooth/hidp/core.c, for hidp_copy_session(), the 'session->input' and 'session->hid' are conflict with each other.

Quoted 8 lines

And excuse me, I do not quit know the details, but I think we have 2 choices for fixing it:

one is ''if (session->input) { } else if (session->hid) { };'' the other is ''if (seesion->hid) { } else if (session->input) { };''

The first choice assumes the original code has a logical issue; the second choice assumes the original code has redundant initialization.

The code is fine. Only one of "->input" or "->hid" can be valid at a time. And exactly one of them is guaranteed to be valid. See hidp_session_dev_init().

I fixed all code that I changed during the rework to say:

if (session->hid)
...
else if (session->input)
...

It makes the code more clear. But I avoided touching all the other places that I didn't change, as the code is technically right. Anyway, I don't care whether we want to fix all other occurrences to use "else if". Feel free to send a patch.

Thanks
David

Quoted 34 lines

Please help check.

Thanks.

  71 static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
  72 {
  73         memset(ci, 0, sizeof(*ci));
  74         bacpy(&ci->bdaddr, &session->bdaddr);
  75
  76         ci->flags = session->flags;
  77         ci->state = BT_CONNECTED;
  78
  79         ci->vendor  = 0x0000;
  80         ci->product = 0x0000;
  81         ci->version = 0x0000;
  82
  83         if (session->input) {
  84                 ci->vendor  = session->input->id.vendor;
  85                 ci->product = session->input->id.product;
  86                 ci->version = session->input->id.version;
  87                 if (session->input->name)
  88                         strncpy(ci->name, session->input->name, 128);
  89                 else
  90                         strncpy(ci->name, "HID Boot Device", 128);
  91         }
  92
  93         if (session->hid) {
  94                 ci->vendor  = session->hid->vendor;
  95                 ci->product = session->hid->product;
  96                 ci->version = session->hid->version;
  97                 strncpy(ci->name, session->hid->name, 128);
  98         }
  99 }

A message that is not in the archive (sent off-list or not yet received): CANq1E4Q8g6Uy5DMiEf3aEBmfWN=KnVCfqxErYr=ZWD94D4vmAQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#5 lore raw

Re: [Suggestion] Bluetooth: hidp: redundant initialization or issue for function hidp_copy_session

Quoted 14 lines · David Herrmann

Hi

Quoted 11 lines · Chen Gang

Hello Maintainers:

In net/bluetooth/hidp/core.c, for hidp_copy_session(), the 'session->input' and 'session->hid' are conflict with each other.

And excuse me, I do not quit know the details, but I think we have 2 choices for fixing it:

one is ''if (session->input) { } else if (session->hid) { };'' the other is ''if (seesion->hid) { } else if (session->input) { };''

Quoted 7 lines
Quoted 2 lines

The first choice assumes the original code has a logical issue; the second choice assumes the original code has redundant initialization.

The code is fine. Only one of "->input" or "->hid" can be valid at a time. And exactly one of them is guaranteed to be valid. See hidp_session_dev_init().

Oh, really it is, thanks.

Quoted 7 lines

I fixed all code that I changed during the rework to say:

if (session->hid)
..
else if (session->input)
..

Quoted 5 lines

It makes the code more clear. But I avoided touching all the other places that I didn't change, as the code is technically right. Anyway, I don't care whether we want to fix all other occurrences to use "else if". Feel free to send a patch.

Me too: "avoided touching all the other places that I didn't change, as the code is technically right".

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
Reviewed-by on v1 1/1
#6 lore raw
In reply to Chen Gang

Hi

Quoted 23 lines · Chen Gang

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

---
 net/bluetooth/hidp/core.c |   10 +++-------
 1 files changed, 3 insertions(+), 7 deletions(-)
net/bluetooth/hidp/core.c +0 -4
index 940f5ac..9a8ae63 100644
@@ -76,25 +76,21 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
        ci->flags = session->flags;
        ci->state = BT_CONNECTED;
 
-       ci->vendor  = 0x0000;
-       ci->product = 0x0000;
-       ci->version = 0x0000;
-

net/bluetooth/hidp/core.c static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo · line 86

Quoted 10 lines
net/bluetooth/hidp/core.c +2 -2
        if (session->input) {
                ci->vendor  = session->input->id.vendor;
                ci->product = session->input->id.product;
                ci->version = session->input->id.version;
                if (session->input->name)
-                       strncpy(ci->name, session->input->name, 128);
+                       strlcpy(ci->name, session->input->name, 128);
                else
-                       strncpy(ci->name, "HID Boot Device", 128);
+                       strcpy(ci->name, "HID Boot Device");

I'd actually prefer strlcpy() here, too (better be safe). Other than that the patch looks fine.

Regards
David

Quoted 13 lines
net/bluetooth/hidp/core.c +1 -2
        }
 
        if (session->hid) {
                ci->vendor  = session->hid->vendor;
                ci->product = session->hid->product;
                ci->version = session->hid->version;
-               strncpy(ci->name, session->hid->name, 128);
+               strlcpy(ci->name, session->hid->name, 128);
        }
 }
 
--

1.7.7.6

A message that is not in the archive (sent off-list or not yet received): CANq1E4SmY+CD0uf53_b6GeFkQ-LYnpdxsDGSRMWdn2i1mnb6WQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

author
#8 lore raw
Quoted 20 lines · David Herrmann
Quoted 19 lines · Chen Gang
Quoted 19 lines

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

---
 net/bluetooth/hidp/core.c |   10 +++-------
 1 files changed, 3 insertions(+), 7 deletions(-)
net/bluetooth/hidp/core.c +0 -0
index 940f5ac..9a8ae63 100644
@@ -76,25 +76,21 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
        ci->flags = session->flags;
        ci->state = BT_CONNECTED;
 
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
net/bluetooth/hidp/core.c +0 -4
-       ci->vendor  = 0x0000;
-       ci->product = 0x0000;
-       ci->version = 0x0000;
-
        if (session->input) {
                ci->vendor  = session->input->id.vendor;
Quoted 8 lines

net/bluetooth/hidp/core.c static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo · line 86

Quoted 8 lines
net/bluetooth/hidp/core.c +2 -2
                ci->product = session->input->id.product;
                ci->version = session->input->id.version;
                if (session->input->name)
-                       strncpy(ci->name, session->input->name, 128);
+                       strlcpy(ci->name, session->input->name, 128);
                else
-                       strncpy(ci->name, "HID Boot Device", 128);
+                       strcpy(ci->name, "HID Boot Device");

I'd actually prefer strlcpy() here, too (better be safe). Other than that the patch looks fine.

OK, thanks. I will send patch v2.

Signature · 3 lines
Chen Gang

Asianux Corporation

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#10 lore raw

PATCH v2 1/1 Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

Better use ''if(session->hid) {} else if(session->input) {}'' instead of ''if(session->hid) {}; if(session->input) {};''

---
 net/bluetooth/hidp/core.c |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)
net/bluetooth/hidp/core.c +4 -10
index 940f5ac..f13a8da 100644
@@ -76,25 +76,19 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
 	ci->flags = session->flags;
 	ci->state = BT_CONNECTED;
 
-	ci->vendor  = 0x0000;
-	ci->product = 0x0000;
-	ci->version = 0x0000;
-
 	if (session->input) {
 		ci->vendor  = session->input->id.vendor;
 		ci->product = session->input->id.product;
 		ci->version = session->input->id.version;
 		if (session->input->name)
-			strncpy(ci->name, session->input->name, 128);
+			strlcpy(ci->name, session->input->name, 128);
 		else
-			strncpy(ci->name, "HID Boot Device", 128);
-	}
-
-	if (session->hid) {
+			strlcpy(ci->name, "HID Boot Device", 128);
+	} else if (session->hid) {
 		ci->vendor  = session->hid->vendor;
 		ci->product = session->hid->product;
 		ci->version = session->hid->version;
-		strncpy(ci->name, session->hid->name, 128);
+		strlcpy(ci->name, session->hid->name, 128);
 	}
 }
 
Signature · 1 line
1.7.7.6

A message that is not in the archive (sent off-list or not yet received): [email protected]

Reviewed-by
#12 lore raw

Re: [PATCH v2] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

Hi Chen

Quoted 8 lines · Chen Gang

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

Better use ''if(session->hid) {} else if(session->input) {}'' instead of ''if(session->hid) {}; if(session->input) {};''

Yep, looks good now.

Thanks
David

Quoted 41 lines
---
 net/bluetooth/hidp/core.c |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)
net/bluetooth/hidp/core.c +4 -11
index 940f5ac..f13a8da 100644
@@ -76,25 +76,19 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
        ci->flags = session->flags;
        ci->state = BT_CONNECTED;
 
-       ci->vendor  = 0x0000;
-       ci->product = 0x0000;
-       ci->version = 0x0000;
-
        if (session->input) {
                ci->vendor  = session->input->id.vendor;
                ci->product = session->input->id.product;
                ci->version = session->input->id.version;
                if (session->input->name)
-                       strncpy(ci->name, session->input->name, 128);
+                       strlcpy(ci->name, session->input->name, 128);
                else
-                       strncpy(ci->name, "HID Boot Device", 128);
-       }
-
-       if (session->hid) {
+                       strlcpy(ci->name, "HID Boot Device", 128);
+       } else if (session->hid) {
                ci->vendor  = session->hid->vendor;
                ci->product = session->hid->product;
                ci->version = session->hid->version;
-               strncpy(ci->name, session->hid->name, 128);
+               strlcpy(ci->name, session->hid->name, 128);
        }
 }
 
--

1.7.7.6

#13 lore raw

Re: [PATCH v2] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

Quoted 4 lines · David Herrmann
Quoted 3 lines · Chen Gang
Quoted 3 lines

For NUL terminated string, need always let it ended by zero.

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

Better use ''if(session->hid) {} else if(session->input) {}'' instead of ''if(session->hid) {}; if(session->input) {};''

Yep, looks good now.

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation
Acked-by
#14 lore raw

Re: [PATCH v2] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

In reply to a message not in the archive
Quoted 10 lines · Chen Gang

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

Better use ''if(session->hid) {} else if(session->input) {}'' instead of ''if(session->hid) {}; if(session->input) {};''

Makes sense.

Gustavo, going to take this, please?

Signature · 2 lines
Jiri Kosina
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): alpine.LNX.2.00.1305091041140.23038-ztGlSCb7Y1iN3ZZ/[email protected]

author
#16 lore raw

Re: [PATCH v2] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

Quoted 11 lines · Jiri Kosina
Quoted 9 lines · Chen Gang
Quoted 9 lines

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundent initializations.

Better use ''if(session->hid) {} else if(session->input) {}'' instead of ''if(session->hid) {}; if(session->input) {};''

Quoted 9 lines
Quoted 1 line
Quoted 1 line

Makes sense.

Gustavo, going to take this, please?

-- Jiri Kosina SUSE Labs

Thanks, and also help to fix the spelling: redundent -> redundant. (if need let me send patch v3, please reply to tell me, thanks)

Signature · 3 lines
Chen Gang

Asianux Corporation
#17 lore raw

PATCH v3 1/1 Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

In reply to Jiri Kosina

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundant initialization.

Better use ''if(session->hid) {} else if(session->input) {}"" instead of ''if(session->hid) {}; if(session->input) {};''

---
 net/bluetooth/hidp/core.c |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)
net/bluetooth/hidp/core.c +4 -10
index 940f5ac..f13a8da 100644
@@ -76,25 +76,19 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
 	ci->flags = session->flags;
 	ci->state = BT_CONNECTED;
 
-	ci->vendor  = 0x0000;
-	ci->product = 0x0000;
-	ci->version = 0x0000;
-
 	if (session->input) {
 		ci->vendor  = session->input->id.vendor;
 		ci->product = session->input->id.product;
 		ci->version = session->input->id.version;
 		if (session->input->name)
-			strncpy(ci->name, session->input->name, 128);
+			strlcpy(ci->name, session->input->name, 128);
 		else
-			strncpy(ci->name, "HID Boot Device", 128);
-	}
-
-	if (session->hid) {
+			strlcpy(ci->name, "HID Boot Device", 128);
+	} else if (session->hid) {
 		ci->vendor  = session->hid->vendor;
 		ci->product = session->hid->product;
 		ci->version = session->hid->version;
-		strncpy(ci->name, session->hid->name, 128);
+		strlcpy(ci->name, session->hid->name, 128);
 	}
 }
 
Signature · 1 line
1.7.7.6
#18 lore raw

Re: [PATCH v3] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

Hello Maintainers:

Please help check this patch when you have time, thanks.

It is "Reviewed-by: David Herrmann <[email protected]>"

And also is "Acked-by: Jiri Kosina <[email protected]>"

If need me do any additional things, please tell me.

Thanks.

Quoted 49 lines · Chen Gang

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundant initialization.

Better use ''if(session->hid) {} else if(session->input) {}"" instead of ''if(session->hid) {}; if(session->input) {};''

---
 net/bluetooth/hidp/core.c |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)
net/bluetooth/hidp/core.c +4 -10
index 940f5ac..f13a8da 100644
@@ -76,25 +76,19 @@ static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo
 	ci->flags = session->flags;
 	ci->state = BT_CONNECTED;
 
-	ci->vendor  = 0x0000;
-	ci->product = 0x0000;
-	ci->version = 0x0000;
-
 	if (session->input) {
 		ci->vendor  = session->input->id.vendor;
 		ci->product = session->input->id.product;
 		ci->version = session->input->id.version;
 		if (session->input->name)
-			strncpy(ci->name, session->input->name, 128);
+			strlcpy(ci->name, session->input->name, 128);
 		else
-			strncpy(ci->name, "HID Boot Device", 128);
-	}
-
-	if (session->hid) {
+			strlcpy(ci->name, "HID Boot Device", 128);
+	} else if (session->hid) {
 		ci->vendor  = session->hid->vendor;
 		ci->product = session->hid->product;
 		ci->version = session->hid->version;
-		strncpy(ci->name, session->hid->name, 128);
+		strlcpy(ci->name, session->hid->name, 128);
 	}
 }
 
Signature · 3 lines
Chen Gang

Asianux Corporation

A message that is not in the archive (sent off-list or not yet received): [email protected]

#20 lore raw

Re: [PATCH v3] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

Hi Chen,

Quoted 6 lines · Chen Gang

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundant initialization.

Quoted 7 lines

Better use ''if(session->hid) {} else if(session->input) {}"" instead of ''if(session->hid) {}; if(session->input) {};''

---
 net/bluetooth/hidp/core.c |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)

Sorry for the big delay on this, patches has now been applied to bluetooth-next. Thanks all.

Gustavo

#21 lore raw

Re: [PATCH v3] Bluetooth: hidp: using strlcpy instead of strncpy, also beautify code.

Quoted 13 lines · Gustavo Padovan

Hi Chen,

Quoted 9 lines · Chen Gang
Quoted 9 lines

For NUL terminated string, need always let it ended by zero.

Since have already called memcpy() to initialize 'ci', so need not redundant initialization.

Better use ''if(session->hid) {} else if(session->input) {}"" instead of ''if(session->hid) {}; if(session->input) {};''

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
---
 net/bluetooth/hidp/core.c |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)

Sorry for the big delay on this, patches has now been applied to bluetooth-next. Thanks all.

It doesn't matter, every member have their own work, especially, most of us are busy.

Thank you for your work, too.

Thanks.

Signature · 3 lines
Chen Gang

Asianux Corporation