Skip to content

[Windows] tmpnam() in runPreprocessor generates root-drive paths, fails with permission denied for non-admin users #22614

@divyansharma001

Description

@divyansharma001

Environment

  • OS: Windows 11, non-admin user
  • Build: dmd debug build, phobos make BUILD=debug

Steps to reproduce

  1. Clone dmd and phobos
  2. Build dmd: rdmd build.d BUILD=debug (from compiler/src/)
  3. Build phobos: make -j4 BUILD=debug (from phobos/)

Error

src\core\stdc\errno.c: fatal error C1083: Cannot open compiler generated file: '\sla8.': Permission denied
Error: C preprocess command ...cl.exe failed for file src\core\stdc\errno.c, exit status 2

Root cause

runPreprocessor() in compiler/src/dmd/link.d:965 calls tmpnam(null) to generate
a temp file name for cl.exe's /Fi output flag. On Windows, MSVC CRT's tmpnam()
always returns paths in the format \sXXXXXX. (at the drive root — e.g. C:\sla8.),
regardless of the TMP/TEMP environment variables. Non-admin users cannot write
to the drive root, so cl.exe fails.

Fix

Replace tmpnam(null) with GetTempPathA + GetTempFileNameA, which correctly
use the user's temp directory:

import core.sys.windows.winbase : GetTempFileNameA, GetTempPathA;
char[260] tempDir = void;
char[260] tempFile = void;
GetTempPathA(260, tempDir.ptr);
GetTempFileNameA(tempDir.ptr, "dmd", 0, tempFile.ptr);
const(char)[] ifilename = tempFile[0 .. strlen(tempFile.ptr) + 1];

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