#913268 pngcrush overwrites a file in the current directory

Package:
pngcrush
Source:
pngcrush
Description:
optimizes PNG (Portable Network Graphics) files
Submitter:
Nicolas George
Date:
2025-11-10 21:45:01 UTC
Severity:
normal
#913268#5
Date:
2018-11-08 20:15:30 UTC
From:
To:
Hi.

When using the -ow option to overwrite the source file with the modified
file, pngcrush creates a temporary file named "pngout.png" in the
current working directory. If the file already exists, it overwrites it.
This is not documented, and could cause data loss. It could even be
considered a security concern.

Also, it causes pngcrush to fail if the output is not in the same
filesystem as the current directory.

Regards,

#913268#10
Date:
2025-11-06 15:17:30 UTC
From:
To:
I am also surprised that a fixed file name is used. Why not calling, in pseudocode, something in the vein of mkstemp(if $TMPDIR exists then $TMPDIR+"/pngoutXXXXXX.png" else "/tmp/pngoutXXXXXX.png") or simply mkstemp("pngoutXXXXXX.png")? It seems to me that mkstemp(…) and its sister functions have more merits than a constant filename.
#913268#15
Date:
2025-11-06 16:03:49 UTC
From:
To:
Upon thinking more, having the temporary file on the same partition as the original file ensures that if the computer crashes, the directory entry for the original file will more likely point to valid data.
So the logic should probably be as follows:
template="pngoutXXXXXX.png.tmp";
if directory_of_the_original_file is writable and has enough space then use directory_of_the_original_file + template as a temporary file
else if $TMPDIR exists, is on the same partition as the original file, is writable, and has enough space then use $TMPDIR + "/"+ template as a temporary file
else if "/tmp" exists, is on the same partition as the original, is writable, and has enough space then use "/tmp/" + template as a temporary file
else if "/var/tmp" exists, is on the same partition as the original, is writable, and has enough space then use "/var/tmp/" + template as a temporary file
else overwrite the original file directly

#913268#20
Date:
2025-11-10 21:42:48 UTC
From:
To:
In particular, notice that I suggested the extension .tmp (or the double extension .png.tmp).
This way, if the computer crashes after pngcrush created this file but not yet cleaned it up, so this file will be left over, the end user or various cleanup programs would know that this file is temporary and can be manually or semi-automatically deleted (in the vein of `find ~ -iname \*.tmp`, followed by `find ~ -iname \*.tmp -delete`).