Linux bluetooth development
 help / color / mirror / Atom feed
From: Johan Hedberg <johan.hedberg@gmail.com>
To: "Frédéric Danis" <frederic.danis@linux.intel.com>
Cc: linux-bluetooth@vger.kernel.org
Subject: Re: [PATCH v2 1/2] Simplify eir_parse function
Date: Thu, 20 Oct 2011 14:19:56 +0300	[thread overview]
Message-ID: <20111020111956.GA5742@fusion.localdomain> (raw)
In-Reply-To: <1319100817-8477-1-git-send-email-frederic.danis@linux.intel.com>

Hi Frédéric,

On Thu, Oct 20, 2011, Frédéric Danis wrote:
> -int eir_parse(struct eir_data *eir, uint8_t *eir_data)
> +static void eir_parse_uuid16(struct eir_data *eir, uint8_t *data, uint8_t len)

If you change data to void * it'd make the following changes easier:

> +	uint8_t *uuid_ptr = data;

How about uint16_t *u16 = data;

It would then make the iteration and conversion easier:

> +	service.type = SDP_UUID16;
> +	for (i = 0; i < len / 2; i++) {
> +		val16 = uuid_ptr[1];
> +		val16 = (val16 << 8) + uuid_ptr[0];
> +		service.value.uuid16 = val16;

This could be simply:

	service.value.uuid16 = btohs(bt_get_unaligned(u16));

> +		uuid_str = bt_uuid2string(&service);
> +		eir->services = g_slist_append(eir->services, uuid_str);
> +		uuid_ptr += 2;

And this u16++, actually just put it after i++ directly in the loop
definintion.

> +static void eir_parse_uuid32(struct eir_data *eir, uint8_t *data, uint8_t len)
> +{
> +	uint8_t *uuid_ptr = data;

uint32_t *u32 = data; (assuming you make data void * like above)

> +	service.type = SDP_UUID32;
> +	for (i = 0; i < len / 4; i++) {
> +		val32 = uuid_ptr[3];
> +		for (k = 2; k >= 0; k--)
> +			val32 = (val32 << 8) + uuid_ptr[k];
> +		service.value.uuid32 = val32;

Just change this all to:

		service.value.uuid32 = bt_get_unaligned(btohl(u32));

> +		uuid_ptr += 4;

And move this to the for-loop definition after i++ as u32++

>  		case EIR_NAME_SHORT:
>  		case EIR_NAME_COMPLETE:
> -			name = (const char *) &eir_data[2];
> -			name_len = field_len - 1;
> +			if (g_utf8_validate((char *) &eir_data[2],
> +							field_len - 1, NULL))
> +				eir->name = g_strndup((char *) &eir_data[2],
> +								field_len - 1);
> +			else
> +				eir->name = g_strdup("");
>  			eir->name_complete = eir_data[1] == EIR_NAME_COMPLETE;
>  			break;

If there are multiple name tags in the EIR data you would leak here all
of them except the last one (devices shouldn't have this but you can't
control all sorts of crazy things people do). To keep the behavior as
the existing code, free eir->name when encountering multiple tags and
just keep the last one.

Also, If the name isn't valid UTF-8 I suppose you can just keep
eir->name as NULL instead of pointing to an empty string?

Johan

  parent reply	other threads:[~2011-10-20 11:19 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-10-20  8:53 [PATCH v2 1/2] Simplify eir_parse function Frédéric Danis
2011-10-20  8:53 ` [PATCH v2 2/2] add unit/test-eir to .gitignore Frédéric Danis
2011-10-20 11:26   ` Johan Hedberg
2011-10-20  9:38 ` [PATCH v2 1/2] Simplify eir_parse function Luiz Augusto von Dentz
2011-10-20 11:19 ` Johan Hedberg [this message]
2011-10-20 12:27   ` Andrei Emeltchenko
2011-10-20 20:24     ` Anderson Lizardo
2011-10-20 14:27   ` Ganir, Chen

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=20111020111956.GA5742@fusion.localdomain \
    --to=johan.hedberg@gmail.com \
    --cc=frederic.danis@linux.intel.com \
    --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