Skip to content

Commit 5d7e8cf

Browse files
committed
caddy.go: add oldCfg length check,fix index write error. (#6991)
1 parent 999e3bd commit 5d7e8cf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

caddy.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,16 @@ func changeConfig(method, path string, input []byte, ifMatchHeader string, force
265265

266266
func rollbackRawCfg() error {
267267
var oldCfg any
268-
err := json.Unmarshal(rawCfgJSON, &oldCfg)
269-
rawCfg[rawConfigKey] = oldCfg
270-
return err
268+
if len(rawCfgJSON) != 0 {
269+
err := json.Unmarshal(rawCfgJSON, &oldCfg)
270+
if err != nil {
271+
return err
272+
}
273+
rawCfg[rawConfigKey] = oldCfg
274+
} else {
275+
rawCfg[rawConfigKey] = nil
276+
}
277+
return nil
271278
}
272279

273280
// readConfig traverses the current config to path
@@ -297,7 +304,7 @@ func indexConfigObjects(ptr any, configPath string, index map[string]string) err
297304
if k == idKey {
298305
switch idVal := v.(type) {
299306
case string:
300-
err := writeToIdIndex(index, idVal, k)
307+
err := writeToIdIndex(index, idVal, configPath)
301308
if err != nil {
302309
return err
303310
}

0 commit comments

Comments
 (0)