Repository: dotnet/aspnetcore
File: src/Servers/IIS/AspNetCoreModuleV2/CommonLib/HostFxrResolver.cpp (C++)

Code before:
    // Seek to the PE header
    file.seekg(dosHeader.e_lfanew, std::ios::beg);

    // Read the PE header
    DWORD peSignature{};
    file.read(reinterpret_cast<char*>(&peSignature), sizeof(peSignature));
    if (peSignature != IMAGE_NT_SIGNATURE) // 'PE\0\0'
    {
        LOG_TRACEF(L"%ls is not a valid PE file (missing PE header).", dotnetPath);
        return false;
    }

    // Read the file header
    IMAGE_FILE_HEADER fileHeader{};
    file.read(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader));

    // Read the optional header magic field
    WORD magic{};
    file.read(reinterpret_cast<char*>(&magic), sizeof(magic));

    // Determine the architecture based on the magic value
    if (magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
    {
        LOG_INFOF(L"%ls is 32-bit", dotnetPath);
        return false;
    }
    else if (magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
    {
        LOG_INFOF(L"%ls is 64-bit", dotnetPath);
        return true;
    }

    LOG_INFOF(L"%ls is unknown architecture %i", dotnetPath, fileHeader.Machine);
    return false;
}

std::optional<fs::path>
HostFxrResolver::GetAbsolutePathToDotnetFromProgramFiles()
{
    const auto programFilesDotnet = fs::path(Environment::ExpandEnvironmentVariables(L"%ProgramFiles%")) / "dotnet" / "dotnet.exe";
    return is_regular_file(programFilesDotnet) ? std::make_optional(programFilesDotnet) : std::nullopt;
}


Review comment (suggestion):
Hmm, if I understand docs correctly, then there are only 3 valid combinations -- 
if IMAGE_FILE_MACHINE_ARM64 then magic should be IMAGE_NT_OPTIONAL_HDR64_MAGIC 
if IMAGE_NT_OPTIONAL_HDR64_MAGIC  then magic should be IMAGE_NT_OPTIONAL_HDR64_MAGIC 
if IMAGE_FILE_MACHINE_I386 then magic should be IMAGE_NT_OPTIONAL_HDR32_MAGIC

assuming that's true, would it be worth asserting magic in these various cases, to record our understanding? given the confusion that we've already had here

Code after:
    if (dosHeader.e_magic != IMAGE_DOS_SIGNATURE) // 'MZ'
    {
        LOG_TRACEF(L"%ls is not a valid executable file (missing MZ header).", binaryPath);
        return ProcessorArchitecture::Unknown;
    }

    // Seek to the PE header
    file.seekg(dosHeader.e_lfanew, std::ios::beg);

    // Read the PE header
    DWORD peSignature{};
    file.read(reinterpret_cast<char*>(&peSignature), sizeof(peSignature));
    if (peSignature != IMAGE_NT_SIGNATURE) // 'PE\0\0'
    {
        LOG_TRACEF(L"%ls is not a valid PE file (missing PE header).", binaryPath);
        return ProcessorArchitecture::Unknown;
    }

    // Read the file header
    IMAGE_FILE_HEADER fileHeader{};
    file.read(reinterpret_cast<char*>(&fileHeader), sizeof(fileHeader));

    // Determine the architecture based on the machine type
    switch (fileHeader.Machine)
    {
        case IMAGE_FILE_MACHINE_I386:
            LOG_INFOF(L"%ls is x86 (32-bit)", binaryPath);
            return ProcessorArchitecture::x86;
        case IMAGE_FILE_MACHINE_AMD64:
            LOG_INFOF(L"%ls is AMD64 (x64)", binaryPath);
            return ProcessorArchitecture::AMD64;
        case IMAGE_FILE_MACHINE_ARM64:
            LOG_INFOF(L"%ls is ARM64", binaryPath);
            return ProcessorArchitecture::ARM64;
        default:
            LOG_INFOF(L"%ls has unknown architecture (machine type: 0x%X)", binaryPath, fileHeader.Machine);
            return ProcessorArchitecture::Unknown;
    }
}

std::optional<fs::path>
HostFxrResolver::GetAbsolutePathToDotnetFromProgramFiles()
{
    const auto programFilesDotnet = fs::path(Environment::ExpandEnvironmentVariables(L"%ProgramFiles%")) / "dotnet" / "dotnet.exe";
    return is_regular_file(programFilesDotnet) ? std::make_optional(programFilesDotnet) : std::nullopt;
}