llvm-mos / llvm-mos-sdk

SDK for developing with the llvm-mos compiler
https://www.llvm-mos.org
Other
283 stars 56 forks source link

Filename allocation problems in freopen #356

Closed pfusik closed 3 months ago

pfusik commented 3 months ago
  1. filename = stream->filename; leaks.
  2. free(stream->filename); double free.
  3. strcpy(stream->filename, filename); null-dereference on OOM (also in fopen).
pfusik commented 3 months ago

cppreference says:

If filename is a null pointer, then the function attempts to reopen the file that is already associated with stream (it is implementation defined which mode changes are allowed in this case). Microsoft CRT version of freopen does not support any mode changes when filename is a null pointer and treats this as an error

If that's good enough for Microsoft CRT, I suggest to make it simple and only store the filename for DELONCLOSE/tmpfile. At least on the Atari, you can delete a file by its name, but not a handle.