[PATCH] V2 mktemp: fix -u option if TMPDIR is set

Tito farmatito at tiscali.it
Thu Sep 20 13:42:20 UTC 2012


On Thursday 20 September 2012 13:31:55 Ron Yorston wrote:
> With the -u option mktemp uses tempnam to generate the temporary file
> name.  This mostly works as required, but the TMPDIR environment variable
> trumps any path supplied to tempnam.  If TMPDIR is set unexpected behaviour
> ensues:
> 
>    $ mktemp -u temp.XXXXXX
>    temp.4qYDxk
>    $ mktemp -u -t temp.XXXXXX
>    /tmp/temp.8gP6Pq
>    $ export TMPDIR=/tmp
>    $ mktemp -u temp.XXXXXX
>    mp/temp.BuEBHM
> 
> Signed-off-by: Ron Yorston <rmy at tigress.co.uk>
> ---
>  debianutils/mktemp.c |    1 +
>  1 files changed, 1 insertions(+), 0 deletions(-)
> 
> diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c
> index dbe4309..b6833af 100644
> --- a/debianutils/mktemp.c
> +++ b/debianutils/mktemp.c
> @@ -91,6 +91,7 @@ int mktemp_main(int argc UNUSED_PARAM, char **argv)
>  		while (--cnt >= 0 && chp[--len] == 'X')
>  			chp[len] = '\0';
>  
> +		unsetenv("TMPDIR");
>  		chp = tempnam(opts & (OPT_t|OPT_p) ? path : "./", chp);
>  		if (!chp)
>  			return EXIT_FAILURE;
> 

Hi,
a cleaner fix without unsetting env vars could be:

--- debianutils/mktemp.c.original       2012-09-20 15:32:32.000000000 +0200
+++ debianutils/mktemp.c        2012-09-20 15:34:47.000000000 +0200
@@ -95,7 +95,7 @@
                if (!chp)
                        return EXIT_FAILURE;
                if (!(opts & (OPT_t|OPT_p)))
-                       chp += 2;
+                       chp = bb_get_last_path_component_strip(chp);
                goto ret;
        }

This seems to work for me as expected eventually needs more testing.

Ciao,
Tito
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mkstemp.patch
Type: text/x-patch
Size: 450 bytes
Desc: not available
URL: <http://lists.busybox.net/pipermail/busybox/attachments/20120920/c4221b26/attachment.bin>


More information about the busybox mailing list