Issue
Function tmpnam in ModelicaInternal.c#L760 is so notoriously unsafe that the C standard warns against it:
libModelicaExternalC.a(ModelicaInternal.c.o): in function `ModelicaInternal_temporaryFileName':
/path/to/ModelicaExternalC/C-Sources/ModelicaInternal.c:682:(.text+0x8cd): warning: the use of `tmpnam' is dangerous, better use `mkstemp'
Suggested Changes
Use a safer alternative
- POSIX (all Unix-like systems):
mkstemp
- Windows:
GetTempFileNameA
That guarantees atomic operations, uniqueness and honors temp directories set by TEMPDIR.
With the POSIX variant the right permissions are set (0600), since the data lives in a shared directory (/tmp).
On Windows usually the %LOCALAPPDATA%\Temp directory is used, which is already only readable by the user, or use an explicit ACL/DACL restrictor.
That would slightly change the behavior to do unique name generation and file generation in one atomic step instead of returning a name to a non-existing temp file (which is precisely the root issue of tmpnam).
If it's fine for ModelicaInternal_temporaryFileName to generate the files directly I can provide a pull request.
Deprecate ModelicaInternal_temporaryFile
If it's not okay that the behavior would change I would suggest to add a new internal function ModelicaInternal_temporaryFile and deprecate ModelicaInternal_temporaryFileName in favor of it and remove it in a future version.
Related issues
See also #3647.
Issue
Function
tmpnamin ModelicaInternal.c#L760 is so notoriously unsafe that the C standard warns against it:Suggested Changes
Use a safer alternative
mkstempGetTempFileNameAThat guarantees atomic operations, uniqueness and honors temp directories set by
TEMPDIR.With the POSIX variant the right permissions are set (
0600), since the data lives in a shared directory (/tmp).On Windows usually the
%LOCALAPPDATA%\Tempdirectory is used, which is already only readable by the user, or use an explicit ACL/DACL restrictor.That would slightly change the behavior to do unique name generation and file generation in one atomic step instead of returning a name to a non-existing temp file (which is precisely the root issue of
tmpnam).If it's fine for
ModelicaInternal_temporaryFileNameto generate the files directly I can provide a pull request.Deprecate
ModelicaInternal_temporaryFileIf it's not okay that the behavior would change I would suggest to add a new internal function
ModelicaInternal_temporaryFileand deprecateModelicaInternal_temporaryFileNamein favor of it and remove it in a future version.Related issues
See also #3647.