From 26d77996cd6057592f0d7212c9017e8b62be66e8 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Mon, 23 Jun 2025 14:22:26 +0200 Subject: prog: use consistent default values for conditional unions We used to assume that the default value was the last, yet when it was not specified in the serialized program, the first union option whose condition is satisfied was chosen. Let's be consistent and use the last value in both cases. Also, remember that there's a case when there's no valid default value - this happens when pkg/compiler wraps a conditional field into a union with two conditional fields. Explicitly check for this case and assume that, whatever value is set, is the correct default because in this particular case the conditions of the two union options must be mutually exclusive. Fixes #6105. --- prog/expr_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'prog/expr_test.go') diff --git a/prog/expr_test.go b/prog/expr_test.go index 1558248f5..b536d05e2 100644 --- a/prog/expr_test.go +++ b/prog/expr_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestGenerateConditionalFields(t *testing.T) { @@ -309,3 +310,16 @@ func TestNestedConditionalCall(t *testing.T) { } } } + +func TestDefaultConditionalSerialize(t *testing.T) { + // Serialize() omits default-valued fields for a more compact representation, + // but that shouldn't mess with the selected option (see #6105). + const rawProg = "test$parent_conditions(&(0x7f0000000000)={0xa3})\n" + target := initTargetTest(t, "test", "64") + prog, err := target.Deserialize([]byte(rawProg), NonStrict) + require.NoError(t, err) + serialized := prog.Serialize() + prog2, err := target.Deserialize(serialized, NonStrict) + require.NoError(t, err) + assert.Equal(t, rawProg, string(prog2.Serialize())) +} -- cgit mrf-deployment