maria-developers team mailing list archive
-
maria-developers team
-
Mailing list archive
-
Message #03274
Re: [Commits] Rev 2866: mysqltest: use setenv, not putenv, to make gcov happy. in http://bazaar.launchpad.net/~maria-captains/maria/5.1/
Hi, Michael!
On Jun 16, Michael Widenius wrote:
>
>
> Hi!
>
> >>>>> "serg" == serg <serg@xxxxxxxxxxxx> writes:
>
> serg> At http://bazaar.launchpad.net/~maria-captains/maria/5.1/
> serg> ------------------------------------------------------------
> serg> revno: 2866
> serg> revision-id: sergii@xxxxxxxxx-20100614091854-5ynq6lo943qlaacw
> serg> parent: monty@xxxxxxxxxxxx-20100613221332-ldsnptg0j0mn8u9a
> serg> committer: Sergei Golubchik <sergii@xxxxxxxxx>
> serg> branch nick: 5.1
> serg> timestamp: Mon 2010-06-14 11:18:54 +0200
> serg> message:
> serg> mysqltest: use setenv, not putenv, to make gcov happy.
> serg> (backport from MySQL)
>
> +static int setenv(const char *name, const char *value, int overwrite)
> +{
> + size_t buflen= strlen(name) + strlen(value) + 2;
> + char *envvar= (char *)malloc(buflen);
> + if(!envvar)
> + return ENOMEM;
> + strcpy(envvar, name);
> + strcat(envvar, "=");
> + strcat(envvar, value);
> + putenv(envvar);
> + return 0;
> +}
> +#endif
>
> I expected better from you :)
It's not from me, it's copied from MySQL.
My code had only
#ifndef HAVE_SETENV
#error implement our portable setenv replacement in mysys
#endif
which passed with flying colors on all PB2 hosts as far as I
remember.
Still, somebody added an "our portable setenv replacement" later,
so apparently some strange systems didn't have a setenv().
> A much better version is:
>
> strcat(strcat(strmov(envvar, name), "="), value);
>
> The other question I have is will this not cause a memory leek?
> If we allocate the same string many times in here it will definitely
> be a memory leak as putenv() will never free the old value.
Yes, it will. But who cares if on some strange system a mysqltest binary
will not free a hundred of bytes explicitly and let them be freed whan
an executable exits ?
Most probably. valgrind isn't even to them.
What *is* important is not to free environment variable values
explicitly on systems where gcov exists - which was the reason for me to
change putenv() to setenv() in the first place.
Regards,
Sergei
References