Fix nil panic in UpdateCache when listapisresponse is empty (#211) - #217
Fix nil panic in UpdateCache when listapisresponse is empty (#211)#217rohanmishra29 wants to merge 1 commit into
Conversation
|
@rohanmishra29 thanks for this fix, I'll include it in the milestone |
There was a problem hiding this comment.
🟡 Changes recommended
The new early-return path can still produce confusing CLI output and may wipe existing in-memory caches before validation, so the error handling should be adjusted to be safe and consistent.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
This review doesn't count toward merge requirements. Sign up for the private preview to control whether Copilot approvals count.
Pull request overview
This PR addresses a crash in CloudMonkey’s API discovery/cache update path when the CloudStack listApis response contains an empty listapisresponse (missing the api field), by adding a guard before the []interface{} type assertion.
Changes:
- Add a nil check for
response["api"]inConfig.UpdateCacheto prevent a panic on emptylistapisresponse. - Print an error message and return early when the API list is missing.
File summaries
| File | Description |
|---|---|
config/cache.go |
Adds a guard in UpdateCache to avoid panicking when listApis returns an empty response payload. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
We're testing this review assessment. Please use 👍 or 👎 to tell us if it's correct.
| count := response["count"] | ||
| if response["api"] == nil { | ||
| fmt.Println("Error: empty API list received, sync failed") | ||
| return nil | ||
| } |
| apiVerbMap = nil | ||
|
|
||
| count := response["count"] | ||
| if response["api"] == nil { |
There was a problem hiding this comment.
This nil check stops the original panic, but a few things are still off:
- The error goes to stdout, not stderr.
- The message hard-codes "sync", but
UpdateCacheis also called fromLoadCache. apiCache/apiVerbMapare already cleared before the early return, so a transient bad response wipes a cache that was previously fine.- If
response["api"]is non-nil but not a[]interface{}, the type assertion still panics.
A safe type assertion placed before the reset covers all four. Returning 0 instead of nil also keeps sync from printing Discovered APIs. Suggested top of the function (note the two reset lines move down):
func (c *Config) UpdateCache(response map[string]interface{}) interface{} {
apiList, valid := response["api"].([]interface{})
if !valid || len(apiList) == 0 {
fmt.Fprintln(os.Stderr, "Error: no APIs found in the discovery response, keeping the existing API cache. Please run 'sync'.")
return 0
}
apiCache = make(map[string]*API)
apiVerbMap = nil
count := response["count"]
for _, node := range apiList {os is already imported in this file, so no import change is needed.
Tested locally with a cache file of {"count":0}, {"count":1,"api":"boom"} and {"count":0,"api":[]} — all three print the error to stderr instead of panicking, and go vet ./config/ is clean.
Fixes #211
Problem
When the CloudStack management server returns an empty
listapisresponse(i.e.,{"listapisresponse":{}}), thesynccommand panics with:panic: interface conversion: interface {} is nil, not []interface {}
This happens in
config/cache.goat the type assertionresponse["api"].([]interface{})whenresponse["api"]is nil.Fix
Added a nil check for
response["api"]before the type assertion. If the API list is empty, the function now returnsnilwith a clear error message instead of panicking.Testing
Built successfully with
go build ./.... The nil check prevents the panic when an emptylistapisresponseis received.