Reviewer · Null Handling
Guard pointer dereferences
Before dereferencing a pointer or accessing a pointer-typed optional field, explicitly check for nil to avoid runtime panics. This applies both to pointers returned from function calls (e.g., config lookups) and to optional fields within structs that may be left unset.
Before dereferencing a pointer or accessing a pointer-typed optional field, explicitly check for nil to avoid runtime panics. This applies both to pointers returned from function calls (e.g., config lookups) and to optional fields within structs that may be left unset.
For example, when a function may return a nil pointer, check it before use:
cfg, err := b.Config(ctx, req)
if err != nil {
return nil, err
}
if cfg == nil {
return nil, fmt.Errorf("ldap backend configuration not found")
}
if cfg.CaseSensitiveNames == nil || !*cfg.CaseSensitiveNames {
// handle case-insensitive logic
}
Similarly, when working with optional fields that may or may not be populated (e.g., RotationWindow which is only meaningful when RotationSchedule is set), do not assume all related optional fields are always set together—base conditional logic only on the fields that are guaranteed to indicate the relevant state, and add explicit nil/zero-value checks for any optional field actually being dereferenced or relied upon. Always trace the origin of pointers and optional values to confirm whether they can be nil/unset in the given code path, and add defensive checks rather than assuming non-nil.</description>
</invoke>