alsa-devel.alsa-project.org archive mirror
 help / color / mirror / Atom feed
From: "Benoît Thébaudeau" <benoit.thebaudeau@advansee.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: alsa-devel@alsa-project.org, Clemens Ladisch <clemens@ladisch.de>
Subject: Re: [PATCH] alsa-lib/tlv: fix handling of raw value	ranges
Date: Mon, 21 May 2012 21:38:22 +0200 (CEST)	[thread overview]
Message-ID: <1701803399.1049204.1337629102784.JavaMail.root@advansee.com> (raw)
In-Reply-To: <s5hk422qek0.wl%tiwai@suse.de>

Hi Takashi, Clemens, all,

Takashi Iwai wrote:
> Benoît Thébaudeau wrote:
> > 
> > Clemens Ladisch wrote:
> > > I'll see if I can cobble together some TLV checking code ...
> > 
> > Great.
> > 
> > > > Because it's useless to wait until the end of the loop to
> > > > return
> > > > -EINVAL,
> > > 
> > > It is not necessary to optimize for error cases.
> > 
> > It's not an optimization, but a simplification. Otherwise, pos
> > would have to be
> > tested again after the loop to differentiate the loop-skipped case
> > from the
> > at-least-one-loop-iteration cases to return either -EINVAL or the
> > prev submax.
> 
> Unless it's really a hot path, such a micro-optimization doesn't give
> much value.
> 
> Rather better to keep the standard style that makes people easier to
> read the code.  A do-while loop with a large block is usually less
> readable than a normal while loop.

Would you agree with the following revision?

[PATCH] alsa-lib/tlv: improve robustness of raw value ranges

snd_tlv_convert_from_dB() relies on rangemin/max blindly. Since this function is
exported, it is better for robustness and consistency to parse the range
properly, which this patch does.

Signed-off-by: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>

--- alsa-lib/src/control/tlv.c
+++ alsa-lib/src/control/tlv.c
@@ -291,41 +291,37 @@ int snd_tlv_convert_from_dB(unsigned int
 {
         switch (tlv[0]) {
         case SND_CTL_TLVT_DB_RANGE: {
-                long dbmin, dbmax, prev_rangemax;
+                long dbmin, dbmax, prev_submax;
                 unsigned int pos, len;
                 len = int_index(tlv[1]);
-                if (len > MAX_TLV_RANGE_SIZE)
-                        return -EINVAL;
-                if (snd_tlv_get_dB_range(tlv, rangemin, rangemax,
-                                         &dbmin, &dbmax))
+                if (len < 6 || len > MAX_TLV_RANGE_SIZE)
                         return -EINVAL;
-                if (db_gain <= dbmin) {
-                        *value = rangemin;
-                        return 0;
-                } else if (db_gain >= dbmax) {
-                        *value = rangemax;
-                        return 0;
-                }
                 pos = 2;
-                prev_rangemax = 0;
+                prev_submax = 0;
                 while (pos + 4 <= len) {
-                        rangemin = (int)tlv[pos];
-                        rangemax = (int)tlv[pos + 1];
+                        long submin, submax;
+                        submin = (int)tlv[pos];
+                        submax = (int)tlv[pos + 1];
+                        if (rangemax < submax)
+                                submax = rangemax;
                         if (!snd_tlv_get_dB_range(tlv + pos + 2,
-                                                  rangemin, rangemax,
+                                                  submin, submax,
                                                   &dbmin, &dbmax) &&
                             db_gain >= dbmin && db_gain <= dbmax)
                                 return snd_tlv_convert_from_dB(tlv + pos + 2,
-                                                               rangemin, rangemax,
+                                                               submin, submax,
                                                                db_gain, value, xdir);
                         else if (db_gain < dbmin) {
-                                *value = xdir ? rangemin : prev_rangemax;
+                                *value = xdir || pos == 2 ? submin : prev_submax;
                                 return 0;
                         }
-                        prev_rangemax = rangemax;
+                        prev_submax = submax;
+                        if (rangemax == submax)
+                                break;
                         pos += int_index(tlv[pos + 3]) + 4;
                 }
-                return -EINVAL;
+                *value = prev_submax;
+                return 0;
         }
         case SND_CTL_TLVT_DB_SCALE: {
                 int min, step, max;
_______________________________________________
Alsa-devel mailing list
Alsa-devel@alsa-project.org
http://mailman.alsa-project.org/mailman/listinfo/alsa-devel

  parent reply	other threads:[~2012-05-21 19:34 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <a995c693-9d0b-4572-af9c-85e3deebf37c@zose-store-12>
2012-03-29 23:05 ` [PATCH] alsa-lib/tlv: fix handling of raw value ranges Benoît Thébaudeau
2012-03-30  8:37   ` Clemens Ladisch
2012-03-30 10:41     ` Benoît Thébaudeau
2012-03-30 11:44       ` Takashi Iwai
2012-03-30 11:51         ` Takashi Iwai
2012-03-30 13:07           ` Benoît Thébaudeau
2012-03-30 13:23             ` Takashi Iwai
2012-03-30 12:38       ` Clemens Ladisch
2012-03-30 13:07         ` Benoît Thébaudeau
2012-03-30 13:10           ` Benoît Thébaudeau
2012-03-30 13:51           ` Clemens Ladisch
2012-03-30 13:58             ` Benoît Thébaudeau
2012-03-30 15:20               ` Benoît Thébaudeau
2012-03-30 15:45                 ` Takashi Iwai
2012-03-30 15:59                   ` Benoît Thébaudeau
2012-03-30 15:44               ` Takashi Iwai
2012-03-30 16:15                 ` Benoît Thébaudeau
2012-05-21 19:38                 ` Benoît Thébaudeau [this message]
2012-05-22  0:57                   ` Takashi Iwai
2012-05-22  2:19                     ` Benoît Thébaudeau
2012-05-22  8:45                       ` Takashi Iwai
2012-05-22 16:06                         ` Benoît Thébaudeau
2012-05-22 23:54                           ` Takashi Iwai
2012-06-02  8:09       ` James Courtier-Dutton
2012-06-02 12:26         ` Benoît Thébaudeau

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=1701803399.1049204.1337629102784.JavaMail.root@advansee.com \
    --to=benoit.thebaudeau@advansee.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=clemens@ladisch.de \
    --cc=tiwai@suse.de \
    /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;
as well as URLs for NNTP newsgroup(s).