-
Notifications
You must be signed in to change notification settings - Fork 5
fix: dereference typed pointers in VML models safely #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+189
−11
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| module ui2 | ||
|
|
||
| // Track the current reference path, not a global visited set: two model fields | ||
| // may legitimately point at the same object. Back-edges and nil values remain | ||
| // invalid VML values and produce the usual source-line property-path error. | ||
| fn v_value_from[T](value T) VValue { | ||
| return v_value_from_tracked[T](value, []voidptr{}) | ||
| } | ||
|
|
||
| fn v_value_from_pointee[E](value &E, ancestors []voidptr) VValue { | ||
| $if E is $pointer { | ||
| return v_value_from_tracked[E](E(unsafe { voidptr(*value) }), ancestors) | ||
| } $else { | ||
| return v_value_from_tracked[E](*value, ancestors) | ||
| } | ||
| } | ||
|
|
||
| fn v_schema_from[T](value T) VSchema { | ||
| return v_schema_from_tracked[T](value, []voidptr{}, []string{}) | ||
| } | ||
|
|
||
| fn v_schema_from_pointee[E](value &E, ancestors []voidptr, nil_types []string) VSchema { | ||
| $if E is $pointer { | ||
| return v_schema_from_tracked[E](E(unsafe { voidptr(*value) }), ancestors, nil_types) | ||
| } $else { | ||
| return v_schema_from_tracked[E](*value, ancestors, nil_types) | ||
| } | ||
| } | ||
|
|
||
| fn v_schema_from_nil_pointee[E](_ &E, ancestors []voidptr, nil_types []string) VSchema { | ||
| $if E is $pointer { | ||
| return v_schema_from_tracked[E](E(unsafe { nil }), ancestors, nil_types) | ||
| } $else { | ||
| return v_schema_from_tracked[E]($zero(E), ancestors, nil_types) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| module ui2 | ||
|
|
||
| pub struct PointerCounter { | ||
| pub mut: | ||
| count int | ||
| next &PointerCounter = unsafe { nil } | ||
| } | ||
|
|
||
| pub struct PointerCounterApp { | ||
| pub mut: | ||
| counter &PointerCounter = unsafe { nil } | ||
| other &PointerCounter = unsafe { nil } | ||
| items []&PointerCounter | ||
| } | ||
|
|
||
| pub fn (mut app PointerCounterApp) increment() { | ||
| app.counter.count++ | ||
| } | ||
|
|
||
| fn test_pointer_model_reads_live_values_after_an_action() { | ||
| counter := &PointerCounter{ | ||
| count: 3 | ||
| } | ||
| mut app := new_vml_app('Screen { | ||
| Label { text: app.counter.count } | ||
| Button { on_tap: app.increment() } | ||
| }', PointerCounterApp{ | ||
| counter: counter | ||
| }) or { panic(err) } | ||
| built := app.build(rect(0, 0, 200, 100)) or { panic(err) } | ||
| assert built.children[0].text == '3' | ||
| app.handle(built.children[1].action_id) or { panic(err) } | ||
| assert counter.count == 4 | ||
| assert (app.build(rect(0, 0, 200, 100)) or { panic(err) }).children[0].text == '4' | ||
| } | ||
|
|
||
| fn test_pointer_model_handles_shared_nested_and_cyclic_references() { | ||
| mut counter := &PointerCounter{ | ||
| count: 3 | ||
| } | ||
| counter.next = counter | ||
| model := PointerCounterApp{ | ||
| counter: counter | ||
| other: counter | ||
| } | ||
| root := element_from_vml_model('Label { text: app.counter.count + app.other.count }', model, rect(0, | ||
| 0, 100, 30)) or { panic(err) } | ||
| assert root.text == '6.0' | ||
| counter.next = &PointerCounter{ | ||
| count: 5 | ||
| } | ||
| nested := element_from_vml_model('Label { text: app.counter.next.count }', model, rect(0, 0, | ||
| 100, 30)) or { panic(err) } | ||
| assert nested.text == '5' | ||
| } | ||
|
|
||
| fn test_pointer_model_nil_path_returns_an_error_without_dereferencing() { | ||
| if _ := element_from_vml_model('Label { text: app.counter.count }', PointerCounterApp{}, rect(0, | ||
| 0, 100, 30)) | ||
| { | ||
| assert false, 'a nil counter must not be dereferenced' | ||
| } else { | ||
| assert err.msg().contains('app.counter.count') | ||
| assert err.msg().contains('line 1') | ||
| } | ||
| // A nil field that is not used by this template must be harmless. | ||
| assert (element_from_vml_model('Label { text: "OK" }', PointerCounterApp{}, rect(0, 0, 100, 30)) or { | ||
| panic(err) | ||
| }).text == 'OK' | ||
| } | ||
|
|
||
| fn test_pointer_model_arrays_validate_when_empty_and_render_when_populated() { | ||
| source := 'Column { Repeater { model: app.items key: item.count Label { text: item.count } } }' | ||
| empty := element_from_vml_model(source, PointerCounterApp{}, rect(0, 0, 100, 100)) or { | ||
| panic(err) | ||
| } | ||
| assert empty.children.len == 0 | ||
| full := element_from_vml_model(source, PointerCounterApp{ items: [&PointerCounter{ count: 7 }] }, rect(0, | ||
| 0, 100, 100)) or { panic(err) } | ||
| assert full.children[0].text == '7' | ||
| if _ := element_from_vml_model(source.replace('text: item.count', 'text: item.typo'), PointerCounterApp{}, rect(0, | ||
| 0, 100, 100)) | ||
| { | ||
| assert false, 'empty pointer arrays must still validate fields' | ||
| } else { | ||
| assert err.msg().contains('item.typo') | ||
| } | ||
| } | ||
|
|
||
| fn test_pointer_model_dereferences_scalar_and_multiple_indirections() { | ||
| number := 42 | ||
| pointer := &number | ||
| assert v_value_from(pointer).number == 42 | ||
| assert v_value_from(&pointer).number == 42 | ||
| assert v_schema_from(&pointer).kind == .number | ||
| text := 'Hello' | ||
| assert v_value_from(&text).text == 'Hello' | ||
| flag := true | ||
| assert v_value_from(&flag).bool_ | ||
| assert v_value_from(unsafe { nil }).kind == .invalid | ||
| } | ||
|
|
||
| fn test_pointer_model_multiple_nil_indirections_are_safe() { | ||
| nil_counter := unsafe { &PointerCounter(nil) } | ||
| assert v_value_from(&nil_counter).kind == .invalid | ||
| assert v_schema_from(&nil_counter).kind == .object | ||
| schema := v_schema_from(&nil_counter) | ||
| assert (schema.fields['count'] or { panic('missing count schema') }).kind == .number | ||
| nil_double := unsafe { &&PointerCounter(nil) } | ||
| assert v_schema_from(nil_double).kind == .object | ||
| assert v_value_from(nil_double).kind == .invalid | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a pointer targets the first field of an ancestor struct, its address is normally identical to the struct's address, so this address-only check incorrectly treats the interior pointer as a cyclic back-edge. For example, if
Counterhascount intfollowed bycount_ref &intandcount_ref = &counter.count, converting&countermakescount_refinvalid instead of exposing the number; the analogous check inv_schema_from_trackedalso rejectsapp.counter.count_refduring validation. Track the pointee type together with the address (or otherwise distinguish interior pointers from actual back-edges).Useful? React with 👍 / 👎.