Linux bluetooth development
 help / color / mirror / Atom feed
From: Anderson Lizardo <anderson.lizardo@openbossa.org>
To: linux-bluetooth@vger.kernel.org
Cc: Anderson Lizardo <anderson.lizardo@openbossa.org>
Subject: [PATCH BlueZ 2/3] input: Refactor req->name composition code
Date: Sun,  6 Jan 2013 11:26:53 -0400	[thread overview]
Message-ID: <1357486014-24600-2-git-send-email-anderson.lizardo@openbossa.org> (raw)
In-Reply-To: <1357486014-24600-1-git-send-email-anderson.lizardo@openbossa.org>

Use snprintf() instead of strncpy()/strcat()/strncat() to avoid
error-prone size calculations.

Note that this commit introduces a slight change: if Service Description
and Provider Name are used to compose req->name, the old code built it
as:

[up to 126 bytes of Provider Name][whitespace][up to 127 bytes of
Service Description, limited to req->name remaining buffer size]

Now it should be:

[up to 127 bytes of Provider Name][whitespace + Service Description,
limited to req->name remaining buffer size]

Hopefully, this change will not affect normal usage.
---
 profiles/input/device.c |   22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/profiles/input/device.c b/profiles/input/device.c
index 6c152f3..0c71786 100644
--- a/profiles/input/device.c
+++ b/profiles/input/device.c
@@ -209,19 +209,17 @@ static void extract_hid_record(sdp_record_t *rec, struct hidp_connadd_req *req)
 
 	pdlist = sdp_data_get(rec, 0x0101);
 	pdlist2 = sdp_data_get(rec, 0x0102);
-	if (pdlist) {
-		if (pdlist2) {
-			if (strncmp(pdlist->val.str, pdlist2->val.str, 5)) {
-				strncpy(req->name, pdlist2->val.str, 126);
-				strcat(req->name, " ");
-			}
-			strncat(req->name, pdlist->val.str, 127 - strlen(req->name));
-		} else
-			strncpy(req->name, pdlist->val.str, 127);
+	if (pdlist && pdlist2 &&
+				strncmp(pdlist->val.str, pdlist2->val.str, 5)) {
+		snprintf(req->name, sizeof(req->name), "%s %s",
+					pdlist2->val.str, pdlist->val.str);
 	} else {
-		pdlist2 = sdp_data_get(rec, 0x0100);
-		if (pdlist2)
-			strncpy(req->name, pdlist2->val.str, 127);
+		if (!pdlist)
+			pdlist = sdp_data_get(rec, 0x0100);
+
+		if (pdlist)
+			snprintf(req->name, sizeof(req->name), "%s",
+							pdlist->val.str);
 	}
 
 	pdlist = sdp_data_get(rec, SDP_ATTR_HID_PARSER_VERSION);
-- 
1.7.9.5


  reply	other threads:[~2013-01-06 15:26 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-06 15:26 [PATCH BlueZ 1/3] input: Fix buffer overflow when parsing HID SDP record Anderson Lizardo
2013-01-06 15:26 ` Anderson Lizardo [this message]
2013-01-06 15:26 ` [PATCH BlueZ 3/3] input: Use defines for SDP attribute names Anderson Lizardo
2013-01-06 16:18 ` [PATCH BlueZ 1/3] input: Fix buffer overflow when parsing HID SDP record Johan Hedberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1357486014-24600-2-git-send-email-anderson.lizardo@openbossa.org \
    --to=anderson.lizardo@openbossa.org \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox