2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-10 23:31:09 +00:00

Merge pull request #3052 from restic/error-on-invalid-size

Return an error for invalid sizes
This commit is contained in:
Alexander Neumann 2020-11-03 12:13:49 +01:00 committed by GitHub
commit 0db9024aad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 1 deletions

View File

@ -314,6 +314,10 @@ func rejectBySize(maxSizeStr string) (RejectFunc, error) {
} }
func parseSizeStr(sizeStr string) (int64, error) { func parseSizeStr(sizeStr string) (int64, error) {
if sizeStr == "" {
return 0, errors.New("expected size, got empty string")
}
numStr := sizeStr[:len(sizeStr)-1] numStr := sizeStr[:len(sizeStr)-1]
var unit int64 = 1 var unit int64 = 1
@ -333,7 +337,7 @@ func parseSizeStr(sizeStr string) (int64, error) {
} }
value, err := strconv.ParseInt(numStr, 10, 64) value, err := strconv.ParseInt(numStr, 10, 64)
if err != nil { if err != nil {
return 0, nil return 0, err
} }
return value * unit, nil return value * unit, nil
} }

View File

@ -219,6 +219,25 @@ func TestParseSizeStr(t *testing.T) {
} }
} }
func TestParseInvalidSizeStr(t *testing.T) {
invalidSizes := []string{
"",
" ",
"foobar",
"zzz",
}
for _, s := range invalidSizes {
v, err := parseSizeStr(s)
if err == nil {
t.Errorf("wanted error for invalid value %q, got nil", s)
}
if v != 0 {
t.Errorf("wanted zero for invalid value %q, got: %v", s, v)
}
}
}
// TestIsExcludedByFileSize is for testing the instance of // TestIsExcludedByFileSize is for testing the instance of
// --exclude-larger-than parameters // --exclude-larger-than parameters
func TestIsExcludedByFileSize(t *testing.T) { func TestIsExcludedByFileSize(t *testing.T) {