Skip to content

Commit

Permalink
Merge pull request #22 from teamhava/bug/dev-1753/fix-derefernce-segf…
Browse files Browse the repository at this point in the history
…ault

Added a safe derefence method for printing values to screen
  • Loading branch information
TWinsnes committed Oct 23, 2023
2 parents a9fc197 + 34725cc commit ed9f214
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ go.work
hava
hava-ui-cli
dist/

# system files
.DS_Store
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"args": ["source", "list"]
}
]
}
23 changes: 18 additions & 5 deletions cmd/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func showSourceId(api *havaclient.APIClient, sourceId string) {
}

o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
o.AddTableRows(*source.DisplayName, *source.Id, *source.Info, *source.Name, *source.State, *source.Type)
o.AddTableRows(
SafeDeref(source.DisplayName),
SafeDeref(source.Id),
SafeDeref(source.Info),
SafeDeref(source.Name),
SafeDeref(source.State),
SafeDeref(source.Type))

}

Expand All @@ -66,13 +72,20 @@ func showSources(api *havaclient.APIClient) {

//Length of Results

if len(source.Results) > 0 {
numResults := len(source.Results)

if numResults > 0 {

o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
// Loop through results
for i := 0; i < len(source.Results); i++ {

o.AddTableRows(*source.Results[i].DisplayName, *source.Results[i].Id, *source.Results[i].Info, *source.Results[i].Name, *source.Results[i].State, *source.Results[i].Type)
for i := 0; i < numResults; i++ {
o.AddTableRows(
SafeDeref(source.Results[i].DisplayName),
SafeDeref(source.Results[i].Id),
SafeDeref(source.Results[i].Info),
SafeDeref(source.Results[i].Name),
SafeDeref(source.Results[i].State),
SafeDeref(source.Results[i].Type))
}

} else {
Expand Down
17 changes: 14 additions & 3 deletions cmd/source_create_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,13 @@ func sourceCreateAwsKey(api *havaclient.APIClient, sourceName string, awsType st

o.AddInfoMessage("Created AWS Source for the following source:\n")
o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
o.AddTableRows(*source.DisplayName, *source.Id, *source.Info, *source.Name, *source.State, *source.Type)

o.AddTableRows(
SafeDeref(source.DisplayName),
SafeDeref(source.Id),
SafeDeref(source.Info),
SafeDeref(source.Name),
SafeDeref(source.State),
SafeDeref(source.Type))
}

func sourceCreateAwsCar(api *havaclient.APIClient, sourceName string, awsType string, roleArn string, externalID string) {
Expand Down Expand Up @@ -121,6 +126,12 @@ func sourceCreateAwsCar(api *havaclient.APIClient, sourceName string, awsType st

o.AddInfoMessage("Created AWS Source for the following source:\n")
o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
o.AddTableRows(*source.DisplayName, *source.Id, *source.Info, *source.Name, *source.State, *source.Type)
o.AddTableRows(
SafeDeref(source.DisplayName),
SafeDeref(source.Id),
SafeDeref(source.Info),
SafeDeref(source.Name),
SafeDeref(source.State),
SafeDeref(source.Type))

}
8 changes: 7 additions & 1 deletion cmd/source_create_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ func sourceCreateAzure(api *havaclient.APIClient, sourceName string, sourceType

o.AddInfoMessage("Created Azure Source for the following source:\n")
o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
o.AddTableRows(*source.DisplayName, *source.Id, *source.Info, *source.Name, *source.State, *source.Type)
o.AddTableRows(
SafeDeref(source.DisplayName),
SafeDeref(source.Id),
SafeDeref(source.Info),
SafeDeref(source.Name),
SafeDeref(source.State),
SafeDeref(source.Type))

}
8 changes: 7 additions & 1 deletion cmd/source_create_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ func sourceCreateGcp(api *havaclient.APIClient, sourceName string, sourceType st

o.AddInfoMessage("Created GCP Source for the following source:\n")
o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
o.AddTableRows(*source.DisplayName, *source.Id, *source.Info, *source.Name, *source.State, *source.Type)
o.AddTableRows(
SafeDeref(source.DisplayName),
SafeDeref(source.Id),
SafeDeref(source.Info),
SafeDeref(source.Name),
SafeDeref(source.State),
SafeDeref(source.Type))

}
8 changes: 7 additions & 1 deletion cmd/source_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func syncSourceID(api *havaclient.APIClient, sourceId string) {

o.AddInfoMessage("Sync scheduled for the following source:")
o.AddTableHeaders("DisplayName", "Id", "Info", "Name", "State", "Type")
o.AddTableRows(*source.DisplayName, *source.Id, *source.Info, *source.Name, *source.State, *source.Type)
o.AddTableRows(
SafeDeref(source.DisplayName),
SafeDeref(source.Id),
SafeDeref(source.Info),
SafeDeref(source.Name),
SafeDeref(source.State),
SafeDeref(source.Type))

}
13 changes: 13 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cmd

// SafeDeref is used to safely derefence a pointer where it might point to null
// In the case where it points to null it will return the default value for the type
// rather than a runtime error / segfault
func SafeDeref[T any](pointer *T) T {
if pointer == nil {
var defaultValue T
return defaultValue
}

return *pointer
}

0 comments on commit ed9f214

Please sign in to comment.