Fix a memory leak in hotplug2 environment handling.
[openwrt.git] / package / hotplug2 / patches / 100-env_memleak.patch
1 diff -Naur a/action.c b/action.c
2 --- a/action.c 2009-11-18 13:15:21.000000000 +0000
3 +++ b/action.c 2009-11-18 13:11:19.000000000 +0000
4 @@ -31,6 +31,30 @@
5 }
6
7 /**
8 + * Creates a "key=value" string from the given key and value
9 + *
10 + * @1 Key
11 + * @2 Value
12 + *
13 + * Returns: Newly allocated string in "key=value" form
14 + *
15 + */
16 +static char* alloc_env(const char *key, const char *value) {
17 + size_t keylen, vallen;
18 + char *combined;
19 +
20 + keylen = strlen(key);
21 + vallen = strlen(value) + 1;
22 +
23 + combined = xmalloc(keylen + vallen + 1);
24 + memcpy(combined, key, keylen);
25 + combined[keylen] = '=';
26 + memcpy(&combined[keylen + 1], value, vallen);
27 +
28 + return combined;
29 +}
30 +
31 +/**
32 * Choose what action should be taken according to passed settings.
33 *
34 * @1 Hotplug settings
35 @@ -41,16 +65,25 @@
36 */
37 void action_perform(struct settings_t *settings, struct uevent_t *event) {
38 int i;
39 + char **env;
40 +
41 + env = xmalloc(sizeof(char *) * event->env_vars_c);
42 +
43 + for (i = 0; i < event->env_vars_c; i++) {
44 + env[i] = alloc_env(event->env_vars[i].key, event->env_vars[i].value);
45 + putenv(env[i]);
46 + }
47
48 - for (i = 0; i < event->env_vars_c; i++)
49 - setenv(event->env_vars[i].key, event->env_vars[i].value, 1);
50 -
51 if (settings->dumb == 0) {
52 ruleset_execute(&settings->rules, event, settings);
53 } else {
54 action_dumb(settings, event);
55 }
56
57 - for (i = 0; i < event->env_vars_c; i++)
58 + for (i = 0; i < event->env_vars_c; i++) {
59 unsetenv(event->env_vars[i].key);
60 + free(env[i]);
61 + }
62 +
63 + free(env);
64 }
This page took 0.043211 seconds and 5 git commands to generate.