From da56ad235630b25a2d4332a310e9b0f65277ab59 Mon Sep 17 00:00:00 2001 From: "Enzo Persillet (Tutez)" Date: Wed, 2 Dec 2026 14:02:31 +0100 Subject: [PATCH] Clear hx::Null's value when it holds null Null(), Null(null) and the constructors from a null Dynamic or Variant left value unset. Default() reads it only when isNull is false, but GCC at -O2 can compute `isNull ? true : value` as `isNull | value`: an omitted Bool argument with a default of true then came out as the bytes the caller's frame held (23, 25, 103), and `compress ? 8 : 0` gave 184. Stock hxcpp 4.3.2 compiles the same code. value is now value-initialized. Test: regression/NullDefaultArgument builds each kind of Null over bytes that are not a bool and checks the value is cleared (each was 254). Co-Authored-By: Claude Opus 5.5 --- include/null.h | 11 +++-- test/regression/NullDefaultArgument/Main.hx | 44 +++++++++++++++++++ .../regression/NullDefaultArgument/build.hxml | 2 + .../regression/NullDefaultArgument/stdout.txt | 7 +++ 4 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 test/regression/NullDefaultArgument/Main.hx create mode 100644 test/regression/NullDefaultArgument/build.hxml create mode 100644 test/regression/NullDefaultArgument/stdout.txt diff --git a/include/null.h b/include/null.h index 4db3f72cf..6b8e056c8 100644 --- a/include/null.h +++ b/include/null.h @@ -183,17 +183,20 @@ namespace hx template struct Null { - inline Null() : isNull(true) { } + // A null leaves value cleared, not unset: a compiler may read it when it + // computes Default(), as GCC at -O2 turns `isNull ? true : value` into + // `isNull | value`, and an omitted Bool argument came out as stack bytes + inline Null() : isNull(true), value() { } inline Null(const Null &inOther) : isNull(inOther.isNull), value(inOther.value) { } inline Null(const T& inVal) : isNull(false), value(inVal) { } - inline Null(const null &) : isNull(true) { } - inline Null(const Dynamic &inVal) + inline Null(const null &) : isNull(true), value() { } + inline Null(const Dynamic &inVal) : value() { isNull = null() == inVal; if (!isNull) value = inVal; } - inline Null(const cpp::Variant &inVal) + inline Null(const cpp::Variant &inVal) : value() { isNull = inVal.isNull(); if (!isNull) diff --git a/test/regression/NullDefaultArgument/Main.hx b/test/regression/NullDefaultArgument/Main.hx new file mode 100644 index 000000000..0c33ea710 --- /dev/null +++ b/test/regression/NullDefaultArgument/Main.hx @@ -0,0 +1,44 @@ +// A Bool argument with a default, left out, comes as an hx::Null built +// from null. Its value used to be left unset: GCC at -O2 can turn +// `isNull ? true : value` into `isNull | value`, which then read the bytes the +// caller's frame held, and an omitted `compress:Bool = true` arrived as 23. +// Here each kind of Null is built over bytes that are not a bool, so the value +// it leaves shows whatever the optimizer does with it. +@:cppFileCode(' +#include + +template +static int valueOver(F make) +{ + alignas(hx::Null) unsigned char buffer[sizeof(hx::Null)]; + // Through volatile, or the compiler drops the stores, as the bytes of a new object are indeterminate + volatile unsigned char *fill = buffer; + for (int i = 0; i < (int)sizeof(buffer); i++) + fill[i] = 0xFE; + hx::Null *built = make(buffer); + const volatile unsigned char *value = (const volatile unsigned char *)&built->value; + int result = 0; + for (int i = 0; i < (int)sizeof(T); i++) + result |= value[i]; + return result; +} +') +class Main { + static function report(what:String, value:Int):Void { + Sys.println(what + ": " + (value == 0 ? "cleared" : "left as " + value)); + } + + static function main() { + report("Null()", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(); })")); + report("Null(null)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(null()); })")); + report("Null(null Dynamic)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(Dynamic()); })")); + report("Null(null Variant)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(cpp::Variant()); })")); + report("Null(null)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(null()); })")); + report("Null(null)", untyped __cpp__("valueOver([](void *at) { return new (at) hx::Null(null()); })")); + Sys.println("default true: " + defaulted()); + } + + static function defaulted(flag:Bool = true):Bool { + return flag; + } +} diff --git a/test/regression/NullDefaultArgument/build.hxml b/test/regression/NullDefaultArgument/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/NullDefaultArgument/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/NullDefaultArgument/stdout.txt b/test/regression/NullDefaultArgument/stdout.txt new file mode 100644 index 000000000..e0e569627 --- /dev/null +++ b/test/regression/NullDefaultArgument/stdout.txt @@ -0,0 +1,7 @@ +Null(): cleared +Null(null): cleared +Null(null Dynamic): cleared +Null(null Variant): cleared +Null(null): cleared +Null(null): cleared +default true: true