topology: do not call strtol directly

Introduce safe_strtol_base() function and redirects all
strtol calls there. Also, improve error and value handling
in callers.

BugLink: https://github.com/alsa-project/alsa-lib/issues/187
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2021-11-02 10:43:27 +01:00
parent f547b2e35f
commit 5fab157a59
3 changed files with 40 additions and 19 deletions

View file

@ -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
*/
@ -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;
}
/*