Skip to content

Commit f2cdfea

Browse files
fix(cmd): avoid nil pointer dereference (#2578)
* fix: avoid nil pointer dereference
1 parent ca7b252 commit f2cdfea

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

cmd/gossamer/config.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,20 @@ var (
4343
// loadConfigFile loads a default config file if --chain is specified, a specific
4444
// config if --config is specified, or the default gossamer config otherwise.
4545
func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
46-
// check --config flag and load toml configuration from config.toml
47-
if cfgPath := ctx.GlobalString(ConfigFlag.Name); cfgPath != "" {
48-
logger.Info("loading toml configuration from " + cfgPath + "...")
49-
if cfg == nil {
50-
cfg = &ctoml.Config{} // if configuration not set, create empty configuration
51-
} else {
52-
logger.Warn(
53-
"overwriting default configuration with id " + cfg.Global.ID +
54-
" with toml configuration values from " + cfgPath)
55-
}
56-
err = loadConfig(cfg, cfgPath) // load toml values into configuration
57-
} else {
58-
err = loadConfig(cfg, defaultGssmrConfigPath)
46+
cfgPath := ctx.GlobalString(ConfigFlag.Name)
47+
if cfgPath == "" {
48+
return loadConfig(cfg, defaultGssmrConfigPath)
5949
}
6050

61-
return err
51+
logger.Info("loading toml configuration from " + cfgPath + "...")
52+
if cfg == nil {
53+
cfg = new(ctoml.Config)
54+
} else {
55+
logger.Warn(
56+
"overwriting default configuration with id " + cfg.Global.ID +
57+
" with toml configuration values from " + cfgPath)
58+
}
59+
return loadConfig(cfg, cfgPath)
6260
}
6361

6462
func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error) {
@@ -215,15 +213,14 @@ func createImportStateConfig(ctx *cli.Context) (*dot.Config, error) {
215213
}
216214

217215
func createBuildSpecConfig(ctx *cli.Context) (*dot.Config, error) {
218-
var tomlCfg *ctoml.Config
219-
cfg := &dot.Config{}
216+
tomlCfg := new(ctoml.Config)
220217
err := loadConfigFile(ctx, tomlCfg)
221218
if err != nil {
222219
logger.Errorf("failed to load toml configuration: %s", err)
223220
return nil, err
224221
}
225222

226-
// set global configuration values
223+
cfg := new(dot.Config)
227224
if err := setDotGlobalConfig(ctx, tomlCfg, &cfg.Global); err != nil {
228225
logger.Errorf("failed to set global node configuration: %s", err)
229226
return nil, err

cmd/gossamer/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ var (
8080
Description: "The build-spec command outputs current genesis JSON data.\n" +
8181
"\tUsage: gossamer build-spec\n" +
8282
"\tTo generate raw genesis file from default: " +
83-
"gossamer build-spec --raw > genesis.json" +
83+
"gossamer build-spec --raw --output genesis.json" +
8484
"\tTo generate raw genesis file from specific genesis file: " +
85-
"gossamer build-spec --raw --genesis genesis-spec.json > genesis.json",
85+
"gossamer build-spec --raw --genesis genesis-spec.json --output genesis.json",
8686
}
8787

8888
// importRuntime generates a genesis file given a .wasm runtime binary.
@@ -397,11 +397,12 @@ func buildSpecAction(ctx *cli.Context) error {
397397
}
398398

399399
if outputPath := ctx.String(OutputSpecFlag.Name); outputPath != "" {
400-
if err = dot.WriteGenesisSpecFile(res, outputPath); err != nil {
401-
return err
400+
err = dot.WriteGenesisSpecFile(res, outputPath)
401+
if err != nil {
402+
return fmt.Errorf("cannot write genesis spec file: %w", err)
402403
}
403404
} else {
404-
fmt.Printf("%s", res)
405+
fmt.Printf("%s\n", res)
405406
}
406407

407408
return nil

cmd/gossamer/toml_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func loadConfig(cfg *ctoml.Config, fp string) error {
4646
},
4747
}
4848

49-
if err = tomlSettings.NewDecoder(file).Decode(&cfg); err != nil {
49+
if err = tomlSettings.NewDecoder(file).Decode(cfg); err != nil {
5050
logger.Errorf("failed to decode configuration: %s", err)
5151
return err
5252
}

0 commit comments

Comments
 (0)