boubker elamri
boubker elamri

Reputation: 21

slice bounds out of range error and i don't have any slice variable

i'm working on a project and i had an error taht say slice bounds out of range but the lines that are concerned with this error don't seem to be a slive variable:

bt.blockDateTime =bt.getDate(bt.getDateBlockHeader(header[10:22]))
serviceType := hex2decimal(record[56:58])

and here are the function that i have created that i have used :

func(bt *Hc34) getDateBlockHeader(input string) string{
        year := input[0:2]
        quant:= hex2decimal(input[3:6])
        hour := input[6:len(input)];
        /*if err!=nil{
            panic(err)
        }*/
        return "20" + year + bt.getQuantDate(quant) + "" + hour
    }
func(bt *Hc34) getDate(date string) string{
        year := date[0:4]
        month := date[4:6]
        day := date[6:8]
        hour := date[8:10]
        minute := date[10:12]
        second := date[12:14]
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }
func(bt *Hc34) getQuantDate(quant int) string{
        nanoDayOfYear:=strconv.FormatInt(int64(quant)*24*60*60*1000*1000*1000, 10) + "ns"
        durt,err:=time.ParseDuration(nanoDayOfYear)
        t := time.Date(2009, time.January, 01, 00, 0, 0, 0, time.UTC)
        t=t.Add(durt)
        if err!=nil{

            panic(err)
        }
        return string(t.Month())+""+string(t.Day())

    }
func hex2decimal(hexStr string) int{
    integer,_:=strconv.ParseInt(hexStr,0,64)
    return int(integer)

}

here is the error lines: panic: runtime error: slice bounds out of range

goroutine 1 [running]:
github.com/bob96/hc34/beater.(*Hc34).hc34decoderfunc(0xc420238380, 0xc4202cc000, 0x2d128)
    /home/hp/src/github.com/bob96/hc34/beater/hc34.go:190 +0xb60
github.com/bob96/hc34/beater.(*Hc34).hc34DataHolderfunc(0xc420238380, 0xc42016e3c0, 0x5e)
    /home/hp/src/github.com/bob96/hc34/beater/hc34.go:141 +0xd3
github.com/bob96/hc34/beater.(*Hc34).Run(0xc420238380, 0xc420176000, 0xc4201760a8, 0xb)
    /home/hp/src/github.com/bob96/hc34/beater/hc34.go:105 +0x109
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.(*Beat).launch(0xc420176000, 0x9a84e8, 0x0, 0x0)
    /home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:211 +0x706
github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat.Run(0x9855d5, 0x4, 0x0, 0x0, 0x9a84e8, 0xc4200001a0, 0xc4200001a0)
    /home/hp/src/github.com/bob96/hc34/vendor/github.com/elastic/beats/libbeat/beat/beat.go:136 +0x65
main.main()
    /home/hp/src/github.com/bob96/hc34/main.go:12 +0x54

can some one help me please it will be a bless!

Upvotes: 1

Views: 6430

Answers (2)

Mayank Patel
Mayank Patel

Reputation: 8536

A string is a slice of characters in Go. That is why you are able to slice the string like record[56:58].

You need to check the length of the string before slicing it to avoid panics.

Something like

if len(string)<requiredLen{
  return
}
a := string[requiredLen-2:requiredLen]

Upvotes: 2

Konrad Zapałowicz
Konrad Zapałowicz

Reputation: 281

Well, whenever you write code like

x := y[a:b]

you are effectively slicing. I suggest you check the length of the "header" and "record" before you do any processing on it.

Hope it helps.

Upvotes: 2

Related Questions