dgageot/demoit

HTTP server panic "slice bounds out of range"

Opened this issue · 3 comments

I've been trying to set up a new project, but whenever I create a new project that's not called sample the HTTP server panics and dies the first time I try to view the page locally.

Steps to reproduce

  1. Clone the repository
  2. Create a new directory inside the repo, e.g. mkdir myproject
  3. Copy sample content from sample project into my new project, cp -prf sample/* myproject/*
  4. Run webserver: demoit myproject
  5. Open http://localhost:8888 in web browser and receive logs:
❯ demoit myproject
2019/10/18 16:30:03 Permitting clients to write input to the PTY.
2019/10/18 16:30:03 Welcome to DemoIt. Please, open http://localhost:8888
2019/10/18 16:30:03 "Dev Mode" to live reload your slides can be enabled with '--dev'
2019/10/18 16:30:03 HTTP server is listening at: http://127.0.0.1:9999/
2019/10/18 16:30:11 127.0.0.1:62745 200 GET /
2019/10/18 16:30:11 127.0.0.1:62748 200 GET /auth_token.js
2019/10/18 16:30:11 127.0.0.1:62749 200 GET /config.js
2019/10/18 16:30:11 127.0.0.1:62745 200 GET /css/index.css
2019/10/18 16:30:11 127.0.0.1:62747 200 GET /css/xterm_customize.css
2019/10/18 16:30:11 127.0.0.1:62746 200 GET /css/xterm.css
2019/10/18 16:30:11 127.0.0.1:62750 200 GET /js/gotty-bundle.js
2019/10/18 16:30:11 127.0.0.1:62750 200 GET /
2019/10/18 16:30:11 New client connected: 127.0.0.1:62751, connections: 1/0
2019/10/18 16:30:11 127.0.0.1:62750 200 GET /favicon.png
2019/10/18 16:30:11 127.0.0.1:62750 304 GET /css/index.css
2019/10/18 16:30:11 127.0.0.1:62746 304 GET /css/xterm_customize.css
2019/10/18 16:30:11 127.0.0.1:62750 304 GET /css/xterm.css
2019/10/18 16:30:11 127.0.0.1:62747 200 GET /auth_token.js
2019/10/18 16:30:11 127.0.0.1:62750 200 GET /config.js
2019/10/18 16:30:11 127.0.0.1:62746 304 GET /js/gotty-bundle.js
2019/10/18 16:30:11 Connection closed by local command: 127.0.0.1:62751, connections: 0/0
2019/10/18 16:30:12 New client connected: 127.0.0.1:62752, connections: 1/0
2019/10/18 16:30:12 Connection closed by local command: 127.0.0.1:62752, connections: 0/0
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62759: runtime error: slice bounds out of range [:10] with length 0
goroutine 29 [running]:
net/http.(*conn).serve.func1(0xc0000aa140)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000026b60)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc00044f800, 0x5a, 0x5a, 0x2, 0xc0001d0088)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc0001880e0, 0xc00028a100)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc0001880e0, 0xc00028a100)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc0001880e0, 0xc00011ed00)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc0001880e0, 0xc00011ed00)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000aa140, 0x18056a0, 0xc0001b40c0)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62760: runtime error: slice bounds out of range [:10] with length 0
goroutine 31 [running]:
net/http.(*conn).serve.func1(0xc0000aa320)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc0003141c0)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0004a1500, 0x5a, 0x5a, 0x2, 0xc0005ae088)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc0000a2000, 0xc00043c100)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc0000a2000, 0xc00043c100)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc0000a2000, 0xc0002d3400)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc0000a2000, 0xc0002d3400)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000aa320, 0x18056a0, 0xc000231000)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62761: runtime error: slice bounds out of range [:10] with length 0
goroutine 73 [running]:
net/http.(*conn).serve.func1(0xc0001bc0a0)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000334320)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc000310000, 0x5a, 0x5a, 0x2, 0xc0001d01a8)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000188380, 0xc00028a700)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000188380, 0xc00028a700)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000188380, 0xc00028a400)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000188380, 0xc00028a400)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0001bc0a0, 0x18056a0, 0xc000078f00)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62762: runtime error: slice bounds out of range [:10] with length 0
goroutine 33 [running]:
net/http.(*conn).serve.func1(0xc0000aa500)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000314280)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0004a1c00, 0x5a, 0x5a, 0x2, 0xc0001d02c8)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000188460, 0xc00028a900)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000188460, 0xc00028a900)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000188460, 0xc00043c200)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000188460, 0xc00043c200)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000aa500, 0x18056a0, 0xc0001b4cc0)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62763: runtime error: slice bounds out of range [:10] with length 0
goroutine 74 [running]:
net/http.(*conn).serve.func1(0xc0001bc1e0)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000314380)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0004a2300, 0x5a, 0x5a, 0x2, 0xc0001d03e8)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000188540, 0xc00028ab00)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000188540, 0xc00028ab00)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000188540, 0xc00043c300)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000188540, 0xc00043c300)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0001bc1e0, 0x18056a0, 0xc0001b4ec0)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62764: runtime error: slice bounds out of range [:10] with length 0
goroutine 84 [running]:
net/http.(*conn).serve.func1(0xc0000aa6e0)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000314420)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0004a2a00, 0x5a, 0x5a, 0x2, 0xc000276088)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000148460, 0xc0001c8b00)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000148460, 0xc0001c8b00)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000148460, 0xc00043c500)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000148460, 0xc00043c500)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000aa6e0, 0x18056a0, 0xc0001b5040)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62765: runtime error: slice bounds out of range [:10] with length 0
goroutine 75 [running]:
net/http.(*conn).serve.func1(0xc0001bc280)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000027220)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0003e2000, 0x5a, 0x5a, 0x2, 0xc0000d3708)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000280380, 0xc0003e0000)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000280380, 0xc0003e0000)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000280380, 0xc0003a4000)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000280380, 0xc0003a4000)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0001bc280, 0x18056a0, 0xc0002fc580)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62766: runtime error: slice bounds out of range [:10] with length 0
goroutine 77 [running]:
net/http.(*conn).serve.func1(0xc0001bc320)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc000027300)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc000454000, 0x5a, 0x5a, 0x2, 0xc0000d3828)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000280460, 0xc0003e0200)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000280460, 0xc0003e0200)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000280460, 0xc00028ac00)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000280460, 0xc00028ac00)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0001bc320, 0x18056a0, 0xc0000797c0)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62767: runtime error: slice bounds out of range [:10] with length 0
goroutine 79 [running]:
net/http.(*conn).serve.func1(0xc0001bc3c0)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc0003147c0)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0004a3100, 0x5a, 0x5a, 0x2, 0xc0002761a8)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000148540, 0xc0003a4200)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000148540, 0xc0003a4200)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000148540, 0xc00043c700)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000148540, 0xc00043c700)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0001bc3c0, 0x18056a0, 0xc0001b5180)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e
2019/10/18 16:30:18 http: panic serving 127.0.0.1:62768: runtime error: slice bounds out of range [:10] with length 0
goroutine 49 [running]:
net/http.(*conn).serve.func1(0xc0000cd4a0)
	/usr/local/go/src/net/http/server.go:1767 +0x139
panic(0x1669de0, 0xc0000273a0)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/dgageot/demoit/templates.hash(...)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:48
github.com/dgageot/demoit/templates.Index(0xc0003e2700, 0x5a, 0x5a, 0x2, 0xc0000d3948)
	/Users/pjlewis/Repositories/demoit-presentations/templates/index.go:28 +0x155
github.com/dgageot/demoit/handlers.Step(0x1804820, 0xc000280620, 0xc0003e0400)
	/Users/pjlewis/Repositories/demoit-presentations/handlers/step.go:66 +0x205
net/http.HandlerFunc.ServeHTTP(0x173ed20, 0x1804820, 0xc000280620, 0xc0003e0400)
	/usr/local/go/src/net/http/server.go:2007 +0x44
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000134000, 0x1804820, 0xc000280620, 0xc00043c800)
	/Users/pjlewis/go/pkg/mod/github.com/gorilla/mux@v1.7.3/mux.go:212 +0xe2
net/http.serverHandler.ServeHTTP(0xc000188000, 0x1804820, 0xc000280620, 0xc00043c800)
	/usr/local/go/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0000cd4a0, 0x18056a0, 0xc0001b5600)
	/usr/local/go/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2927 +0x38e

Note that running demoit sample from the repo root works fine, and I'm able to browse without problem.

When I follow your steps (minus the trailing star at cp -prf sample/* myproject/) I do reproduce the crash. I'll have a look at this.

Demoit relies on a hidden folder .demoit next to demoit.html. cp -prf sample/* myproject/ does not copy it, while cp -prf sample myproject does.

I think the root cause is clear and we can close the issue.

@pjlewisuk while having your project folder in demoit's repo root is possible, I suggest to let it live somewhere else in your filesystem.

I can think of 2 improvements we could make to demoit's code:

  • not panic at templates/index.go:48, as the func comment suggests it was supposed to be fault-tolerant
  • maybe print a human friendly error when the folder .demoit is missing, or a necessary file inside .demoit is missing