Examples of Undefined Behavior Related to Type Accessibility in C++

From C++ standard:

Attempting to read or modify the stored value through any other type results in undefined behavior.

The first basic example illustrating this principle:

#include <cstdint>

std::uint32_t toInt(float value)
{
    auto* p = reinterpret_cast<std::uint32_t*>(&value); // The cast itself is permitted.
    return *p; // Undefined behavior: accesses a float object through a std::uint32_t glvalue.
}
(more…)

Violating strict aliasing with reinterpret_cast in C++

The first simple example:

#include <bit>
#include <cstdint>
#include <iomanip>
#include <iostream>

std::uint32_t badBits(const float value)
{
  // const prevents writes, but reading a float through a uint32_t pointer
  // still violates the strict-aliasing rule.
  const auto* bits = reinterpret_cast<const std::uint32_t*>(&value);
  return *bits; // Undefined behavior.
}

std::uint32_t goodBits(const float value)
{
  // std::bit_cast copies the object representation without violating
  // strict-aliasing or object-lifetime rules.
  return std::bit_cast<std::uint32_t>(value);
}

int main()
{
  constexpr float value = 1.0F;

  std::cout << std::hex
      << "bad:  0x" << badBits(value) << '\n'
      << "good: 0x" << goodBits(value) << '\n';
}
(more…)

Show RDP connections with credentials in PowerShell

Get-ChildItem `
'HKCU:\Software\Microsoft\Terminal Server Client\Servers' |
ForEach-Object {
    $p = Get-ItemProperty $_.PSPath

    [PSCustomObject]@{
        Server   = $_.PSChildName
        Username = $p.UsernameHint
    }
}
(more…)

Override one virtual function multiple times in C++

The code below does not compile in C++:

template <class Result, class... Params>
class Slot
{
public:

    virtual Result operator()(Params ... args) = 0;
};
(more…)

Debugging Credential Provider with Visual Studio 2022 Remote Debugger

I installed Remote Tools for Visual Studio 2022 and run:

(more…)

Investigating Credential Providers on Windows

I registered a sample Credential Provider with the following .reg file:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\Credential Providers\{5fd3d285-0dd9-4362-8855-e0abaacd4af6}]
@="SampleV2CredentialProvider"

[HKEY_CLASSES_ROOT\CLSID\{5fd3d285-0dd9-4362-8855-e0abaacd4af6}]
@="SampleV2CredentialProvider"

[HKEY_CLASSES_ROOT\CLSID\{5fd3d285-0dd9-4362-8855-e0abaacd4af6}\InprocServer32]
@="SampleV2CredentialProvider.dll"
"ThreadingModel"="Apartment"
(more…)

Generating Events 8004 and 8005 in Windows Logs

On DC with IP address 192.168.0.123:

wevtutil sl Microsoft-Windows-NTLM/Operational /e:true
wevtutil qe Microsoft-Windows-NTLM/Operational /q:"*[System[(EventID=8004 or EventID=8005)]]" /f:text
net share
Share name   Resource                        Remark

-------------------------------------------------------------------------------
C$           C:\                             Default share
IPC$                                         Remote IPC
ADMIN$       C:\Windows                      Remote Admin
NETLOGON     C:\Windows\SYSVOL\sysvol\my.local\SCRIPTS
                                             Logon server share
SYSVOL       C:\Windows\SYSVOL\sysvol        Logon server share
The command completed successfully.
(more…)

Enabling Debug Visualizers in MS Visual Studio

Tools->Options:

(more…)

Installing LDAPS certificate on Windows 10

I realized that my LDAPS certificate is not trusted with the following command in PowerShell:

certutil -verify ldap.crt
(more…)

Why do we pass parameters to coroutines by value in C++?

C++ coroutines and const reference parameters

A coroutine func accepts a parameter by const reference in the code below:

#include <boost/asio.hpp>

#include <iostream>

namespace asio = boost::asio;
using asio::awaitable;
using asio::use_awaitable;

class Param
{
public:

    Param(int val) : m_val(val)
    {
        std::cout << "Param constructor " << m_val << std::endl;
    }
(more…)