Skip to content

Commit

Permalink
Fix crash due to stale owner reference in weapon entry
Browse files Browse the repository at this point in the history
  • Loading branch information
richardwilkes committed Jul 16, 2024
1 parent 6864dbd commit 6e74995
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 9 additions & 6 deletions model/gurps/weapon.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,13 @@ func (w *Weapon) Clone(_ LibraryFile, _ DataOwner, _ *Weapon, preserveID bool) *
if !preserveID {
other.TID = tid.MustNewTID(w.TID[0])
}
other.Damage = *other.Damage.Clone(&other)
if other.Defaults != nil {
other.Defaults = make([]*SkillDefault, 0, len(w.Defaults))
for _, one := range w.Defaults {
other.Damage = *w.Damage.Clone(&other)
other.Defaults = nil
if len(w.Defaults) != 0 {
other.Defaults = make([]*SkillDefault, len(w.Defaults))
for i, one := range w.Defaults {
d := *one
other.Defaults = append(other.Defaults, &d)
other.Defaults[i] = &d
}
}
return &other
Expand Down Expand Up @@ -547,7 +548,9 @@ func (w *Weapon) collectWeaponBonuses(dieCount int, tooltip *xio.ByteBuffer, all
var bestDef *SkillDefault
best := fxp.Min
for _, one := range w.Defaults {
if one.SkillBased() {
// Need the nil check here, as an entry in the defaults list could be nil due if this weapon pointer was
// obtained via a stale owner reference during an edit operation.
if one != nil && one.SkillBased() {
if level := one.SkillLevelFast(entity, false, nil, true); best < level {
best = level
bestDef = one
Expand Down
4 changes: 2 additions & 2 deletions ux/defaults_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ func newDefaultsPanel(entity *gurps.Entity, defaults *[]*gurps.SkillDefault) *de
addButton := unison.NewSVGButton(svg.CircledAdd)
addButton.ClickCallback = func() {
def := &gurps.SkillDefault{DefaultType: lastDefaultTypeUsed}
*defaults = slices.Insert(*defaults, 0, def)
*p.defaults = slices.Insert(*p.defaults, 0, def)
p.insertDefaultsPanel(1, def)
MarkRootAncestorForLayoutRecursively(p)
MarkModified(p)
}
p.AddChild(addButton)
for i, one := range *defaults {
for i, one := range *p.defaults {
p.insertDefaultsPanel(i+1, one)
}
return p
Expand Down

0 comments on commit 6e74995

Please sign in to comment.