]> git.ipfire.org Git - thirdparty/iw.git/commitdiff
iw: parse_freqs: check how many center frequencies to expect
authorJohannes Berg <johannes.berg@intel.com>
Thu, 26 Jan 2017 09:20:08 +0000 (10:20 +0100)
committerJohannes Berg <johannes.berg@intel.com>
Thu, 26 Jan 2017 09:20:08 +0000 (10:20 +0100)
Instead of just plain assuming the next two integer arguments
are, if present, center frequencies, teach the code how many
are expected and check that they're actually present.

This makes it slightly better to use the parser before an int
argument, although it's still ambiguous due to the width.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
util.c

diff --git a/util.c b/util.c
index 81299d46448766e581ff4a025c1d76c23008f3b1..b9e9643b5ebb55fcfe43264a83e6f8bf70c60110 100644 (file)
--- a/util.c
+++ b/util.c
@@ -498,40 +498,61 @@ static int parse_freqs(struct chandef *chandef, int argc, char **argv,
 {
        uint32_t freq;
        char *end;
+       bool need_cf1 = false, need_cf2 = false;
 
        if (argc < 1)
                return 0;
 
        chandef->width = str_to_bw(argv[0]);
 
-       /* First argument was not understood, give up gracefully. */
-       if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT)
+       switch (chandef->width) {
+       case NL80211_CHAN_WIDTH_20_NOHT:
+               /* First argument was not understood, give up gracefully. */
                return 0;
+       case NL80211_CHAN_WIDTH_20:
+       case NL80211_CHAN_WIDTH_5:
+       case NL80211_CHAN_WIDTH_10:
+               break;
+       case NL80211_CHAN_WIDTH_80P80:
+               need_cf2 = true;
+               /* fall through */
+       case NL80211_CHAN_WIDTH_40:
+       case NL80211_CHAN_WIDTH_80:
+       case NL80211_CHAN_WIDTH_160:
+               need_cf1 = true;
+               break;
+       }
 
        *parsed += 1;
 
-       if (argc < 2)
+       if (!need_cf1)
                return 0;
 
+       if (argc < 2)
+               return 1;
+
        /* center freq 1 */
        if (!*argv[1])
-               return 0;
+               return 1;
        freq = strtoul(argv[1], &end, 10);
        if (*end)
-               return 0;
+               return 1;
        *parsed += 1;
 
        chandef->center_freq1 = freq;
 
-       if (argc < 3)
+       if (!need_cf2)
                return 0;
 
+       if (argc < 3)
+               return 1;
+
        /* center freq 2 */
        if (!*argv[2])
-               return 0;
+               return 1;
        freq = strtoul(argv[2], &end, 10);
        if (*end)
-               return 0;
+               return 1;
        chandef->center_freq2 = freq;
 
        *parsed += 1;
@@ -554,8 +575,11 @@ static int parse_freqs(struct chandef *chandef, int argc, char **argv,
  * arguments. argc/argv will be updated so that further arguments from the
  * command line can be parsed.
  *
- * Note that no integer argument may follow a frequency definition to allow the
- * user to skip the center frequency definition(s).
+ * Note that despite the fact that the function knows how many center freqs
+ * are needed, there's an ambiguity if the next argument after this is an
+ * integer argument, since the valid channel width values are interpreted
+ * as such, rather than a following argument. This can be avoided by the
+ * user by giving "NOHT" instead.
  *
  * The working specifier if chan is set are:
  *   <channel> [NOHT|HT20|HT40+|HT40-|5MHz|10MHz|80MHz]