bugGNU Octave - Bugs: bug #43844, tmpnam() ignores dir parameter if...

 
 

bug #43844: tmpnam() ignores dir parameter if systems temporary dir environment variable is set.

Submitter:  sergey plotnikov <nul0m>
Submitted:  Thu 18 Dec 2014 02:06:44 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Mon 22 Dec 2014 05:05:43 AM UTC, comment #8: 

I checked in a change that fixes tempname and adds BIST tests which should work on both UNIX and Windows (http://hg.savannah.gnu.org/hgweb/octave/rev/82f2a3437e02).  Closing report.

Rik <rik5>
Group administrator
Mon 22 Dec 2014 01:31:59 AM UTC, comment #7: 

I meant tempname in matlab [1]

According to the docs for matlab, it doesn't accept any arguments, however looking at the .m file, it does.


[1] http://www.mathworks.com/help/matlab/ref/tempname.html

John Donoghue <lostbard>
Group Member
Sun 21 Dec 2014 05:41:59 PM UTC, comment #6: 

Aaach, thanks for checking the patch.  I had a feeling I might get something wrong here so I didn't commit it immediately.  I'll take a look at the best way to handle a null prefix.

Rik <rik5>
Group administrator
Sun 21 Dec 2014 04:21:35 PM UTC, comment #5: 

I looked back to when the tmpnam function was added to Octave, and it looks like there was never a time when it was a simple wrapper for the C library tmpnam function.  Instead it called tempnam.  I don't know why exactly.  Then sometime later I assume the handling of the DIR and PREFIX arguments was added.

Rik, I agree that it is probably easier to just rewrite the file name.  I tried your patch and now if I try someething like tmpnam  ("/home", ""), the result is "/home/".  Without the patch it was something like "/home/fileFH6WIK".  Was that intentional?


John W. Eaton <jwe>
Group administrator
Sun 21 Dec 2014 03:35:51 PM UTC, comment #4: 

Matlab doesn't have the tmpnam function so I don't think Matlab can be doing this correctly as implied by "Matlab just uses whatever is provided as a dir name."

The root cause analysis is correct that Octave is using the C library function tempnam.  'man tempnam' says:


NAME
       tempnam - create a name for a temporary file

SYNOPSIS
       #include <stdio.h>

       char *tempnam(const char *dir, const char *pfx);

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       tempnam(): _BSD_SOURCE || _SVID_SOURCE

DESCRIPTION
       The  tempnam()  function  returns a pointer to a string that is a valid filename, and such
       that a file with this name did not exist when tempnam() checked.  The filename  suffix  of
       the  pathname  generated  will  start with pfx in case pfx is a non-NULL string of at most
       five bytes.  The directory prefix part of the pathname generated is required to be "appro‐
       priate" (often that at least implies writable).

       Attempts to find an appropriate directory go through the following steps:

       a) In  case the environment variable TMPDIR exists and contains the name of an appropriate
          directory, that is used.

       b) Otherwise, if the dir argument is non-NULL and appropriate, it is used.

       c) Otherwise, P_tmpdir (as defined in <stdio.h>) is used when appropriate.

       d) Finally an implementation-defined directory may be used.


So it is pretty clear why this is happening.  The code for this is in function octave_tempnam is liboctave/system/file-ops.cc at line 674.

I've attached a patch which seems to work.  Another solution would be to temporarily unset the environment variable TMPDIR ahead of calling tmpnam.  But I think the code for that gets a little harder because one has to figure out which platform is active and whether the environment variable is TMP or TMPDIR.





(file #32697)

Rik <rik5>
Group administrator
Fri 19 Dec 2014 01:35:22 PM UTC, comment #3: 

So it is a bug in all OS.

Rewrote summary to reflect full bug and set OS to any.

tempname and tmpnam ignore the DIR parameter if the OS specific temporty dir environment variable is set.
In windows: TMP
In Linux: TMPDIR

Matlab just uses whatever is provided as a dir name.

John Donoghue <lostbard>
Group Member
Fri 19 Dec 2014 01:09:02 PM UTC, comment #2: 

Use of TMP is for windows - the Linux version behaves the same if the TMPDIR environment variable it set.
 
On my Linux system, TMPDIR is not set by default. On my windows machine TMP is set.


John Donoghue <lostbard>
Group Member
Thu 18 Dec 2014 06:18:28 PM UTC, comment #1: 

octave uses the systems tempnam function which says it uses the dir if environment variable TMP is not set.

On my windows machine:

>> pwd
ans = C:\Users\john
>> getenv('TMP')
ans = C:\Users\john\AppData\Local\Temp
>> tempname(pwd())
ans = C:\Users\john\AppData\Local\Temp\oct-2
>> setenv('TMP','')
>> tempname(pwd())
ans = C:\Users\john\oct-3
>>

I tried on Matlab 2013, and it uses the input name, regardless of whether it is a valid directory, character etc.

John Donoghue <lostbard>
Group Member
Thu 18 Dec 2014 02:06:44 PM UTC, original submission:  

on Windows machines tmpnam() is not able to construct the name using user-defined directory.


>> tempname(pwd())
ans = C:\Users\myusername\AppData\Local\Temp\oct-2


sergey plotnikov <nul0m>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #32697:  tmpnam.cset added by rik5 (2KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by rik5 (Updated the item)
  • -email is unavailable- added by avinoam
  • -email is unavailable- added by lostbard (Posted a comment)
  • -email is unavailable- added by nul0m (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2014-12-22 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2014-12-21 rik5 Attached File- Added tmpnam.cset, #32697
        StatusConfirmed Patch Submitted
    2014-12-19 lostbard StatusNone Confirmed
        Operating SystemMicrosoft Windows Any
        Summarytmpnam() always uses default directory for temporary files on Windows tmpnam() ignores dir parameter if systems temporary dir environment variable is set.
    2014-12-18 avinoam Carbon-Copy- Added avinoam

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code