-
Notifications
You must be signed in to change notification settings - Fork 85
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 crash related to empty block in lb_vs resource #1463
Conversation
Empty block can be configured when no required attributes are defined in the block. In this case nil protection is required, since empty block is presented as nil rather than empty map in the schema structure. Signed-off-by: Anna Khmelnitsky <akhmelnitsky@vmware.com>
@@ -903,6 +903,10 @@ func getPolicyLbRuleVariablePersistenceLearnActionSchema() *schema.Schema { | |||
func getPolicyClientSSLBindingFromSchema(d *schema.ResourceData) *model.LBClientSslProfileBinding { | |||
bindings := d.Get("client_ssl").([]interface{}) | |||
for _, binding := range bindings { | |||
if binding == nil { |
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.
This is unsafe as d.Get()
result can be nil as well and casting it will result a crash.
As we have multiple instances in the scope of this resource and multiple others in other resources, can we use a func instead of the range which will do all the validations, e.g check that input (d.Get()
result) is not null, that members aren't null as well etc?
e.g:
func iterateSchemaSlice(s interface{}, f func(interface{})) {
if s == nil {
return
}
for _, i := range s.([]interface{}) {
if i == nil {
continue
}
f(i)
}
}
And call it as:
iterateSchemaSlice(d.Get("client_ssl"), func(binding interface{}){
data := binding.(map[string]interface{})
chainDepth := int64(data["certificate_chain_depth"].(int))
})
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.
If the key exists in schema, then nil is not expected to be returned, as stated here
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.
Thanks for the clarification Anna.
A pedant note. Do you think it might be better to rewrite this to:
if binding != nil {
// maximum count is one
data := binding.(map[string]interface{})
chainDepth := int64(data["certificate_chain_depth"].(int))
}
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.
In most cases that are addressed in this patch, the code block that would need to be indented is rather big, so I would leave it as is for the sake of readability
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.
LGTM, no additional comment from me
Empty block can be configured when no required attributes are defined in the block. In this case nil protection is required, since empty block is presented as nil rather than empty map in the schema structure.