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

file.AutoFilter() can sometimes error out with a "nil pointer dereference". #1655

Closed
blakepatteson opened this issue Sep 8, 2023 · 2 comments
Labels
bug Something isn't working
Projects

Comments

@blakepatteson
Copy link

Description

file.AutoFilter can sometimes error out with a "nil pointer dereference".

I think it's just due to this one bad file and on most files, "file.AutoFilter()" works fine, but it did happen with this file and I couldn't figure out why at first.

Steps to reproduce the issue:
Use this test repo I made with the bad file :

  1. git clone "https://github.com/blakepatteson/autofilterIssueTest"
  2. cd autofilterIssueTest
  3. go run .

Describe the results you received:

Got a nil pointer dereference panic.

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x56c480]

goroutine 1 [running]:
github.com/xuri/excelize/v2.(*File).AutoFilter(0x5fce49?, {0x5f9bf4, 0x4}, {0xc000353f37?, 0x2?}, {0x0, 0x0, 0x0})
        C:/Users/bpatt/go/pkg/mod/github.com/xuri/excelize/v2@v2.8.0/table.go:454 +0x4c0
main.main()
        C:/zzz/repos/autofilterIssueTest/main.go:17 +0x105
exit status 2

The issue seems to be with this part being possibly nil :
*definedName.LocalSheetID == sheetID

So I think the fix would be just to add a check for this being nil?

Pretty sure the issue is with the "definedNames" part of this specific "test-autofilter-issue.xlsx" workbook :

<definedNames>
    <definedName name="_xlnm._FilterDatabase" localSheetId="0" hidden="1">test!#REF!</definedName>
    <definedName name="_xlnm._FilterDatabase" hidden="1">'[1]Error Sheet'!$A$1:$I$1</definedName>
    <definedName name="Arial">#REF!</definedName>
</definedNames>

Describe the results you expected:

Expected Excelize to put a filter on the "test" worksheet with the provided parameters :

err = errWkbk.AutoFilter(sheetNm, fmt.Sprintf("A1:%v%v", "E", 5), nil)

Output of go version:

go version go1.21.1 windows/amd64

Excelize version or commit ID:

github.com/xuri/excelize/v2 v2.8.0

Environment details (OS, Microsoft Excel™ version, physical, etc.):

Here's the output of "go env" :

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\bpatt\AppData\Local\go-build
set GOENV=C:\Users\bpatt\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\bpatt\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\bpatt\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.1
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\zzz\repos\autofilterIssueTest\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\bpatt\AppData\Local\Temp\go-build164768209=/tmp/go-build -gno-record-gcc-switches
@xuri xuri added bug Something isn't working in progress Working in progress labels Sep 9, 2023
@xuri xuri added this to Bugfix in v2.8.1 Sep 9, 2023
@xuri xuri closed this as completed in 49706c9 Sep 9, 2023
@xuri
Copy link
Member

xuri commented Sep 9, 2023

Thanks for your issue. I have fixed this issue, please try to upgrade the master branch code, and this patch will be released in the next version.

@xuri xuri removed the in progress Working in progress label Sep 9, 2023
@blakepatteson
Copy link
Author

Awesome - thanks so much!

jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this issue Oct 22, 2023
- Breaking changes, change the data type for the `HeaderFooterOptions` structure fields `AlignWithMargins` and `ScaleWithDoc` as a pointer
- Fixed panic on `AutoFilter` by adding nil pointer guard for local sheet ID
- Allow dot character in the defined name, table name, or pivot table name
- Update the unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
No open projects
v2.8.1
Bugfix
Development

No branches or pull requests

2 participants