Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault running swag init after 1.8.5 release #1309

Open
mackrorysd opened this issue Aug 26, 2022 · 13 comments
Open

Segmentation fault running swag init after 1.8.5 release #1309

mackrorysd opened this issue Aug 26, 2022 · 13 comments

Comments

@mackrorysd
Copy link

mackrorysd commented Aug 26, 2022

Describe the bug
Since 1.8.5 was released, when running swag init --parseDependency true on my code I encounter a segfault. After switching back to 1.8.4 (or other versions in the 1.8 line) the command completes successfully.

To Reproduce
I'm unable to share the entire code this happens on. I will see if I can isolate this to a small code sample I can share.

Stack trace

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x85c8a8]

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0xc00000e7c8, 0xc00035b980, {0xc0003e15c0, 0x18}, 0xc000cff680)
	/home/sean/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0xa8
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0xc00000e7c8)
	/home/sean/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xc9
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0xc0001e22a0, {0xc0001d7fb0?, 0x1?, 0x0?}, {0x9587e8?, 0x7?}, 0x64)
	/home/sean/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x3bf
github.com/swaggo/swag/gen.(*Gen).Build(0xc0001db980, 0xc0001e1790)
	/home/sean/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x5c9
main.initAction(0xc0001af680?)
	/home/sean/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x757
github.com/urfave/cli/v2.(*Command).Run(0xc0001ad560, 0xc0001ec580)
	/home/sean/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x5bb
github.com/urfave/cli/v2.(*App).RunContext(0xc000204000, {0xa25860?, 0xc0000240f0}, {0xc000020080, 0x4, 0x4})
	/home/sean/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0xb48
github.com/urfave/cli/v2.(*App).Run(...)
	/home/sean/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
	/home/sean/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x55d

Your swag version
1.8.5 (1.8.4 and earlier worked)

Go version: 1.18

Desktop (please complete the following information):

  • OS: Pop! OS 22.04 (essentially Ubuntu)
@nekomeowww
Copy link

I got this panic while trying to use it in GitHub Actions too today.

Run swag init  --parseDependency --parseInternal
  swag init  --parseDependency --parseInternal
  shell: /usr/bin/bash -e {0}
[2](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:2)022/08/26 0[3](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:3):01:17 Generate swagger docs....
2022/08/26 03:01:17 Generate general API Info, search dir:./
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x87dc68]

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0xc0000a2798, 0xc002bc[4](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:5)900, {0xc000b34c84, 0xa}, 0xc0163b[5](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:6)b[6](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:7)0)
	/home/runner/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0xa8
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0xc0000a2[7](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:8)9[8](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:9))
	/home/runner/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xc[9](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:10)
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0xc0001582a0, {0xc00014bfc0?, 0x1?, 0x0?}, {0x981e72?, 0x7?}, 0x64)
	/home/runner/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x3bf
github.com/swaggo/swag/gen.(*Gen).Build(0xc0001518f0, 0xc000152c30)
	/home/runner/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x5c9
main.initAction(0xc000[12](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:13)1680?)
	/home/runner/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x757
github.com/urfave/cli/v2.(*Command).Run(0xc000128ea0, 0xc000166380)
	/home/runner/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x5dc
github.com/urfave/cli/v2.(*App).RunContext(0xc0001601a0, {0xa56670?, 0xc0000a8000}, {0xc000086040, 0x4, 0x4})
	/home/runner/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:3[13](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:14) +0xb7d
github.com/urfave/cli/v2.(*App).Run(...)
	/home/runner/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
	/home/runner/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:2[21](<org>/<repo>/runs/8029066662?check_suite_focus=true#step:9:22) +0x5c5
Error: Process completed with exit code 2.

@zetaab
Copy link

zetaab commented Aug 26, 2022

we are seeing same go 1.18 + ubuntu 20

@zetaab
Copy link

zetaab commented Aug 26, 2022

ping @ubogdan

@zetaab
Copy link

zetaab commented Aug 26, 2022

the PR that is breaking this is maybe #1283

@liut
Copy link

liut commented Aug 26, 2022

I also got this panic too! 1.8.5.
And 1.8.4 also failed.
It seems that the latest stable commit is
af1c525.

trace:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x1477b68]

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0xc000010858, 0xc002954000, {0xc0010d2570, 0x15}, 0xc002e7b8f0)
	/go/src/github.com/swaggo/swag/packages.go:168 +0xa8
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0xc000010858)
	/go/src/github.com/swaggo/swag/packages.go:110 +0xc9
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0xc00019e2a0, {0xc00011f600?, 0x2?, 0x0?}, {0x7ff7bfeff2d5?, 0x11?}, 0x64)
	/go/src/github.com/swaggo/swag/parser.go:362 +0x3bf
