Skip to content

Commit

Permalink
parse_channelstring(): remove useless checks of g_strsplit() results.
Browse files Browse the repository at this point in the history
1. !names[0] could only be true if tokens[i] is an empty string,
   but we already checked at the beginning of the for() loop that
   this is not the case.

   "(names[1] && names[2])" can also never be true, as
   g_strsplit(x, 2) returns a NULL-terminated pointer array with
   either one or a maximum of two non-NULL strings, so either
   names[1] or names[2] is always == NULL.

   Worst of all, in the case that the separator character "="
   doesn't exist in the string, names[0] is the complete string,
   names[1] is == NULL and names[2] is undefined, so it's an error
   to even access it. (Current glib 2.64.1 seems to return an
   array with at least 3 allocated entries even if the result is
   only one string, but I can't find anything about that in the
   docs so better not rely on this behaviour).

2. Likewise, !range[0] could only be true if names[0] is an empty
   string. But we already asserted that it contains at least a dash
   character.

   So both range[0] and range[1] at least contain pointers to
   empty strings. g_strsplit(x, 2) returns a NULL-terminated
   pointer array with a maximum of (and in this case, exactly)
   two non-NULL strings, which means range[2] is always == NULL.

   As a result, "!range[0] || !range[1] || range[2]" can never be
   true.
  • Loading branch information
thierer committed Jun 12, 2020
1 parent f4863d2 commit 7a0e13d
Showing 1 changed file with 0 additions and 13 deletions.
13 changes: 0 additions & 13 deletions parsers.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,6 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
}

names = g_strsplit(tokens[i], "=", 2);
if (!names[0] || (names[1] && names[2])) {
/* Need one or two arguments. */
g_critical("Invalid channel '%s'.", tokens[i]);
g_strfreev(names);
ret = SR_ERR;
break;
}

/* Check first if a channel with the given name already exists, as it
* might contain a dash from a rename in an earlier option.
Expand Down Expand Up @@ -102,12 +95,6 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
}

range = g_strsplit(names[0], "-", 2);
if (!range[0] || !range[1] || range[2]) {
/* Need exactly two arguments. */
g_critical("Invalid channel syntax '%s'.", tokens[i]);
ret = SR_ERR;
goto range_fail;
}

/* Find start of numeric range (first digit) in range string. */
for (sptr = names[0]; *sptr != '-' && !isdigit(*sptr); sptr++);
Expand Down

0 comments on commit 7a0e13d

Please sign in to comment.