Oriel — AI & technology worth noticing, every day.

Spooky C++

Web page · https://nablag.com/spooky_cpp

First seen by Oriel on .

AI summary

This Halloween example shows a dangling std::string_view whose undefined behavior changes a permission check from false at -O0 to true at -O2 across C++ standards and compilers.

  • Sanitize returns a std::string_view referring to a temporary string created from the inline literal "foobarbazboo".
  • At -O0, both Clang and GCC print false, while -O2 makes the comparison print true and grants apparent root permissions.
  • The behavior depends on the input matching admin_id’s length and being passed inline; changing the length or storing it temporarily removes it.
  • The author says the bug is difficult to reproduce or exploit, while readability and refactoring principles can eliminate this specific behavior.
  • The example reproduces with clang++-19 and GCC 14.2 under C++17, and also occurs under C++20 and C++23.

#c++ #undefined behavior #string_view #compiler optimization

Generated by gpt-5.6-sol. Check the original for details.

Metric trends

Last 30 days · Up to 120 recorded samples per source and metric, with reposts kept separate · First and latest retained

Points

Score reported by the source.

  • Hacker News: 15, latest observation ; +9 over observed period since
  • Lobsters: 13, latest observation ; +12 over observed period since

Comments

Comment count reported by the source.

  • Hacker News: 14, latest observation ; +13 over observed period since
  • Lobsters: 1, latest observation ; +1 over observed period since

Aliases

SourceExternal IDURL
Hacker News50320513https://www.nablag.com/spooky_cpp Read discussion
Lobsters4yxx6fhttps://www.nablag.com/spooky_cpp Read discussion