JP2: JasPer uses tmpnam(), which is broken on Windows

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
sacha
Posts: 17
Joined: 2012-04-12T14:37:19-07:00
Authentication code: 8675308

JP2: JasPer uses tmpnam(), which is broken on Windows

Post by sacha »

This is not an IM bug, it's a JasPer bug, but nonetheless read on. FWIW I observe this with IM 6.7.7.

I've been debugging a puzzling permissions-related issue relating to decoding JPEG2000 images, and it seems to be this line in JasPer's jas_stream.c that's the culprit:

/* Choose a file name. */
tmpnam(obj->pathname);

Apaprently, on Windows, tmpnam() always returns a temporary filename in the root directory of the current drive (!), which therefore fails for various users who can't write to the root. (I saw these files being created in my process trace, and after some googling came across this: http://docs.python.org/library/os.html#os.tmpnam)

Various distributions patch around this tmpnam() for other reasons:

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=506739

https://bugzilla.redhat.com/show_bug.cgi?id=461477

I've reported this to the JasPer maintainer, but perhaps IM ought to patch it too in the meantime? (Ideally, it would use MAGICK_TMPDIR.)
sacha
Posts: 17
Joined: 2012-04-12T14:37:19-07:00
Authentication code: 8675308

Re: JP2: JasPer uses tmpnam(), which is broken on Windows

Post by sacha »

Some further info:

1) The semantics of tmpnam() on Windows are odd, in that if it returns a filename beginning with a \, that indicates that the file is unique to the current working directory. This is probably what is happening, but the Jasper code is not expecting it, and so ends up creating temporary files at the root of the filesystem. http://msdn.microsoft.com/en-us/library/hs3e7355.aspx

2) Jasper uses temporary files when it detects that the decompressed image is going to be >16MB.
Post Reply