Re: [PATCH] ifplugd: repeated setenv calls
Maksym Kryzhanovskyy
xmaks at email.cz
Mon May 10 19:55:16 UTC 2010
> On Friday 07 May 2010 10:00, Maksym Kryzhanovskyy wrote:
> > > What we can do is to use the sequence
> > >
> > > unsetenv(IFPLUGD_ENV_PREVIOUS);
> > > free(env_PREVIOUS);
> > > env_PREVIOUS = xasprintf(IFPLUGD_ENV_PREVIOUS"=%s",
> > > strstatus(G.iface_last_status));
> > > putenv(env_PREVIOUS);
> > >
> > > every time we want to set the variable to the new value.
> > > env_PREVIOUS is a global variable of type char*.
> > > This will work correctly with any libc.
> > >
> > > --
> > > vda
> > >
> >
> > Hi Denis,
> >
> > thanks for your reply. I wrote two alternatives to
> > apply this technique. Please consider their usage.
>
> ifplugd.setenv1.patch looks better. These lines are not needed:
> + G.env_PREVIOUS = NULL; \
> + G.env_CURRENT = NULL; \
> the rest is fine.
>
> I applied a bit different version of it - I set/unset
> env just around spawn_and_wait(). See attached.
>
Attached is a typo patch for IFPLUGD_ENV_CURRENT
>
> ifplugd.setenv2.patch:
>
> + env = getenv(name);
> + unsetenv(name);
> + if (env != NULL) {
> + free(env - strlen(name) - 1);
> + }
>
> Why do you think getenv result is safe to be freed?
> Initial program environment is NOT malloced,
> you must not free it, this may SEGV!
But when getenv returns non-null pointer it means
that the environment is allocated and var is found
in the table, or not? It also returns a pointer to
the previous block inserted by putenv().
+ env = getenv(name);
+ if (env != NULL) {
+ unsetenv(name);
+ free(env - strlen(name) - 1);
+ }
Valgrind is ok. Pointers are equal:
env_PREVIOUS = xasprintf("%s=%s", IFPLUGD_ENV_PREVIOUS, strstatus(G.iface_prev_status));
putenv(env_PREVIOUS);
env_CURRENT = xasprintf("%s=%s", IFPLUGD_ENV_CURRENT, strstatus(G.iface_last_status));
putenv(env_CURRENT);
bb_error_msg("%x:%x", env_PREVIOUS, getenv(IFPLUGD_ENV_PREVIOUS) - strlen(IFPLUGD_ENV_PREVIOUS) - 1);
bb_error_msg("%x:%x", env_CURRENT, getenv(IFPLUGD_ENV_CURRENT) - strlen(IFPLUGD_ENV_CURRENT) - 1);
>
> --
> vda
>
>
>
Max
-------------- next part --------------
A non-text attachment was scrubbed...
Name: busybox.typo.patch
Type: text/x-patch
Size: 611 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20100510/0a259a4d/attachment.bin>
More information about the busybox
mailing list