golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.41k stars 17.71k forks source link

panic: runtime error: index out of range , when there are ~ 9k elements in array. #17910

Closed ralic closed 8 years ago

ralic commented 8 years ago

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.7.3

What operating system and processor architecture are you using (go env)?

GOARCH="amd64" GOBIN="" GOEXE="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GOOS="darwin" GOPATH="/usr/local/opt/go/libexec/bin:/usr/local/go" GORACE="" GOROOT="/usr/local/Cellar/go/1.7.3/libexec" GOTOOLDIR="/usr/local/Cellar/go/1.7.3/libexec/pkg/tool/darwin_amd64" CC="clang" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/d6/lnrxqp194zn2c1g3_02j1pdh0000gn/T/go-build196018249=/tmp/go-build -gno-record-gcc-switches -fno-common" CXX="clang++" CGO_ENABLED="1"

What did you do?

source code -- http://tinyurl.com/hnzm7nd

What did you expect to see?

https://www.hackerrank.com/challenges/ctci-array-left-rotation All test case passed.

What did you see instead?

failed in test case 6, 8, 9

[Error Messages]

panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x9a840, 0xc42000a110)
    /usr/local/Cellar/go/1.7.3/libexec/src/runtime/panic.go:500 +0x1a1
main.main()
    /Users/raliclo/work/@Netbeans/HackerRank_Practices/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation.go:25 +0x5ff
exit status 2
odeke-em commented 8 years ago

@ralic the error you are getting seems to come from your code, according to the stack trace ie on line 25, which is from https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation.go#L25

line2 := strings.Split(lines[1], " ")

could you confirm that you actually get more that one line as a sanity check in your code?

namKolo commented 8 years ago

I have the same issue with @ralic @odeke-em With test cases, the second line always exists . P/S sorry, that was my mistake. I found the issue :)

odeke-em commented 8 years ago
P/S sorry, that was my mistake. I found the issue :)

@namKolo, so you found the issue in your code? Does this issue still stand then?

@ralic and @namKolo could y'all perhaps reproduce this by making a repository with the inputs that caused the failure? Otherwise we won't be able to diagnose what's going on easily. Thanks.

ralic commented 8 years ago

@Emmanuel,

Input : https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Input_for_Array_Left_Rotation.txt

Output : https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Output_for_arrays_left_rotation.txt

Failed Code for this large array : https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation.go

Working Code for the same input : https://github.com/ralic/HackerRank_Practices/blob/master/TestNG-1/src/main/golang/CrackingTheCodingInterview/Arrays_LeftRotation_2.go

------------------------------------------------------------------------ --- Phone: 408-609-7628 Email: RalicLo@gmail.com ------------------------------------------------------------------------ ---

--

On Tue, Nov 15, 2016 at 8:00 PM, Emmanuel T Odeke notifications@github.com wrote:

P/S sorry, that was my mistake. I found the issue :)

@namKolo https://github.com/namKolo, so you found the issue in your code? Does this issue still stand then?

@ralic https://github.com/ralic and @namKolo https://github.com/namKolo could y'all perhaps reproduce this by making a repository with the inputs that caused the failure? Otherwise we won't be able to diagnose what's going on easily. Thanks.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/golang/go/issues/17910#issuecomment-260847848, or mute the thread https://github.com/notifications/unsubscribe-auth/AGbukq4AUZEJPRYbQ06ehJXGQfwi7k1_ks5q-n_EgaJpZM4KxC7X .

odeke-em commented 8 years ago

@ralic thank you for the repro. Okay so the problem is that bufio.Scanner returns a bufio.ErrTokenTooLong error after that scan loop, yet that error wasn't being checked for early enough (it was checked way too late down the program) hence the runtime panic because you only got one line instead of two. The diff below catches the error

--- orig.go 2016-11-16 20:08:58.000000000 -0800
+++ fixed.go    2016-11-16 20:23:51.000000000 -0800
@@ -6,6 +6,7 @@
    "os"
    "fmt"
    "strings"
+   "log"
    "strconv"
 )

@@ -21,6 +22,10 @@
        lines = append(lines, scanner.Text())
    }

+   if err := scanner.Err(); err != nil {
+       log.Fatalf("err reading standard input: %v", err)
+   }
+
    line1 := strings.Split(lines[0], " ")
    line2 := strings.Split(lines[1], " ")
    //fmt.Println(line1)
@@ -37,11 +42,6 @@
        }
    }

-   if err := scanner.Err(); err != nil {
-       fmt.Fprintln(os.Stderr, "reading standard input:", err)
-   }
-
-
    // Performance Report
    //elapsed := time.Since(start)
    //fmt.Println("Time Elapsed:", elapsed)

and the example on bufio.Scanner also shows how to catch that error https://golang.org/pkg/bufio/#example_Scanner_lines.

To get your program to deal with the multitude of elements that are in the single line, I instead used bufio.NewReader and then Reader.ReadString('\n') like this https://play.golang.org/p/WGfyqmVk79.

I don't see anything more that we can do on the Go side, except encourage checking errors early, otherwise this isn't a bug in the Go stdlib packages.

ralic commented 8 years ago

Hi Emmanuel,

Thank you so much for catching my need, I would also start using bufferedReader (newReader) in most cases, instead of newScanner for data array that may have unknown size.

If the case of fixed size array, it seems to be a good workaround using make() to initiate it and then get each value by fmt.Scan() , which works very well to replace the scanner with the flexibility to define the data input type either to be double/string ... for the large array as well.

Sincerely, Ralic

--

On Wed, Nov 16, 2016 at 8:44 PM, Emmanuel T Odeke notifications@github.com wrote:

Closed #17910 https://github.com/golang/go/issues/17910.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/golang/go/issues/17910#event-862233522, or mute the thread https://github.com/notifications/unsubscribe-auth/AGbukvookgbC3y7c0q34mm2QJ1LDs7Kdks5q-9ulgaJpZM4KxC7X .