Skip to content

Commit

Permalink
Ensure slice index is always is within range
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed May 4, 2021
1 parent 2039ed3 commit 0796771
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
64 changes: 51 additions & 13 deletions src/replace.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package f2

import (
"errors"
"math"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -77,12 +78,21 @@ type replaceVars struct {
random randomVar
}

var (
errInvalidSubmatches = errors.New("Invalid number of submatches")
)

func getDateVar(str string) (dateVar, error) {
var d dateVar
if dateRegex.MatchString(str) {
d.submatches = dateRegex.FindAllStringSubmatch(str, -1)
expectedLength := 3

for _, submatch := range d.submatches {
if len(submatch) < expectedLength {
return d, errInvalidSubmatches
}

var x struct {
regex *regexp.Regexp
attr string
Expand All @@ -108,8 +118,13 @@ func getHashVar(str string) (hashVar, error) {
var h hashVar
if hashRegex.MatchString(str) {
h.submatches = hashRegex.FindAllStringSubmatch(str, -1)
expectedLength := 2

for _, submatch := range h.submatches {
if len(submatch) < expectedLength {
return h, errInvalidSubmatches
}

var x struct {
regex *regexp.Regexp
hashFn string
Expand All @@ -133,7 +148,13 @@ func getExifVar(str string) (exifVar, error) {

if exifRegex.MatchString(str) {
ex.submatches = exifRegex.FindAllStringSubmatch(str, -1)
expectedLength := 3

for _, submatch := range ex.submatches {
if len(submatch) < expectedLength {
return ex, errInvalidSubmatches
}

var x struct {
regex *regexp.Regexp
attr string
Expand Down Expand Up @@ -167,7 +188,13 @@ func getNumberVar(str string) (numberVar, error) {

if indexRegex.MatchString(str) {
nv.submatches = indexRegex.FindAllStringSubmatch(str, -1)
expectedLength := 7

for _, submatch := range nv.submatches {
if len(submatch) < expectedLength {
return nv, errInvalidSubmatches
}

var n struct {
regex *regexp.Regexp
startNumber int
Expand Down Expand Up @@ -246,63 +273,74 @@ func getNumberVar(str string) (numberVar, error) {
}

func getID3Var(str string) (id3Var, error) {
var i id3Var
var iv id3Var
if id3Regex.MatchString(str) {
i.submatches = id3Regex.FindAllStringSubmatch(str, -1)
for _, submatch := range i.submatches {
iv.submatches = id3Regex.FindAllStringSubmatch(str, -1)
expectedLength := 2

for _, submatch := range iv.submatches {
if len(submatch) < expectedLength {
return iv, errInvalidSubmatches
}

var x struct {
regex *regexp.Regexp
tag string
}
regex, err := regexp.Compile(submatch[0])
if err != nil {
return i, err
return iv, err
}

x.regex = regex
x.tag = submatch[1]

i.values = append(i.values, x)
iv.values = append(iv.values, x)
}
}

return i, nil
return iv, nil
}

func getRandomVar(str string) (randomVar, error) {
var rv randomVar

if randomRegex.MatchString(str) {
rv.submatches = randomRegex.FindAllStringSubmatch(str, -1)
expectedLength := 4

for _, submatch := range rv.submatches {
var r struct {
if len(submatch) < expectedLength {
return rv, errInvalidSubmatches
}

var val struct {
regex *regexp.Regexp
length int
characters string
}
r.length = 10
val.length = 10
regex, err := regexp.Compile(submatch[0])
if err != nil {
return rv, err
}
r.regex = regex
val.regex = regex

strLen := submatch[1]
if strLen != "" {
r.length, err = strconv.Atoi(strLen)
val.length, err = strconv.Atoi(strLen)
if err != nil {
return rv, err
}
}

r.characters = submatch[2]
val.characters = submatch[2]

if submatch[3] != "" {
r.characters = submatch[3]
val.characters = submatch[3]
}

rv.values = append(rv.values, r)
rv.values = append(rv.values, val)
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,7 @@ func getExifData(file string) (*Exif, error) {
return nil, err
}

defer func() {
ferr := f.Close()
if ferr != nil {
err = ferr
}
}()
defer f.Close()

exifData := &Exif{}
x, err := exif.Decode(f)
Expand Down Expand Up @@ -630,7 +625,7 @@ func (op *Operation) handleVariables(
str = out
}

if randomRegex.Match([]byte(str)) {
if randomRegex.MatchString(str) {
str = replaceRandomVariables(str, vars.random)
}

Expand Down

0 comments on commit 0796771

Please sign in to comment.