From 00c58a73f32e1e8e176abee6f775b865c542e52d Mon Sep 17 00:00:00 2001 From: Liron Levin Date: Wed, 11 Jan 2023 18:14:38 +0200 Subject: [PATCH] Fix panic caused by the workbook relationship part not exist (#1443) - Check nil map in the getSheetMap function - Update unit tests --- excelize_test.go | 30 +++++++++++++++++++++++------- sheet.go | 3 +++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/excelize_test.go b/excelize_test.go index 63d96f4..7e19c5b 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -228,14 +228,18 @@ func TestOpenReader(t *testing.T) { _, err = OpenReader(bytes.NewReader(oleIdentifier), Options{Password: "password", UnzipXMLSizeLimit: UnzipSizeLimit + 1}) assert.EqualError(t, err, ErrWorkbookFileFormat.Error()) - // Test open workbook with unsupported charset internal calculation chain - preset := func(filePath string) *bytes.Buffer { + // Prepare unusual workbook, made the specified internal XML parts missing + // or contain unsupported charset + preset := func(filePath string, notExist bool) *bytes.Buffer { source, err := zip.OpenReader(filepath.Join("test", "Book1.xlsx")) assert.NoError(t, err) buf := new(bytes.Buffer) zw := zip.NewWriter(buf) for _, item := range source.File { // The following statements can be simplified as zw.Copy(item) in go1.17 + if notExist && item.Name == filePath { + continue + } writer, err := zw.Create(item.Name) assert.NoError(t, err) readerCloser, err := item.Open() @@ -243,21 +247,33 @@ func TestOpenReader(t *testing.T) { _, err = io.Copy(writer, readerCloser) assert.NoError(t, err) } - fi, err := zw.Create(filePath) - assert.NoError(t, err) - _, err = fi.Write(MacintoshCyrillicCharset) - assert.NoError(t, err) + if !notExist { + fi, err := zw.Create(filePath) + assert.NoError(t, err) + _, err = fi.Write(MacintoshCyrillicCharset) + assert.NoError(t, err) + } assert.NoError(t, zw.Close()) return buf } + // Test open workbook with unsupported charset internal XML parts for _, defaultXMLPath := range []string{ defaultXMLPathCalcChain, defaultXMLPathStyles, defaultXMLPathWorkbookRels, } { - _, err = OpenReader(preset(defaultXMLPath)) + _, err = OpenReader(preset(defaultXMLPath, false)) assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8") } + // Test open workbook without internal XML parts + for _, defaultXMLPath := range []string{ + defaultXMLPathCalcChain, + defaultXMLPathStyles, + defaultXMLPathWorkbookRels, + } { + _, err = OpenReader(preset(defaultXMLPath, true)) + assert.NoError(t, err) + } // Test open spreadsheet with unzip size limit _, err = OpenFile(filepath.Join("test", "Book1.xlsx"), Options{UnzipSizeLimit: 100}) diff --git a/sheet.go b/sheet.go index a4be16a..9d1f5f9 100644 --- a/sheet.go +++ b/sheet.go @@ -464,6 +464,9 @@ func (f *File) getSheetMap() (map[string]string, error) { if err != nil { return nil, err } + if rels == nil { + return maps, nil + } for _, v := range wb.Sheets.Sheet { for _, rel := range rels.Relationships { if rel.ID == v.ID {