Skip to content

Commit

Permalink
topology: do not call strtol directly
Browse files Browse the repository at this point in the history
Introduce safe_strtol_base() function and redirects all
strtol calls there. Also, improve error and value handling
in callers.

BugLink: #187
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
  • Loading branch information
perexg committed Nov 2, 2021
1 parent f547b2e commit 5fab157
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 19 deletions.
6 changes: 1 addition & 5 deletions src/topology/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,8 @@ static int write_hex(char *buf, char *str, int width)
void *p = &val;

errno = 0;
val = strtol(str, NULL, 16);

if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
|| (errno != 0 && val == 0)) {
if (safe_strtol_base(str, &val, 16) < 0)
return -EINVAL;
}

switch (width) {
case 1:
Expand Down
13 changes: 10 additions & 3 deletions src/topology/ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ static const struct map_elem control_map[] = {

static int lookup_ops(const char *c)
{
unsigned int i;
int i;
long ret;

for (i = 0; i < ARRAY_SIZE(control_map); i++) {
for (i = 0; i < (int)ARRAY_SIZE(control_map); i++) {
if (strcmp(control_map[i].name, c) == 0)
return control_map[i].id;
}

/* cant find string name in our table so we use its ID number */
return strtol(c, NULL, 0);
i = safe_strtol(c, &ret);
if (i < 0) {
SNDERR("wrong kcontrol ops value string '%s'", c);
return i;
}

return ret;
}

const char *tplg_ops_name(int type)
Expand Down
40 changes: 29 additions & 11 deletions src/topology/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@
#include "list.h"
#include "tplg_local.h"

/*
* Safe strtol call
*/
int safe_strtol_base(const char *str, long *val, int base)
{
char *end;
long v;
if (!*str)
return -EINVAL;
errno = 0;
v = strtol(str, &end, base);
if (errno)
return -errno;
if (*end)
return -EINVAL;
*val = v;
return 0;
}

/*
* Get integer value
*/
Expand All @@ -35,24 +54,23 @@ int tplg_get_integer(snd_config_t *n, int *val, int base)
err = snd_config_get_integer(n, &lval);
if (err < 0)
return err;
if (lval < INT_MIN || lval > INT_MAX)
return -ERANGE;
*val = lval;
return err;
goto __retval;
case SND_CONFIG_TYPE_STRING:
err = snd_config_get_string(n, &str);
if (err < 0)
return err;
errno = 0;
*val = strtol(str, NULL, base);
if (errno == ERANGE)
return -ERANGE;
if (errno && *val == 0)
return -EINVAL;
return 0;
err = safe_strtol_base(str, &lval, base);
if (err < 0)
return err;
goto __retval;
default:
return -EINVAL;
}
__retval:
if (lval < INT_MIN || lval > INT_MAX)
return -ERANGE;
*val = lval;
return 0;
}

/*
Expand Down

0 comments on commit 5fab157

Please sign in to comment.