SecuritylabJuly 31, 2026🇷🇺Translated from Russian

Dangerous C++ Traps: Memory Safety Issues, Undefined Behavior, and Code That Betrays Developers

Around 70% of vulnerabilities assigned CVE numbers by Microsoft each year are linked to memory safety errors. The Chromium project reported nearly identical statistics for serious Chrome bugs: roughly 70% arose from unsafe memory handling, with half of those defects involving use-after-free. Decades after widespread adoption of C and C++, the industry continues to encounter the same pitfalls, now embedded in browsers, servers, and firmware updates.

Undefined Behavior: When the Program Promises Nothing

Undefined behavior (UB) means the language imposes no requirements on program results once rules are violated. Compilers need not warn, processes need not crash, and output may be expected values, garbage, leaked secrets, or entirely different control flow after optimization. A classic example shows how an optimizer can assume signed overflow never occurs:

bool grows_after_increment(int value) {
    return value + 1 > value;
    // UB at INT_MAX allows optimizer to return true always
}

Use-After-Free: Address Exists, Object Does Not

Memory and objects are distinct in C++. Returning memory to the allocator while retaining a pointer creates use-after-free (CWE-416). std::unique_ptr reduces risk by expressing unique ownership, yet extracting a raw pointer via get() can still produce dangling references after the owner is destroyed.

Heartbleed: How a Separate Length Field Leaked Server Memory

In April 2014, Heartbleed (CVE-2014-0160) in OpenSSL demonstrated the cost of trusting a client-supplied length. The vulnerable heartbeat handler copied up to 64 KB without verifying the actual payload size, exposing session secrets, passwords, and private keys. The flaw was a read beyond bounds that often left the server running while silently leaking memory.

Buffer Overflows, Integer Overflows, and Dangling Views

Simple off-by-one errors using <= instead of < write past array ends. Integer overflow before allocation can produce a tiny buffer followed by a large memcpy. std::string_view and std::span store pointers and lengths without ownership; when the original string or vector is destroyed or reallocated, the view becomes dangling. C++20 and C++23 std::span lack an at() method; bounds-checked access arrived only in the technically complete C++26 via proposal P2821R5.

Data Races, Move Semantics, and Uninitialized Values

Concurrent unsynchronized writes to non-atomic objects produce data races classified as UB. After std::move, standard-library objects remain valid but hold unspecified values; relying on any particular state is a logic error. C++26 proposal P2795R5 introduces “erroneous behavior” for some reads of uninitialized automatic variables, giving compilers better diagnostic options without full UB freedom.

Dynamic Analysis Tools and Compiler Warnings

Recommended instrumented builds include:

  • AddressSanitizer (ASan) — detects out-of-bounds access and use-after-free (roughly 2× slowdown)
  • UndefinedBehaviorSanitizer (UBSan) — catches signed overflow and alignment issues
  • MemorySanitizer (MSan) — finds reads of uninitialized data
  • ThreadSanitizer (TSan) — identifies data races (5–15× slowdown)

Strict warning sets with -Wall -Wextra -Wpedantic -Wconversion -Wshadow plus -Wreturn-stack-address (Clang) or -fanalyzer (GCC) catch many issues at compile time. New projects should treat warnings as errors; legacy codebases benefit from freezing existing debt and enforcing clean modules for untrusted input.

Migration Advice and When to Choose Another Language

Teams should begin at trust boundaries: network packet handlers, file parsers, and decoders. Ownership should move to containers or std::unique_ptr following the rule of zero. For new services handling untrusted data without legacy constraints, memory-safe languages such as Rust eliminate entire classes of lifetime and ownership defects before compilation.

Related articles

BoletimSecVulnerabilities & Exploits

Critical CosmosEscape Flaw Breaks Customer Isolation in Azure Cosmos DB

A critical vulnerability named CosmosEscape allowed attackers to escape the restricted Gremlin API environment and execute arbitrary code on the DB Gateway component of Azure Cosmos DB. The flaw exploited weaknesses in .NET reflection protections, enabling file read/write operations and command execution that ultimately yielded the Cosmos Master Key. With this global signing key, an attacker could retrieve the primary key for any customer account and gain full read/write access. The exploit also exposed the Config Store containing account names, subscription identifiers, tenant details, and network rules. Even network-isolated and private instances remained vulnerable because the DB Gateway itself enforced those restrictions. Internal Microsoft services including Entra ID, Teams, and Copilot rely on Cosmos DB, amplifying the potential impact of the issue.

Security NEXTVulnerabilities & Exploits

Critical RCE Vulnerability CVE-2026-66066 Affects Ruby on Rails Active Storage with libvips

A severe vulnerability tracked as CVE-2026-66066 has been identified in the Ruby on Rails web application framework. The flaw, also referred to as KindaRails2Shell by researchers, impacts applications that use Active Storage with the libvips image processing engine. Attackers can exploit the issue by uploading specially crafted files to read arbitrary files without authentication. This exposure may lead to theft of environment variables, secret keys, and external service credentials, enabling remote code execution or further attacks. The vulnerability stems from unsafe operations in the dependent libvips library. It carries a CVSS v4.0 base score of 9.5 and is rated Critical. JPCERT/CC has issued an advisory urging immediate updates.

Security NEXTVulnerabilities & Exploits

Critical Vulnerabilities Disclosed in Adobe Campaign Classic Require Immediate Patching

Adobe has released a security advisory detailing two high-risk vulnerabilities affecting Adobe Campaign Classic on-premises deployments on Windows and Linux. CVE-2026-48449 is an improper authorization flaw that permits remote attackers to execute arbitrary code without authentication and carries a maximum CVSSv3.1 base score of 10.0. CVE-2026-48448 is a SQL injection vulnerability that allows unauthenticated attackers to read arbitrary files from the file system, rated at 8.6. Both issues affect the campaign management platform used by organizations for marketing automation. Adobe urges administrators to apply the available security updates without delay due to the elevated risk of exploitation.

HabrVulnerabilities & Exploits

Secure Error Logging Practices to Prevent Information Leaks Across Java, Kotlin, JavaScript and Python

The article examines how improper error logging can expose sensitive details such as stack traces, file paths, library versions and database structures, enabling attackers to map applications and craft targeted exploits. It covers core logging levels from DEBUG to FATAL, mechanisms including text files, binary logs and databases, plus the roles of Trace ID and Correlation ID in tracing requests across microservices. Real-world vulnerable code examples in Flask and SQLite demonstrate direct exception output, manual traceback exposure and SQL error leakage that confirm technologies like Python 3.10 or SQLite usage. CWE categories including CWE-209, CWE-532, CWE-538 and CWE-1295 are referenced to classify risks of information disclosure through logs. Mitigation steps include stripping version headers in Nginx, sanitizing inputs with regular expressions, using OpenTelemetry for structured JSON logging and avoiding debug modes in production. The guidance stresses balancing detailed logs for incident response with protections against injection and reconnaissance.