Add missing error checks in `getSheetMap` to fix panic(#1437)

Unit tests updated
This commit is contained in:
Liron Levin 2023-01-07 07:17:00 +02:00 committed by GitHub
parent b39626fae9
commit 9c3a5eb983
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 6 deletions

View File

@ -179,7 +179,9 @@ func OpenReader(r io.Reader, opts ...Options) (*File, error) {
if f.CalcChain, err = f.calcChainReader(); err != nil { if f.CalcChain, err = f.calcChainReader(); err != nil {
return f, err 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 { if f.Styles, err = f.stylesReader(); err != nil {
return f, err return f, err
} }

View File

@ -250,7 +250,10 @@ func TestOpenReader(t *testing.T) {
assert.NoError(t, zw.Close()) assert.NoError(t, zw.Close())
return buf return buf
} }
for _, defaultXMLPath := range []string{defaultXMLPathCalcChain, defaultXMLPathStyles} { for _, defaultXMLPath := range []string{
defaultXMLPathCalcChain,
defaultXMLPathStyles,
defaultXMLPathWorkbookRels} {
_, err = OpenReader(preset(defaultXMLPath)) _, err = OpenReader(preset(defaultXMLPath))
assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8") assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
} }

View File

@ -454,10 +454,16 @@ func (f *File) GetSheetList() (list []string) {
// getSheetMap provides a function to get worksheet name and XML file path map // getSheetMap provides a function to get worksheet name and XML file path map
// of the spreadsheet. // of the spreadsheet.
func (f *File) getSheetMap() map[string]string { func (f *File) getSheetMap() (map[string]string, error) {
maps := map[string]string{} maps := map[string]string{}
wb, _ := f.workbookReader() wb, err := f.workbookReader()
rels, _ := f.relsReader(f.getWorkbookRelsPath()) 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 _, v := range wb.Sheets.Sheet {
for _, rel := range rels.Relationships { for _, rel := range rels.Relationships {
if rel.ID == v.ID { if rel.ID == v.ID {
@ -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 // getSheetXMLPath provides a function to get XML file path by given sheet

View File

@ -378,6 +378,12 @@ func TestGetSheetMap(t *testing.T) {
} }
assert.Equal(t, len(sheetMap), 2) assert.Equal(t, len(sheetMap), 2)
assert.NoError(t, f.Close()) 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) { func TestSetActiveSheet(t *testing.T) {