Commit 915f6490 authored by Masahiro Yamada's avatar Masahiro Yamada

kconfig: error out if a recursive variable references itself

When using a recursively expanded variable, it is a common mistake
to make circular reference.

For example, Make terminates the following code:

  X = $(X)
  Y := $(X)

Let's detect the circular expansion in Kconfig, too.

On the other hand, a function that recurses itself is a commonly-used
programming technique.  So, Make does not check recursion in the
reference with 'call'.  For example, the following code continues
running eternally:

  X = $(call X)
  Y := $(X)

Kconfig allows circular expansion if one or more arguments are given,
but terminates when the same function is recursively invoked 1000 times,
assuming it is a programming mistake.
Signed-off-by: default avatarMasahiro Yamada <yamada.masahiro@socionext.com>
parent a702a617
...@@ -229,6 +229,7 @@ struct variable { ...@@ -229,6 +229,7 @@ struct variable {
char *name; char *name;
char *value; char *value;
enum variable_flavor flavor; enum variable_flavor flavor;
int exp_count;
struct list_head node; struct list_head node;
}; };
...@@ -253,11 +254,22 @@ static char *variable_expand(const char *name, int argc, char *argv[]) ...@@ -253,11 +254,22 @@ static char *variable_expand(const char *name, int argc, char *argv[])
if (!v) if (!v)
return NULL; return NULL;
if (argc == 0 && v->exp_count)
pperror("Recursive variable '%s' references itself (eventually)",
name);
if (v->exp_count > 1000)
pperror("Too deep recursive expansion");
v->exp_count++;
if (v->flavor == VAR_RECURSIVE) if (v->flavor == VAR_RECURSIVE)
res = expand_string_with_args(v->value, argc, argv); res = expand_string_with_args(v->value, argc, argv);
else else
res = xstrdup(v->value); res = xstrdup(v->value);
v->exp_count--;
return res; return res;
} }
...@@ -284,6 +296,7 @@ void variable_add(const char *name, const char *value, ...@@ -284,6 +296,7 @@ void variable_add(const char *name, const char *value,
v = xmalloc(sizeof(*v)); v = xmalloc(sizeof(*v));
v->name = xstrdup(name); v->name = xstrdup(name);
v->exp_count = 0;
list_add_tail(&v->node, &variable_list); list_add_tail(&v->node, &variable_list);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment