Skip to content

Commit 7259712

Browse files
committed
refactor: clone slices in Node methods to prevent unintended modifications
1 parent 40632e2 commit 7259712

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

node.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func WithAction(act *NodeAction) NodeOption {
7272
// WithNext sets the next nodes list for the node.
7373
func WithNext(next []NodeNextItem) NodeOption {
7474
return func(n *Node) {
75-
n.Next = next
75+
n.Next = slices.Clone(next)
7676
}
7777
}
7878

@@ -95,7 +95,7 @@ func WithTimeout(timeout time.Duration) NodeOption {
9595
// WithOnError sets the error handling nodes for the node.
9696
func WithOnError(onError []NodeNextItem) NodeOption {
9797
return func(n *Node) {
98-
n.OnError = onError
98+
n.OnError = slices.Clone(onError)
9999
}
100100
}
101101

@@ -201,7 +201,7 @@ func NewNode(name string, opts ...NodeOption) *Node {
201201

202202
// SetAnchor sets the anchor for the node and returns the node for chaining.
203203
func (n *Node) SetAnchor(anchor []string) *Node {
204-
n.Anchor = anchor
204+
n.Anchor = slices.Clone(anchor)
205205
return n
206206
}
207207

@@ -219,7 +219,7 @@ func (n *Node) SetAction(act *NodeAction) *Node {
219219

220220
// SetNext sets the next nodes list for the node and returns the node for chaining.
221221
func (n *Node) SetNext(next []NodeNextItem) *Node {
222-
n.Next = next
222+
n.Next = slices.Clone(next)
223223
return n
224224
}
225225

@@ -239,7 +239,7 @@ func (n *Node) SetTimeout(timeout time.Duration) *Node {
239239

240240
// SetOnError sets the error handling nodes for the node and returns the node for chaining.
241241
func (n *Node) SetOnError(onError []NodeNextItem) *Node {
242-
n.OnError = onError
242+
n.OnError = slices.Clone(onError)
243243
return n
244244
}
245245

@@ -646,7 +646,7 @@ func WithTemplateMatchGreenMask(greenMask bool) TemplateMatchOption {
646646
// RecTemplateMatch creates a TemplateMatch recognition with the given template images.
647647
func RecTemplateMatch(template []string, opts ...TemplateMatchOption) *NodeRecognition {
648648
param := &NodeTemplateMatchParam{
649-
Template: template,
649+
Template: slices.Clone(template),
650650
}
651651

652652
for _, opt := range opts {
@@ -768,7 +768,7 @@ func WithFeatureMatchRatio(ratio float64) FeatureMatchOption {
768768
// Feature matching provides better generalization with perspective and scale invariance.
769769
func RecFeatureMatch(template []string, opts ...FeatureMatchOption) *NodeRecognition {
770770
param := &NodeFeatureMatchParam{
771-
Template: template,
771+
Template: slices.Clone(template),
772772
}
773773

774774
for _, opt := range opts {
@@ -880,8 +880,8 @@ func WithColorMatchConnected(connected bool) ColorMatchOption {
880880
// RecColorMatch creates a ColorMatch recognition with the given color bounds.
881881
func RecColorMatch(lower, upper [][]int, opts ...ColorMatchOption) *NodeRecognition {
882882
param := &NodeColorMatchParam{
883-
Lower: lower,
884-
Upper: upper,
883+
Lower: slices.Clone(lower),
884+
Upper: slices.Clone(upper),
885885
}
886886

887887
for _, opt := range opts {
@@ -949,7 +949,7 @@ func WithOCRROIOffset(offset Rect) OCROption {
949949
// WithOCRExpected sets the expected text results.
950950
func WithOCRExpected(expected []string) OCROption {
951951
return func(param *NodeOCRParam) {
952-
param.Expected = expected
952+
param.Expected = slices.Clone(expected)
953953
}
954954
}
955955

@@ -963,7 +963,7 @@ func WithOCRThreshold(th float64) OCROption {
963963
// WithOCRReplace sets text replacement rules for correcting OCR errors.
964964
func WithOCRReplace(replace [][2]string) OCROption {
965965
return func(param *NodeOCRParam) {
966-
param.Replace = replace
966+
param.Replace = slices.Clone(replace)
967967
}
968968
}
969969

@@ -1059,14 +1059,14 @@ func WithNeuralClassifyROIOffset(offset Rect) NeuralClassifyOption {
10591059
// WithNeuralClassifyLabels sets the class names for debugging and logging.
10601060
func WithNeuralClassifyLabels(labels []string) NeuralClassifyOption {
10611061
return func(param *NodeNeuralNetworkClassifyParam) {
1062-
param.Labels = labels
1062+
param.Labels = slices.Clone(labels)
10631063
}
10641064
}
10651065

10661066
// WithNeuralClassifyExpected sets the expected class indices.
10671067
func WithNeuralClassifyExpected(expected []int) NeuralClassifyOption {
10681068
return func(param *NodeNeuralNetworkClassifyParam) {
1069-
param.Expected = expected
1069+
param.Expected = slices.Clone(expected)
10701070
}
10711071
}
10721072

@@ -1152,14 +1152,14 @@ func WithNeuralDetectROIOffset(offset Rect) NeuralDetectOption {
11521152
// WithNeuralDetectLabels sets the class names for debugging and logging.
11531153
func WithNeuralDetectLabels(labels []string) NeuralDetectOption {
11541154
return func(param *NodeNeuralNetworkDetectParam) {
1155-
param.Labels = labels
1155+
param.Labels = slices.Clone(labels)
11561156
}
11571157
}
11581158

11591159
// WithNeuralDetectExpected sets the expected class indices.
11601160
func WithNeuralDetectExpected(expected []int) NeuralDetectOption {
11611161
return func(param *NodeNeuralNetworkDetectParam) {
1162-
param.Expected = expected
1162+
param.Expected = slices.Clone(expected)
11631163
}
11641164
}
11651165

@@ -1229,7 +1229,7 @@ func WithAndRecognitionBoxIndex(boxIndex int) AndRecognitionOption {
12291229
// RecAnd creates an AND recognition that requires all sub-recognitions to succeed.
12301230
func RecAnd(allOf []NodeAndRecognitionItem, opts ...AndRecognitionOption) *NodeRecognition {
12311231
param := &NodeAndRecognitionParam{
1232-
AllOf: allOf,
1232+
AllOf: slices.Clone(allOf),
12331233
}
12341234
for _, opt := range opts {
12351235
opt(param)
@@ -1250,7 +1250,7 @@ func (n NodeOrRecognitionParam) isRecognitionParam() {}
12501250
// RecOr creates an OR recognition that succeeds if any sub-recognition succeeds.
12511251
func RecOr(anyOf []*NodeRecognition) *NodeRecognition {
12521252
param := &NodeOrRecognitionParam{
1253-
AnyOf: anyOf,
1253+
AnyOf: slices.Clone(anyOf),
12541254
}
12551255
return &NodeRecognition{
12561256
Type: NodeRecognitionTypeOr,
@@ -1571,14 +1571,14 @@ func WithSwipeBeginOffset(offset Rect) SwipeOption {
15711571
// WithSwipeEnd sets the swipe end position.
15721572
func WithSwipeEnd(end []Target) SwipeOption {
15731573
return func(p *NodeSwipeParam) {
1574-
p.End = end
1574+
p.End = slices.Clone(end)
15751575
}
15761576
}
15771577

15781578
// WithSwipeEndOffset sets additional offset applied to end position.
15791579
func WithSwipeEndOffset(offset []Rect) SwipeOption {
15801580
return func(p *NodeSwipeParam) {
1581-
p.EndOffset = offset
1581+
p.EndOffset = slices.Clone(offset)
15821582
}
15831583
}
15841584

@@ -1685,14 +1685,14 @@ func WithMultiSwipeItemBeginOffset(offset Rect) MultiSwipeItemOption {
16851685
// WithMultiSwipeItemEnd sets the swipe end position.
16861686
func WithMultiSwipeItemEnd(end []Target) MultiSwipeItemOption {
16871687
return func(i *NodeMultiSwipeItem) {
1688-
i.End = end
1688+
i.End = slices.Clone(end)
16891689
}
16901690
}
16911691

16921692
// WithMultiSwipeItemEndOffset sets additional offset applied to end position.
16931693
func WithMultiSwipeItemEndOffset(offset []Rect) MultiSwipeItemOption {
16941694
return func(i *NodeMultiSwipeItem) {
1695-
i.EndOffset = offset
1695+
i.EndOffset = slices.Clone(offset)
16961696
}
16971697
}
16981698

@@ -1923,7 +1923,9 @@ func WithLongPressKeyDuration(d time.Duration) LongPressKeyOption {
19231923

19241924
// ActLongPressKey creates a LongPressKey action with the given virtual key code.
19251925
func ActLongPressKey(key []int, opts ...LongPressKeyOption) *NodeAction {
1926-
param := &NodeLongPressKeyParam{Key: key}
1926+
param := &NodeLongPressKeyParam{
1927+
Key: slices.Clone(key),
1928+
}
19271929
for _, opt := range opts {
19281930
opt(param)
19291931
}
@@ -2076,7 +2078,9 @@ type CommandOption func(*NodeCommandParam)
20762078

20772079
// WithCommandArgs sets the command arguments.
20782080
func WithCommandArgs(args []string) CommandOption {
2079-
return func(p *NodeCommandParam) { p.Args = args }
2081+
return func(p *NodeCommandParam) {
2082+
p.Args = slices.Clone(args)
2083+
}
20802084
}
20812085

20822086
// WithCommandDetach enables detached mode to run without waiting for completion.

0 commit comments

Comments
 (0)