Skip to content
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

Fix potential race condition leading to nil dereference #105

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions usecases/cs/lpc/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/enbility/eebus-go/api"
"github.com/enbility/eebus-go/features/server"
ucapi "github.com/enbility/eebus-go/usecases/api"
spineapi "github.com/enbility/spine-go/api"
"github.com/enbility/spine-go/model"
"github.com/enbility/spine-go/util"
)
Expand Down Expand Up @@ -134,29 +133,13 @@ func (e *LPC) ApproveOrDenyConsumptionLimit(msgCounter model.MsgCounterType, app
e.pendingMux.Lock()
defer e.pendingMux.Unlock()

f := e.LocalEntity.FeatureOfTypeAndRole(model.FeatureTypeTypeLoadControl, model.RoleTypeServer)

result := model.ErrorType{
ErrorNumber: model.ErrorNumberType(0),
}

msg, ok := e.pendingLimits[msgCounter]
if !ok {
// it is not a limit of this usecase, so approve it
newMsg := &spineapi.Message{
RequestHeader: &model.HeaderType{
MsgCounter: &msgCounter,
},
}
f.ApproveOrDenyWrite(newMsg, result)
// no pending limit for this msgCounter, this is a caller error
return
}

if !approve {
result.ErrorNumber = model.ErrorNumberType(7)
result.Description = util.Ptr(model.DescriptionType(reason))
}
f.ApproveOrDenyWrite(msg, result)
e.approveOrDenyConsumptionLimit(msg, approve, reason)

delete(e.pendingLimits, msgCounter)
}
Expand Down
16 changes: 15 additions & 1 deletion usecases/cs/lpc/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ func (e *LPC) loadControlServerAndLimitId() (lc *server.LoadControl, limitid mod
return lc, *description.LimitId, nil
}

func (e *LPC) approveOrDenyConsumptionLimit(msg *spineapi.Message, approve bool, reason string) {
f := e.LocalEntity.FeatureOfTypeAndRole(model.FeatureTypeTypeLoadControl, model.RoleTypeServer)

result := model.ErrorType{
ErrorNumber: model.ErrorNumberType(0),
}

if !approve {
result.ErrorNumber = model.ErrorNumberType(7)
result.Description = util.Ptr(model.DescriptionType(reason))
}
f.ApproveOrDenyWrite(msg, result)
}

// callback invoked on incoming write messages to this
// loadcontrol server feature.
// the implementation only considers write messages for this use case and
Expand Down Expand Up @@ -145,7 +159,7 @@ func (e *LPC) loadControlWriteCB(msg *spineapi.Message) {
e.pendingMux.Unlock()

// approve, because this is no request for this usecase
go e.ApproveOrDenyConsumptionLimit(*msg.RequestHeader.MsgCounter, true, "")
go e.approveOrDenyConsumptionLimit(msg, true, "")
}

func (e *LPC) AddFeatures() {
Expand Down
21 changes: 2 additions & 19 deletions usecases/cs/lpp/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/enbility/eebus-go/api"
"github.com/enbility/eebus-go/features/server"
ucapi "github.com/enbility/eebus-go/usecases/api"
spineapi "github.com/enbility/spine-go/api"
"github.com/enbility/spine-go/model"
"github.com/enbility/spine-go/util"
)
Expand Down Expand Up @@ -134,29 +133,13 @@ func (e *LPP) ApproveOrDenyProductionLimit(msgCounter model.MsgCounterType, appr
e.pendingMux.Lock()
defer e.pendingMux.Unlock()

f := e.LocalEntity.FeatureOfTypeAndRole(model.FeatureTypeTypeLoadControl, model.RoleTypeServer)

result := model.ErrorType{
ErrorNumber: model.ErrorNumberType(0),
}

msg, ok := e.pendingLimits[msgCounter]
if !ok {
// it is not a limit of this usecase, so approve it
newMsg := &spineapi.Message{
RequestHeader: &model.HeaderType{
MsgCounter: &msgCounter,
},
}
f.ApproveOrDenyWrite(newMsg, result)
// no pending limit for this msgCounter, this is a caller error
return
}

if !approve {
result.ErrorNumber = model.ErrorNumberType(7)
result.Description = util.Ptr(model.DescriptionType(reason))
}
f.ApproveOrDenyWrite(msg, result)
e.approveOrDenyProductionLimit(msg, approve, reason)

delete(e.pendingLimits, msgCounter)
}
Expand Down
16 changes: 15 additions & 1 deletion usecases/cs/lpp/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ func (e *LPP) loadControlServerAndLimitId() (lc *server.LoadControl, limitid mod
return lc, *description.LimitId, nil
}

func (e *LPP) approveOrDenyProductionLimit(msg *spineapi.Message, approve bool, reason string) {
f := e.LocalEntity.FeatureOfTypeAndRole(model.FeatureTypeTypeLoadControl, model.RoleTypeServer)

result := model.ErrorType{
ErrorNumber: model.ErrorNumberType(0),
}

if !approve {
result.ErrorNumber = model.ErrorNumberType(7)
result.Description = util.Ptr(model.DescriptionType(reason))
}
f.ApproveOrDenyWrite(msg, result)
}

// callback invoked on incoming write messages to this
// loadcontrol server feature.
// the implementation only considers write messages for this use case and
Expand Down Expand Up @@ -145,7 +159,7 @@ func (e *LPP) loadControlWriteCB(msg *spineapi.Message) {
e.pendingMux.Unlock()

// approve, because this is no request for this usecase
go e.ApproveOrDenyProductionLimit(*msg.RequestHeader.MsgCounter, true, "")
go e.approveOrDenyProductionLimit(msg, true, "")
}

func (e *LPP) AddFeatures() {
Expand Down
Loading