Skip to content

Commit

Permalink
Handles type conversion errors
Browse files Browse the repository at this point in the history
  • Loading branch information
reederc42 authored Jun 12, 2024
1 parent 506a2f7 commit 7824986
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion storage_drivers/azure/azure_anf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"net"
"os"
"reflect"
Expand Down Expand Up @@ -793,7 +794,7 @@ func (d *NASStorageDriver) Create(
return fmt.Errorf("could not convert volume size %s; %v", volConfig.Size, err)
}
sizeBytes, err := strconv.ParseUint(requestedSize, 10, 64)
if err != nil {
if err != nil || sizeBytes > math.MaxInt64 { // the azure api requires both int64 and uint64
return fmt.Errorf("%v is an invalid volume size; %v", volConfig.Size, err)
}
if sizeBytes == 0 {
Expand Down
19 changes: 12 additions & 7 deletions storage_drivers/gcp/gcp_gcnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"encoding/json"
"fmt"
"math"
"net"
"reflect"
"regexp"
Expand Down Expand Up @@ -264,7 +265,7 @@ func (d *NASStorageDriver) populateConfigurationDefaults(
// VolumeCreateTimeoutSeconds is the timeout value in seconds.
volumeCreateTimeout := d.defaultCreateTimeout()
if config.VolumeCreateTimeout != "" {
i, err := strconv.ParseUint(d.Config.VolumeCreateTimeout, 10, 64)
i, err := strconv.ParseInt(d.Config.VolumeCreateTimeout, 10, 64)
if err != nil {
Logc(ctx).WithField("interval", d.Config.VolumeCreateTimeout).Errorf(
"Invalid volume create timeout period. %v", err)
Expand Down Expand Up @@ -478,7 +479,7 @@ func (d *NASStorageDriver) initializeGCNVAPIClient(

sdkTimeout := gcnvapi.DefaultSDKTimeout
if config.SDKTimeout != "" {
if i, parseErr := strconv.ParseUint(d.Config.SDKTimeout, 10, 64); parseErr != nil {
if i, parseErr := strconv.ParseInt(d.Config.SDKTimeout, 10, 64); parseErr != nil {
Logc(ctx).WithField("interval", d.Config.SDKTimeout).WithError(parseErr).Error(
"Invalid value for SDK timeout.")
return nil, parseErr
Expand All @@ -489,7 +490,7 @@ func (d *NASStorageDriver) initializeGCNVAPIClient(

maxCacheAge := gcnvapi.DefaultMaxCacheAge
if config.MaxCacheAge != "" {
if i, parseErr := strconv.ParseUint(d.Config.MaxCacheAge, 10, 64); parseErr != nil {
if i, parseErr := strconv.ParseInt(d.Config.MaxCacheAge, 10, 64); parseErr != nil {
Logc(ctx).WithField("interval", d.Config.MaxCacheAge).WithError(parseErr).Error(
"Invalid value for max cache age.")
return nil, parseErr
Expand Down Expand Up @@ -666,13 +667,14 @@ func (d *NASStorageDriver) Create(
snapshotReserve = pool.InternalAttributes()[SnapshotReserve]
}
var snapshotReservePtr *int64
var snapshotReserveInt int64
var snapshotReserveInt int
if snapshotReserve != "" {
snapshotReserveInt, err = strconv.ParseInt(snapshotReserve, 10, 64)
snapshotReserveInt64, err := strconv.ParseInt(snapshotReserve, 10, 0)
snapshotReserveInt = int(snapshotReserveInt64)
if err != nil {
return fmt.Errorf("invalid value for snapshotReserve: %v", err)
}
snapshotReservePtr = &snapshotReserveInt
snapshotReservePtr = &snapshotReserveInt64
}

// Determine volume size in bytes
Expand Down Expand Up @@ -704,7 +706,7 @@ func (d *NASStorageDriver) Create(
}

// Get the volume size based on the snapshot reserve
sizeWithReserveBytes := drivers.CalculateVolumeSizeBytes(ctx, name, sizeBytes, int(snapshotReserveInt))
sizeWithReserveBytes := drivers.CalculateVolumeSizeBytes(ctx, name, sizeBytes, snapshotReserveInt)

_, _, err = drivers.CheckVolumeSizeLimits(ctx, sizeWithReserveBytes, d.Config.CommonStorageDriverConfig)
if err != nil {
Expand Down Expand Up @@ -1801,6 +1803,9 @@ func (d *NASStorageDriver) Resize(ctx context.Context, volConfig *storage.Volume
}

// Include the snapshot reserve in the new size
if volume.SnapshotReserve > math.MaxInt {
return fmt.Errorf("snapshot reserve too large")
}
sizeWithReserveBytes := drivers.CalculateVolumeSizeBytes(ctx, name, sizeBytes, int(volume.SnapshotReserve))

// If the volume is already the requested size, there's nothing to do
Expand Down

0 comments on commit 7824986

Please sign in to comment.