Skip to content

Commit

Permalink
fix(cmd): avoid nil pointer dereference (#2578)
Browse files Browse the repository at this point in the history
* fix: avoid nil pointer dereference
  • Loading branch information
EclesioMeloJunior committed Jun 3, 2022
1 parent ca7b252 commit f2cdfea
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
31 changes: 14 additions & 17 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,20 @@ var (
// loadConfigFile loads a default config file if --chain is specified, a specific
// config if --config is specified, or the default gossamer config otherwise.
func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
// check --config flag and load toml configuration from config.toml
if cfgPath := ctx.GlobalString(ConfigFlag.Name); cfgPath != "" {
logger.Info("loading toml configuration from " + cfgPath + "...")
if cfg == nil {
cfg = &ctoml.Config{} // if configuration not set, create empty configuration
} else {
logger.Warn(
"overwriting default configuration with id " + cfg.Global.ID +
" with toml configuration values from " + cfgPath)
}
err = loadConfig(cfg, cfgPath) // load toml values into configuration
} else {
err = loadConfig(cfg, defaultGssmrConfigPath)
cfgPath := ctx.GlobalString(ConfigFlag.Name)
if cfgPath == "" {
return loadConfig(cfg, defaultGssmrConfigPath)
}

return err
logger.Info("loading toml configuration from " + cfgPath + "...")
if cfg == nil {
cfg = new(ctoml.Config)
} else {
logger.Warn(
"overwriting default configuration with id " + cfg.Global.ID +
" with toml configuration values from " + cfgPath)
}
return loadConfig(cfg, cfgPath)
}

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

func createBuildSpecConfig(ctx *cli.Context) (*dot.Config, error) {
var tomlCfg *ctoml.Config
cfg := &dot.Config{}
tomlCfg := new(ctoml.Config)
err := loadConfigFile(ctx, tomlCfg)
if err != nil {
logger.Errorf("failed to load toml configuration: %s", err)
return nil, err
}

// set global configuration values
cfg := new(dot.Config)
if err := setDotGlobalConfig(ctx, tomlCfg, &cfg.Global); err != nil {
logger.Errorf("failed to set global node configuration: %s", err)
return nil, err
Expand Down
11 changes: 6 additions & 5 deletions cmd/gossamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ var (
Description: "The build-spec command outputs current genesis JSON data.\n" +
"\tUsage: gossamer build-spec\n" +
"\tTo generate raw genesis file from default: " +
"gossamer build-spec --raw > genesis.json" +
"gossamer build-spec --raw --output genesis.json" +
"\tTo generate raw genesis file from specific genesis file: " +
"gossamer build-spec --raw --genesis genesis-spec.json > genesis.json",
"gossamer build-spec --raw --genesis genesis-spec.json --output genesis.json",
}

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

if outputPath := ctx.String(OutputSpecFlag.Name); outputPath != "" {
if err = dot.WriteGenesisSpecFile(res, outputPath); err != nil {
return err
err = dot.WriteGenesisSpecFile(res, outputPath)
if err != nil {
return fmt.Errorf("cannot write genesis spec file: %w", err)
}
} else {
fmt.Printf("%s", res)
fmt.Printf("%s\n", res)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/toml_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func loadConfig(cfg *ctoml.Config, fp string) error {
},
}

if err = tomlSettings.NewDecoder(file).Decode(&cfg); err != nil {
if err = tomlSettings.NewDecoder(file).Decode(cfg); err != nil {
logger.Errorf("failed to decode configuration: %s", err)
return err
}
Expand Down

0 comments on commit f2cdfea

Please sign in to comment.