alloc-util: add strdupa_safe() + strndupa_safe() and use it everywhere
Let's define two helpers strdupa_safe() + strndupa_safe() which do the same as their non-safe counterparts, except that they abort if called with allocations larger than ALLOCA_MAX. This should ensure that all our alloca() based allocations are subject to this limit. afaics glibc offers three alloca() based APIs: alloca() itself, strndupa() + strdupa(). With this we have now replacements for all of them, that take the limit into account.
This commit is contained in:
@@ -394,7 +394,7 @@ int strv_env_replace_consume(char ***l, char *p) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
name = strndupa(p, t - p);
|
||||
name = strndupa_safe(p, t - p);
|
||||
|
||||
STRV_FOREACH(f, *l)
|
||||
if (env_entry_has_name(*f, name)) {
|
||||
@@ -481,7 +481,7 @@ char *strv_env_get_n(char **l, const char *name, size_t k, unsigned flags) {
|
||||
if (flags & REPLACE_ENV_USE_ENVIRONMENT) {
|
||||
const char *t;
|
||||
|
||||
t = strndupa(name, k);
|
||||
t = strndupa_safe(name, k);
|
||||
return getenv(t);
|
||||
};
|
||||
|
||||
@@ -804,7 +804,7 @@ int putenv_dup(const char *assignment, bool override) {
|
||||
if (!e)
|
||||
return -EINVAL;
|
||||
|
||||
n = strndupa(assignment, e - assignment);
|
||||
n = strndupa_safe(assignment, e - assignment);
|
||||
|
||||
/* This is like putenv(), but uses setenv() so that our memory doesn't become part of environ[]. */
|
||||
if (setenv(n, e + 1, override) < 0)
|
||||
|
||||
Reference in New Issue
Block a user