github.com/swaggo/swag/gen.(*Gen).Build(0xc0001a14d0, 0xc000198a90)
	/go/src/github.com/swaggo/swag/gen/gen.go:177 +0x5c9
main.initAction(0xc000171680?)
	/go/src/github.com/swaggo/swag/cmd/swag/main.go:151 +0x757
github.com/urfave/cli/v2.(*Command).Run(0xc000176c60, 0xc0001c6140)
	/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x5dc
github.com/urfave/cli/v2.(*App).RunContext(0xc000194820, {0x164fb10?, 0xc000026138}, {0xc000022090, 0x9, 0x9})
	/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0xb7d
github.com/urfave/cli/v2.(*App).Run(...)
	/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
	/go/src/github.com/swaggo/swag/cmd/swag/main.go:221 +0x5c5

go1.19 darwin/amd64

@pietrodicaprio
Copy link

Same here

#15 23.59 swag init --parseDependency --parseInternal
#15 23.60 2022/08/26 07:44:07 Generate swagger docs....
#15 23.60 2022/08/26 07:44:07 Generate general API Info, search dir:./
#15 25.93 panic: runtime error: invalid memory address or nil pointer dereference
#15 25.93 [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x823508]
#15 25.93
#15 25.93 goroutine 1 [running]:
#15 25.93 github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0xc00000eab0, 0xc001295800, {0xc0004eac04, 0x9}, 0xc010122450)
#15 25.93       /go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0xa8
#15 25.93 github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0xc00000eab0)
#15 25.93       /go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xc9
#15 25.93 github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0xc0001b42a0, {0xc0001f0440, 0x1, 0x0}, {0x94ac56, 0x7}, 0x64)
#15 25.93       /go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x3da
#15 25.93 github.com/swaggo/swag/gen.(*Gen).Build(0xc0001bd5c0, 0xc0001b3930)
#15 25.93       /go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x5af
#15 25.93 main.initAction(0xc000189680)
#15 25.93       /go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x6f7
#15 25.93 github.com/urfave/cli/v2.(*Command).Run(0xc000189680, 0xc0001d4480)
#15 25.93       /go/pkg/mod/github.com/urfave/cli/v2@v2.11.2/command.go:173 +0x738
#15 25.93 github.com/urfave/cli/v2.(*App).RunContext(0xc0001a8700, {0x9fd5f0, 0xc000024070}, {0xc000020080, 0x4, 0x4})
#15 25.93       /go/pkg/mod/github.com/urfave/cli/v2@v2.11.2/app.go:382 +0xe0c
#15 25.93 github.com/urfave/cli/v2.(*App).Run(...)
#15 25.93       /go/pkg/mod/github.com/urfave/cli/v2@v2.11.2/app.go:251
#15 25.93 main.main()
#15 25.93       /go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x576
#15 25.94 make: *** [Makefile:51: docs] Error 2

@rytsh
Copy link
Contributor

rytsh commented Aug 26, 2022

if funcDeclaration, ok := astDeclaration.(*ast.FuncDecl); ok {

In this kind of checks, we should check funcDeclaration is not nil
So ok mean is fit the type but still it can be nil???

I was thinking the

if funcDeclaration, _ := astDeclaration.(*ast.FuncDecl); funcDeclaration != nil {

EDITED

Looks like I am wrong in here, not working like that in just sending nil without typecasting the pointer stuct of nil, anyways:)

@dgbittner
Copy link

Also experiencing this issue with the latest release.

@mikolajsemeniuk
Copy link

mikolajsemeniuk commented Aug 26, 2022

Same here worked for 1.8.4 but after releasing 1.8.5 got this error.

2022/08/26 13:12:22 Generate swagger docs....
2022/08/26 13:12:22 Generate general API Info, search dir:./
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x10114a988]

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0x140001a8828, 0x14000d9c280, {0x140008c5bc0, 0x15}, 0x14001fce690)
	/Users/mikolaj/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x98
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0x140001a8828)
	/Users/mikolaj/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0x140002602a0, {0x1400028e070?, 0x1?, 0x0?}, {0x1011741a1?, 0x7?}, 0x64)
	/Users/mikolaj/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x2b0
github.com/swaggo/swag/gen.(*Gen).Build(0x140002599e0, 0x1400025f2b0)
	/Users/mikolaj/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a8
main.initAction(0x1400022f8c0?)
	/Users/mikolaj/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x658
github.com/urfave/cli/v2.(*Command).Run(0x1400022d560, 0x1400026c4c0)
	/Users/mikolaj/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x50c
