fix nil pointer dereference

This commit is contained in:
pluja 2024-08-13 07:01:44 +02:00
parent 081d59e58f
commit a636975aa2
No known key found for this signature in database

View File

@ -40,8 +40,17 @@ func (s *Server) handleVerifyPow(c iris.Context) {
func (s *Server) handleApiPicture(c iris.Context) {
id := c.Params().Get("id")
// Log and check s.Cache to make sure it is not nil
if s.Cache == nil {
err := fmt.Errorf("server's Cache is nil")
log.Error().Err(err).Msg("GetPicture: Fatal error")
respondWithPlaceholder(c, "?")
return
}
imgCacheKey := fmt.Sprintf("img-%s", id)
cttCacheKey := fmt.Sprintf("ctt-%s", id)
if imageData, err := s.Cache.Get(imgCacheKey); err == nil {
if ctt, err := s.Cache.Get(cttCacheKey); err == nil {
c.ContentType(string(ctt))
@ -51,9 +60,29 @@ func (s *Server) handleApiPicture(c iris.Context) {
}
}
// Log and check database.Pb to make sure it is not nil
if database.Pb == nil {
err := fmt.Errorf("database.Pb is nil")
log.Error().Err(err).Msg("GetPicture: Fatal error")
respondWithPlaceholder(c, "?")
return
}
service, err := database.Pb.GetServiceById(id)
if err != nil || service.LogoURL == "" {
log.Debug().Err(err).Msg("GetPicture: Could not get service or logo URL is empty")
if err != nil || service == nil || service.LogoURL == "" { // Include service nil check
log.Error().Err(err).Msg("GetPicture: Could not get service or logo URL is empty")
name := "?"
if service != nil {
name = service.Name
}
respondWithPlaceholder(c, name)
return
}
// Log and check s.HttpClient to make sure it is not nil
if s.HttpClient == nil {
err := fmt.Errorf("server's HttpClient is nil")
log.Error().Err(err).Msg("GetPicture: Fatal error")
respondWithPlaceholder(c, service.Name)
return
}
@ -67,7 +96,7 @@ func (s *Server) handleApiPicture(c iris.Context) {
}
resp, err := s.HttpClient.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
if err != nil || resp == nil || resp.StatusCode != http.StatusOK { // Include resp nil check
log.Debug().Err(err).Msgf("GetPicture: Could not get image for %s", service.Name)
respondWithPlaceholder(c, service.Name)
return