fix: address PR review identity and snapshot safety gaps

This commit is contained in:
阿白叔 committed 2026-10-30 17:48:08 +08:00
1 parent 131b6dbbb3
commit f0a0131aac
15 files changed
+312 -64

No files matched your search

@@ -222,7 +222,7 @@ Function QPNativeCollectMode(ByRef SelectedUids, ByVal Token, ByVal Project, ByV
Count = 0
QPProgress "native_snapshot_read", 0, Components.Count
For Each Comp In Components
Uid = CStr(Comp.UID)
Uid = QPComponentIdentityKey(Comp)
If Len(Uid) = 0 Or Seen.Exists(Uid) Then QPNativeReadInvalid "duplicate or empty component UID"
Seen.Add Uid, True
If SelectedUids.Exists(Uid) Then
@@ -281,7 +281,7 @@ Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token,
"If QP_NR_Code<>0 Then QP_NR_Text=""ERROR"" & vbTab & " & QPNativeExpression(Token) & _
" & vbTab & CStr(QP_NR_Code) & vbTab & QPEncode(QP_NR_Source) & vbTab & QPEncode(QP_NR_Description) & vbCrLf" & vbCrLf & _
"QPWriteUtf8 " & QPNativeExpression(ResultPath) & ",QP_NR_Text" & vbCrLf & "On Error GoTo 0" & vbCrLf
Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPComponentIdentityKey QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Pattern.Global = False
For Each Name In Names
Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*"
+54 -47
View File
@@ -414,7 +414,7 @@ Sub QPBuildComponentIndex(ByRef Components)
Set Uids = CreateObject("Scripting.Dictionary")
Count = 0
For Each Comp In Components
QPAddComponentIndexEntry Uids, CStr(Comp.UID), Comp
QPAddComponentIndexEntry Uids, QPComponentIdentityKey(Comp), Comp
Count = Count + 1
If Count Mod 25 = 0 Then QPProgress "index_components", Count, 0
Next
@@ -480,8 +480,7 @@ Function QPComponentPins(ByVal ComponentUid)
Set AllPins = QP_App.ActiveView.Query(VDM_COMPPIN, VD_ALL)
Count = 0
For Each CmpPin In AllPins
Owner = CStr(CmpPin.Component.UID)
If Len(Owner) = 0 Then Err.Raise vbObjectError + 862, "QuickPartReplace", "pin owner UID is empty"
Owner = QPComponentIdentityKey(CmpPin.Component)
If Not LocalIndex.Exists(Owner) Then
Set Bucket = CreateObject("Scripting.Dictionary")
LocalIndex.Add Owner, Bucket
@@ -815,17 +814,7 @@ Function QPComponentIdentityKey(ByRef Comp)
QPComponentIdentityKey = "@REFDES|" & Trim(CStr(Refdes))
QPLog QP_Root, "WARN", "component_uid_missing_using_refdes_fallback refdes=" & CStr(Refdes)
Else
Dim Point
On Error Resume Next
Set Point = Comp.GetLocation()
If Err.Number <> 0 Or Point Is Nothing Then
Err.Clear
On Error GoTo 0
Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component UID and Refdes are empty"
End If
QPComponentIdentityKey = "@ANON|" & CStr(Point.X) & "|" & CStr(Point.Y)
Err.Clear
On Error GoTo 0
Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component UID and Refdes are empty"
End If
End If
End Function
@@ -1017,7 +1006,7 @@ Sub QPFinishBatchItem()
For Index = 0 To Records.Item("components").Count - 1
Set Record = Records.Item("components").Item(CStr(Index))
Set Candidate = QPFindUniqueByOldLocation(Record)
If CStr(Candidate.UID) <> CStr(Record("uid")) Then _
If QPComponentIdentityKey(Candidate) <> CStr(Record("uid")) Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "saved component UID differs"
Plans.Add CStr(Index), Candidate
Next
@@ -1226,6 +1215,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Dim Index
Dim Selectors, Selector, Uid, ValueAttr, ValueText, MatchedUids, UntouchedCount, NativeRead
QP_FallbackIdentityUsed = False
If StrComp(CStr(Command("old_part_number")), CStr(Command("new_part_number")), vbTextCompare) = 0 Then _
Err.Raise vbObjectError + 829, "QuickPartReplace", "source and target must differ"
QPInvalidatePinIndex
@@ -1318,7 +1308,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
UntouchedCount = 0
If NativeRead Is Nothing Then
For Each Comp In Components
If Not MatchedUids.Exists(CStr(Comp.UID)) Then
If Not MatchedUids.Exists(QPComponentIdentityKey(Comp)) Then
QPProgress "capture_untouched", UntouchedCount, Components.Count - Matches.Count
Snapshot = Snapshot & QPUntouchedComponent(Comp)
UntouchedCount = UntouchedCount + 1
@@ -1433,17 +1423,15 @@ End Function
Function QPUntouchedFullState(ByRef Comp)
Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid, Identity
Uid = CStr(Comp.UID)
Identity = Uid
If Len(Trim(Uid)) = 0 Then Identity = "@REFDES|" & Trim(CStr(Comp.Refdes))
Identity = QPComponentIdentityKey(Comp)
Set Point = Comp.GetLocation()
Set Pins = QPComponentPins(Uid)
Set Pins = QPComponentPins(Identity)
If Pins.Count = 0 Then
' A genuinely empty query is valid for annotation symbols; COM errors
' still propagate. Selected replacement symbols continue to require pins.
Connections = "NO_PINS"
Else
Connections = QPConnectionSignatureFromPins(Uid, Pins)
Connections = QPConnectionSignatureFromPins(Identity, Pins)
End If
Result = "C" & vbTab & QPEncode(Identity) & vbTab & QPEncode(CStr(Comp.Refdes)) & _
vbTab & QPEncode(CStr(Point.X)) & vbTab & QPEncode(CStr(Point.Y)) & _
@@ -1506,7 +1494,7 @@ Sub QPVerifyUntouched(ByRef Records)
' Read new component/pin proxies after each native mutation, restore or reopen.
QPInvalidatePinIndex
QPInvalidateComponentIndex
If QPUseNativeSnapshot() Then
If QPUseNativeSnapshot() And Not QP_FallbackIdentityUsed Then
If QPTryNativeVerifyUntouched(Records) Then Exit Sub
End If
Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL)
@@ -1579,12 +1567,10 @@ Function QPSnapshotComponent(ByRef Comp)
Dim ErrorDescription
Dim Uid, Identity, Pins
Uid = CStr(Comp.UID)
Identity = Uid
If Len(Trim(Uid)) = 0 Then Identity = "@REFDES|" & Trim(CStr(Comp.Refdes))
Identity = QPComponentIdentityKey(Comp)
Set Point = Comp.GetLocation()
Set Pins = QPComponentPins(Uid)
Connections = QPConnectionSignatureFromPins(Uid, Pins)
Set Pins = QPComponentPins(Identity)
Connections = QPConnectionSignatureFromPins(Identity, Pins)
Result = "C" & vbTab & QPEncode(Identity) & vbTab & _
QPEncode(CStr(Comp.Refdes)) & vbTab & QPEncode(CStr(Point.X)) & _
vbTab & QPEncode(CStr(Point.Y)) & vbTab & _
@@ -1638,7 +1624,7 @@ Function QPSortSnapshotLines(ByVal Text)
End Function
Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr)
Dim Instance, Value, Point, ErrorNumber, ErrorDescription
Dim Instance, Value, Point, ErrorNumber, ErrorDescription, Field
Dim LocationX
Dim LocationY
Dim Color
@@ -1653,23 +1639,31 @@ Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr)
Err.Clear
Instance = CStr(Attr.InstanceValue)
If Err.Number <> 0 Then
Instance = ""
QPLog QP_Root, "WARN", "optional_attribute_unavailable field=instance_value"
Err.Clear
On Error GoTo 0
Err.Raise vbObjectError + 836, "QuickPartReplace.Snapshot", "attribute InstanceValue unreadable; replacement blocked before mutation"
End If
Err.Clear
Value = CStr(Attr.Value)
If Err.Number <> 0 Then
Value = ""
QPLog QP_Root, "WARN", "optional_attribute_unavailable field=value"
Err.Clear
On Error GoTo 0
Err.Raise vbObjectError + 836, "QuickPartReplace.Snapshot", "attribute Value unreadable; replacement blocked before mutation"
End If
Err.Clear
Set Point = Nothing
Set Point = Attr.GetLocation()
If Err.Number = 0 And Not Point Is Nothing Then
LocationX = CStr(Point.X)
LocationY = CStr(Point.Y)
Else
LocationX = "0"
LocationY = "0"
ErrorNumber = Err.Number
Err.Clear
LocationX = ""
LocationY = ""
If ErrorNumber = 0 Then
If Not Point Is Nothing Then
LocationX = CStr(Point.X)
LocationY = CStr(Point.Y)
End If
End If
If Len(LocationX) = 0 Or Len(LocationY) = 0 Then
QPLog QP_Root, "WARN", "optional_display_field_unavailable field=location"
End If
Err.Clear
@@ -1679,6 +1673,10 @@ Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr)
Size = QPOptionalAttributeField(Attr, "size")
Orientation = QPOptionalAttributeField(Attr, "orientation")
Visible = QPOptionalAttributeField(Attr, "visible")
For Each Field In Array(LocationX, LocationY, Color, Font, Size, Orientation, Visible)
If Len(CStr(Field)) = 0 Then Err.Raise vbObjectError + 836, _
"QuickPartReplace.Snapshot", "attribute display state unreadable; replacement blocked before mutation"
Next
QPSnapshotAttribute = "A" & vbTab & QPEncode(ComponentUid) & vbTab & _
QPEncode(CStr(Attr.Name)) & vbTab & QPEncode(Value) & _
vbTab & QPEncode(CStr(Instance)) & vbTab & QPEncode(LocationX) & _
@@ -1722,7 +1720,7 @@ Function QPOptionalAttributeField(ByRef Attr, ByVal FieldName)
Err.Clear
On Error GoTo 0
If ErrorNumber <> 0 Then
QPOptionalAttributeField = "0"
QPOptionalAttributeField = ""
QPLog QP_Root, "WARN", "optional_display_field_unavailable field=" & CStr(FieldName) & _
" description=" & CStr(ErrorDescription)
Else
@@ -1761,9 +1759,9 @@ Sub QPRestoreAfterOperatorReplace(ByRef Command, ByVal SnapshotPath)
For Index = 0 To Records.Item("components").Count - 1
Set ComponentRecord = Records.Item("components").Item(CStr(Index))
Set Candidate = QPFindUniqueByOldLocation(ComponentRecord)
If Used.Exists(CStr(Candidate.UID)) Then _
If Used.Exists(QPComponentIdentityKey(Candidate)) Then _
Err.Raise vbObjectError + 831, "QuickPartReplace", "duplicate replacement association"
Used.Add CStr(Candidate.UID), True
Used.Add QPComponentIdentityKey(Candidate), True
QPRestorePreflight Candidate, ComponentRecord, Records.Item("attributes"), _
CStr(Command("new_part_number")), QPValueMode(Command)
Plans.Add CStr(Index), Candidate
@@ -1920,9 +1918,9 @@ Sub QPRecoverSavedJob(ByRef Command, ByVal SnapshotPath)
QPRequireSnapshotContext SnapshotPath
Set Record = Records.Item("components").Item(CStr(Index))
Set Candidate = QPFindUniqueByOldLocation(Record)
If CStr(Candidate.UID) <> CStr(Record("uid")) Or Used.Exists(CStr(Candidate.UID)) Then _
If QPComponentIdentityKey(Candidate) <> CStr(Record("uid")) Or Used.Exists(QPComponentIdentityKey(Candidate)) Then _
Err.Raise vbObjectError + 865, "QuickPartReplace.Recovery", "saved component UID changed or duplicated"
Used.Add CStr(Candidate.UID), True
Used.Add QPComponentIdentityKey(Candidate), True
PartNumber = QPRequiredAttributeValue(Candidate, "Part Number")
If StrComp(PartNumber, CStr(Command("old_part_number")), vbTextCompare) = 0 Then
OldCount = OldCount + 1
@@ -2029,7 +2027,7 @@ End Function
Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _
ByRef Attributes, ByVal NewPartNumber, ByVal ValueMode)
Dim Index
Dim Index, Field
Dim Record
Dim LabelAttr
Dim ValueAttr
@@ -2039,7 +2037,7 @@ Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _
Dim CurrentConnections
Dim SavedConnections
If CStr(Comp.UID) <> CStr(ComponentRecord("uid")) Or _
If QPComponentIdentityKey(Comp) <> CStr(ComponentRecord("uid")) Or _
CStr(Comp.Refdes) <> CStr(ComponentRecord("refdes")) Then _
Err.Raise vbObjectError + 831, "QuickPartReplace", "replacement component UID or Refdes differs from snapshot; no restoration writes allowed"
If StrComp(QPRequiredAttributeValue(Comp, "Part Number"), _
@@ -2057,6 +2055,15 @@ Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _
Err.Raise vbObjectError + 804, "QuickPartReplace", _
"connection validation failed"
End If
For Index = 0 To Attributes.Count - 1
Set Record = Attributes.Item(CStr(Index))
If CStr(Record("uid")) = CStr(ComponentRecord("uid")) Then
For Each Field In Array("x", "y", "color", "font", "size", "orientation", "visible")
If Len(CStr(Record(Field))) = 0 Then Err.Raise vbObjectError + 836, _
"QuickPartReplace.Snapshot", "attribute display state unavailable; no restoration writes allowed"
Next
End If
Next
If ValueMode = "preserve" Or ValueMode = "custom" Then Exit Sub
HasOldValue = False
Set ValueRecord = Nothing
@@ -2777,7 +2784,7 @@ End Function
Function QPConnectionSignature(ByRef Comp)
Dim Pins, Uid
Uid = CStr(Comp.UID)
Uid = QPComponentIdentityKey(Comp)
Set Pins = QPComponentPins(Uid)
QPConnectionSignature = QPConnectionSignatureFromPins(Uid, Pins)
End Function
@@ -2806,7 +2813,7 @@ Function QPConnectionSignatureFromPins(ByVal ComponentUid, ByRef Pins)
PinReadCount = 0
For Each PinIndexKey In Pins.Keys
Set CmpPin = Pins.Item(CStr(PinIndexKey))
If StrComp(CStr(CmpPin.Component.UID), ComponentUid, _
If StrComp(QPComponentIdentityKey(CmpPin.Component), ComponentUid, _
vbBinaryCompare) = 0 Then
Set Point = CmpPin.GetLocation()
PinKey = CStr(CmpPin.Number) & "|" & CStr(Point.X) & "|" & _