Skip to content

ModelicaInternal_temporaryFileName: Replace tmpnam with mkstemp #4794

Description

@AnHeuermann

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions