News for hackers. Stuff that matters.
Re:C++26: A User-Friednly assert() macro by maccard on Saturday March 28, 2026 @02:45PM (#47557212)
Indeed.

   bool is_even(int* valPtr) {
      assert(valPtr != nullptr);
      return *valPtr % 2;
    }
Does not do what you think it does with nullptr. A major game engine [0] has a toggle to enable asserts in shipping builds, mostly for this reason

[0] https://dev.epicgames.com/documentation/en-us/unreal-engine/...

Re:C++26: A User-Friednly assert() macro by mhh__ on Saturday March 28, 2026 @03:42PM (#47557603)
This is a very "Dr Dr it hurts when I do this" "Don't do that" one it must be said.
Re:C++26: A User-Friednly assert() macro by dccsillag on Saturday March 28, 2026 @02:59PM (#47557309)
I'm sorry, but what exactly is the problem with the code? I've been staring at it for quite a while now and still don't see what is counterintuitive about it.
Re:C++26: A User-Friednly assert() macro by IshKebab on Saturday March 28, 2026 @03:12PM (#47557388)
There's nothing wrong with it. It does exactly what you think it does when passed null.
Re:C++26: A User-Friednly assert() macro by jmalicki on Sunday March 29, 2026 @12:04AM (#47560304)
A lot of compilers will optimize out a NULL pointer check because dereferencing a NULL pointer is UB.

Because assert will not run the following code in the case of a NULL pointer, AFAIK this exact code is still defined behavior, but if for some reason some code dereferenced the NULL pointer before, it would be optimized out - there are some corner cases that aren't obvious on the surface.

This kind of thing was always theoretically allowed, but really started to become insidious within the past 5-10 years. It's probably one of the more surprising UB things that bites people in the field.

GCC has a flag "-fno-delete-null-pointer-checks" to specifically turn off this behavior.

https://qinsb.blogspot.com/2018/03/ub-will-delete-your-null-...

This is an actual Linux kernel exploit caused by this behavior where the compiler optimized out code that checked for a NULL pointer and returned an error.

https://lwn.net/Articles/342330/

Re:C++26: A User-Friednly assert() macro by IshKebab on Sunday March 29, 2026 @04:25AM (#47561333)
Sure, but none of that is relevant to just the code snippet that was posted. The compiler can exploit UB in other code to do weird things, but that's just C being C. There's nothing unexpected in the snippet posted.

The issue is cause by C declaring that dereferencing a null pointer is UB. It's not really an issue with assertions.

You can get the same optimisation-removes-code for any UB.

Re:C++26: A User-Friednly assert() macro by maccard on Sunday March 29, 2026 @05:53AM (#47561744)
> There's nothing unexpected in the snippet posted.

> The issue is cause by C declaring that dereferencing a null pointer is UB. It's not really an issue with assertions. > You can get the same optimisation-removes-code for any UB.

I disagree - It’s a 4 line toy example but in a 30-40 line function these things are not always clear. The actual problem is if you compile with NDEBUG=1, the nullptr check is removed and the optimiser can (and will, currently) do unexpected things.

The printf sample above is a good example of the side effects.

Re:C++26: A User-Friednly assert() macro by IshKebab on Sunday March 29, 2026 @08:05AM (#47562442)
> The actual problem is if you compile with NDEBUG=1

That is entirely expected by any C programmer. Sure they named things wrong - it should have been something like `assert` (always enabled) and `debug_assert` (controlled by NDEBUG), as Rust did. And I have actually done that in my C++ code before.

But I don't think the mere fact that assertions can be disabled was the issue that was being alluded to.

Re:C++26: A User-Friednly assert() macro by maccard on Sunday March 29, 2026 @09:39AM (#47563060)
I wrote the comment, assertions being disabled was exactly what was being alluded to.

> that is entirely expected by any C programmer

That’s great. Every C programmer also knows to avoid all the footguns and nasties - yet we still have issues like this come up all the time. I’ve worked as a C++ programmer for 12 years and I’d say it’s probably 50/50 in practice how many people would spot that in a code review.

Re:C++26: A User-Friednly assert() macro by IshKebab on Sunday March 29, 2026 @12:41PM (#47564749)
It's definitely a footgun, but the compiler isn't doing weird stuff because the assertions can be disabled. It's doing weird stuff because there's UB all over the place and it expects programmers to magically not make any mistakes. Completely orthogonal to this particular (fairly minor IMO) footgun.

> I’ve worked as a C++ programmer for 12 years and I’d say it’s probably 50/50 in practice how many people would spot that in a code review.

Spot what? There's absolutely nothing wrong with the code you posted.

Re:C++26: A User-Friednly assert() macro by jmalicki on Sunday March 29, 2026 @02:49PM (#47565954)
That assert could completely fail to fire if inlined into another function that did a dereference first.
Re:C++26: A User-Friednly assert() macro by dataflow on Saturday March 28, 2026 @06:03PM (#47558512)
Depends on where you're coming from, but some people would expect it to enforce that the pointer is non-null, then proceed. Which would actually give you a guaranteed crash in case it is null. But that's not what it does in C++, and I could see it not being entirely obvious.
Re:C++26: A User-Friednly assert() macro by IshKebab on Saturday March 28, 2026 @06:55PM (#47558803)
Assert doesn't work like that in any language.
Re:C++26: A User-Friednly assert() macro by comex on Saturday March 28, 2026 @07:02PM (#47558836)
It does in Rust: assert is always enabled, whereas the debug-only version is called debug_assert.

But yes, “assert” in most languages is debug-only.

Re:C++26: A User-Friednly assert() macro by IshKebab on Sunday March 29, 2026 @04:18AM (#47561292)
He said

> some people would expect it to enforce that the pointer is non-null, then proceed

No language magically makes the pointer non-null and then continues. I don't even know what that would mean.

Re:C++26: A User-Friednly assert() macro by dataflow on Tuesday March 31, 2026 @07:18PM (#47594746)
If you don't even know what that would mean then it's premature to declare that nothing works that way. Understanding the meaning is a prerequisite for that.

In this case, it may help to understand that e.g. border control enforces a traveler's permission to cross the border, then lets them proceed.

Re:C++26: A User-Friednly assert() macro by secondcoming on Saturday March 28, 2026 @05:05PM (#47558113)
Let's not vague post on HN. What's the problem with the above?
Re:C++26: A User-Friednly assert() macro by saagarjha on Saturday March 28, 2026 @08:34PM (#47559363)
The problem is the code unconditionally dereferences the pointer, which would be UB if it was a null pointer. This means it is legal to optimize out any code paths that rely on this, even if they occur earlier in program order.
Re:C++26: A User-Friednly assert() macro by aw1621107 on Saturday March 28, 2026 @08:46PM (#47559439)
> The problem is the code unconditionally dereferences the pointer, which would be UB if it was a null pointer.

Only when NDEBUG is defined, right?

Re:C++26: A User-Friednly assert() macro by saagarjha on Saturday March 28, 2026 @09:03PM (#47559528)
No, the code that does this is always active
Re:C++26: A User-Friednly assert() macro by gottheUIblues on Saturday March 28, 2026 @09:20PM (#47559605)
Right so strictly speaking C++ could do anything here when passed a null pointer, because even though assert terminates the program, the C++ compiler cannot see that, and there is then undefined behaviour in that case
Re:C++26: A User-Friednly assert() macro by aw1621107 on Sunday March 29, 2026 @04:06AM (#47561248)
> because even though assert terminates the program, the C++ compiler cannot see that

I think it should be able to. I'm pretty sure assert is defined to call abort when triggered and abort is tagged with [[noreturn]], so the compiler knows control flow isn't coming back.

Re:C++26: A User-Friednly assert() macro by aw1621107 on Sunday March 29, 2026 @04:03AM (#47561237)
Shouldn't control flow diverge if the assert is triggered when NDEBUG is not defined? Pretty sure assert is defined to call abort when triggered and that is tagged [[noreturn]].
Re:C++26: A User-Friednly assert() macro by saagarjha on Sunday March 29, 2026 @04:19AM (#47561301)
Sorry, yes, I misread you
Re:C++26: A User-Friednly assert() macro by pwdisswordfishy on Sunday March 29, 2026 @02:44AM (#47560903)
But if the assertion fails, the program is aborted before the pointer would have been dereferenced, making it not UB. This explanation is bogus.
Re:C++26: A User-Friednly assert() macro by saagarjha on Sunday March 29, 2026 @04:20AM (#47561306)
Only if the assert is active. It basically means that the code is invalid when NDEBUG is set.
Re:C++26: A User-Friednly assert() macro by quuxplusone on Sunday March 29, 2026 @10:02AM (#47563229)
When NDEBUG is set, there is no test, no assertion, at all. So yes, this code has UB if you set NDEBUG and then pass it a null pointer — but that's obvious. The code does exactly what it looks like it does; there's no tricks or time travel hiding here.
Re:C++26: A User-Friednly assert() macro by teo_zero on Sunday March 29, 2026 @03:23AM (#47561066)
> it is legal to optimize out any code paths that rely on this, even if they occur earlier in program order.

I don't think this is true. The compiler cannot remove or reorder instructions that have a visible effect.

  if (p == 0)
    printf("Ready?\n");
  *p++;
The printf() can't be omitted.
Re:C++26: A User-Friednly assert() macro by saagarjha on Sunday March 29, 2026 @04:18AM (#47561291)
No, this is explicitly legal. Most compilers will shy away from it these days since it made a lot of people upset, but it's definitely allowed.
Re:C++26: A User-Friednly assert() macro by aw1621107 on Sunday March 29, 2026 @04:13AM (#47561275)
> The compiler cannot remove or reorder instructions that have a visible effect.

You might be surprised! When it comes to UB compilers can and do reorder/eliminate instructions with side effects, resulting in "time travel" [0].

IIRC the upcoming version of the C standard bans this behavior, but the C++ standard still allows it (for now, at least).

[0]: https://devblogs.microsoft.com/oldnewthing/20140627-00/?p=63...