Ver Fonte

fix(settings): fall back to defaults for empty/NULL setting values

A setting row whose value column is empty or NULL (seen on some migrated databases) was parsed directly, so getInt/getBool and the GetAllSetting reflection path crashed with 'strconv.Atoi: parsing "": invalid syntax'. This made the Inbounds page (/defaultSettings -> GetPageSize) and the Settings page fail to load.

Treat an empty stored value the same as a missing row and fall back to the built-in default at the int/bool parse sites. String getters are unchanged, so legitimately-empty string settings stay empty.

Closes #4830
MHSanaei há 9 horas atrás
pai
commit
fcc6787a64
1 ficheiros alterados com 13 adições e 4 exclusões
  1. 13 4
      web/service/setting.go

+ 13 - 4
web/service/setting.go

@@ -166,7 +166,7 @@ func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
 		fieldV := v.FieldByName(field.Name)
 		switch t := fieldV.Interface().(type) {
 		case int:
-			n, err := strconv.ParseInt(value, 10, 64)
+			n, err := strconv.ParseInt(effectiveSettingValue(key, value), 10, 64)
 			if err != nil {
 				return err
 			}
@@ -174,7 +174,7 @@ func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) {
 		case string:
 			fieldV.SetString(value)
 		case bool:
-			fieldV.SetBool(value == "true")
+			fieldV.SetBool(effectiveSettingValue(key, value) == "true")
 		default:
 			return common.NewErrorf("unknown field %v type %v", key, t)
 		}
@@ -286,12 +286,21 @@ func (s *SettingService) setString(key string, value string) error {
 	return s.saveSetting(key, value)
 }
 
+func effectiveSettingValue(key, stored string) string {
+	if stored == "" {
+		if def, ok := defaultValueMap[key]; ok {
+			return def
+		}
+	}
+	return stored
+}
+
 func (s *SettingService) getBool(key string) (bool, error) {
 	str, err := s.getString(key)
 	if err != nil {
 		return false, err
 	}
-	return strconv.ParseBool(str)
+	return strconv.ParseBool(effectiveSettingValue(key, str))
 }
 
 func (s *SettingService) setBool(key string, value bool) error {
@@ -303,7 +312,7 @@ func (s *SettingService) getInt(key string) (int, error) {
 	if err != nil {
 		return 0, err
 	}
-	return strconv.Atoi(str)
+	return strconv.Atoi(effectiveSettingValue(key, str))
 }
 
 func (s *SettingService) setInt(key string, value int) error {