Skip to content

Commit

Permalink
Add missing error checks in getSheetMap to fix panic(#1437)
Browse files Browse the repository at this point in the history
Unit tests updated
  • Loading branch information
liron-l authored Jan 7, 2023
1 parent b39626f commit 9c3a5eb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
4 changes: 3 additions & 1 deletion excelize.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ func OpenReader(r io.Reader, opts ...Options) (*File, error) {
if f.CalcChain, err = f.calcChainReader(); err != nil {
return f, err
}
f.sheetMap = f.getSheetMap()
if f.sheetMap, err = f.getSheetMap(); err != nil {
return f, err
}
if f.Styles, err = f.stylesReader(); err != nil {
return f, err
}
Expand Down
5 changes: 4 additions & 1 deletion excelize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,10 @@ func TestOpenReader(t *testing.T) {
assert.NoError(t, zw.Close())
return buf
}
for _, defaultXMLPath := range []string{defaultXMLPathCalcChain, defaultXMLPathStyles} {
for _, defaultXMLPath := range []string{
defaultXMLPathCalcChain,
defaultXMLPathStyles,
defaultXMLPathWorkbookRels} {
_, err = OpenReader(preset(defaultXMLPath))
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
}
Expand Down
14 changes: 10 additions & 4 deletions sheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,16 @@ func (f *File) GetSheetList() (list []string) {

// getSheetMap provides a function to get worksheet name and XML file path map
// of the spreadsheet.
func (f *File) getSheetMap() map[string]string {
func (f *File) getSheetMap() (map[string]string, error) {
maps := map[string]string{}
wb, _ := f.workbookReader()
rels, _ := f.relsReader(f.getWorkbookRelsPath())
wb, err := f.workbookReader()
if err != nil {
return nil, err
}
rels, err := f.relsReader(f.getWorkbookRelsPath())
if err != nil {
return nil, err
}
for _, v := range wb.Sheets.Sheet {
for _, rel := range rels.Relationships {
if rel.ID == v.ID {
Expand All @@ -471,7 +477,7 @@ func (f *File) getSheetMap() map[string]string {
}
}
}
return maps
return maps, nil
}

// getSheetXMLPath provides a function to get XML file path by given sheet
Expand Down
6 changes: 6 additions & 0 deletions sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ func TestGetSheetMap(t *testing.T) {
}
assert.Equal(t, len(sheetMap), 2)
assert.NoError(t, f.Close())

f = NewFile()
f.WorkBook = nil
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
_, err = f.getSheetMap()
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
}

func TestSetActiveSheet(t *testing.T) {
Expand Down

0 comments on commit 9c3a5eb

Please sign in to comment.