github.com/urfave/cli/v2.(*App).RunContext(0x140002531e0, {0x1013199a0?, 0x140001b4008}, {0x1400019c180, 0x3, 0x3})
	/Users/mikolaj/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x888
github.com/urfave/cli/v2.(*App).Run(...)
	/Users/mikolaj/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
	/Users/mikolaj/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x660

After downgrading via go install github.com/swaggo/swag/cmd/swag@v1.8.4 swag init works again

@mstrYoda
Copy link
Contributor

mstrYoda commented Aug 26, 2022

Hi all, this is related to a development that was done by me merged lastly.

I am investigating it right now, and working on it 🙏

Edit: I just saw that it is fixed here: #1310

The root cause of this error is that the functions that have no body declaration. For example when you declare a function with //go:linkname without body decleration like here. While parsing dependencies swag faces with this functions and panics :( My mistake, sorry guys. Could not imagine that can happen.

//go:linkname copySlice reflect.typedslicecopy
func copySlice(elemType *runtime.Type, dst, src sliceHeader) int

I wrote a story about this problem: https://itnext.io/my-contribution-to-a-popular-open-source-package-caused-a-panic-in-golang-projects-4d34394df4cf

@ubogdan
Copy link
Contributor

ubogdan commented Sep 17, 2022

v1.8.6 released.

@magandrez
Copy link

Verified as fixed with v1.8.6

Thanks.

@tjsampson
Copy link

tjsampson commented Dec 5, 2022

Fixed in 1.8.6, however there was a regression on 1.8.8:

[10:37:45] tsampson:cloud-api git:(main) $ swag --version                                                            
swag version v1.8.8
[10:38:47] tsampson:cloud-api git:(main) $ swag init --parseDependency --parseInternal --parseDepth 1 --dir ./cmd/api
2022/12/05 10:39:00 Generate swagger docs....
2022/12/05 10:39:00 Generate general API Info, search dir:./cmd/api
panic: runtime error: negative shift amount

goroutine 1 [running]:
github.com/swaggo/swag.(*PackageDefinitions).evaluateConstValue(0xc001a93bd0, 0xc00c6f7a70?, 0xc00ee44d10?, {0x165c920?, 0xc001ad2090?}, {0x165aec0, 0xc0001bf820}, 0x55?)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/package.go:146 +0x102b
github.com/swaggo/swag.(*PackagesDefinitions).EvaluateConstValue(0xc00bffb698?, 0xc001a93bd0, 0xc01a6a73c0, 0x0)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/packages.go:282 +0x19d
github.com/swaggo/swag.(*PackagesDefinitions).evaluateAllConstVariables(...)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/packages.go:265
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0xc0001bf820)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/packages.go:110 +0x254
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0xc0002820c0, {0xc00027e0d0?, 0x1?, 0x0?}, {0x1589292?, 0x7?}, 0x1)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/parser.go:375 +0x3bf
github.com/swaggo/swag/gen.(*Gen).Build(0xc000249920, 0xc0002442a0)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/gen/gen.go:182 +0x637
main.initAction(0xc00026c780?)
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/cmd/swag/main.go:158 +0x7bd
github.com/urfave/cli/v2.(*Command).Run(0xc000222a20, 0xc00026a300)
        /Users/tsampson/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x5dc
github.com/urfave/cli/v2.(*App).RunContext(0xc000187d40, {0x165d930?, 0xc0001a6008}, {0xc0001ae000, 0x8, 0x8})
        /Users/tsampson/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0xb7d
github.com/urfave/cli/v2.(*App).Run(...)
        /Users/tsampson/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
        /Users/tsampson/go/pkg/mod/github.com/swaggo/swag@v1.8.8/cmd/swag/main.go:229 +0x5c5
[10:39:03] tsampson:cloud-api git:(main) $ go install github.com/swaggo/swag/cmd/swag@v1.8.6                         
[10:39:48] tsampson:cloud-api git:(main) $ swag --version                                                            
swag version v1.8.6
[10:39:51] tsampson:cloud-api git:(main) $ swag init --parseDependency --parseInternal --parseDepth 1 --dir ./cmd/api
2022/12/05 10:39:54 Generate swagger docs....
2022/12/05 10:39:54 Generate general API Info, search dir:./cmd/api
2022/12/05 10:39:57 Generating errs.HTTPError
2022/12/05 10:39:57 Generating schema.Health
2022/12/05 10:39:57 Generating version.Metadata
2022/12/05 10:39:57 create docs.go at  docs/docs.go
2022/12/05 10:39:57 create swagger.json at  docs/swagger.json
2022/12/05 10:39:58 create swagger.yaml at  docs/swagger.yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests