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

Fix panic in getSheetMap #1443

Merged
merged 2 commits into from
Jan 11, 2023
Merged

Fix panic in getSheetMap #1443

merged 2 commits into from
Jan 11, 2023

Conversation

liron-l
Copy link
Contributor

@liron-l liron-l commented Jan 10, 2023

Return early if relationship was not found for the given workbook

PR Details

Fix panic in getSheetMap where we don't check if relationship map is nil

Description

Return early on nil relationship

Related Issue

Motivation and Context

Code panics

How Has This Been Tested

UT + manual against malformed files

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Return early if relationship was not found for the
given workbook
@xuri xuri added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jan 10, 2023
Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your pull request. I suggest testing this in TestOpenReader function, preparing an unusual workbook, made the specified internal XML parts missing or containing unsupported charset, instead of adding a new test function TestCorruptedRelationship and attachment, the patch based on the current code maybe like this, what do you think about?

diff --git a/excelize_test.go b/excelize_test.go
index 63d96f4..3e2e097 100644
--- a/excelize_test.go
+++ b/excelize_test.go
@@ -227,15 +227,18 @@ func TestOpenReader(t *testing.T) {
    assert.EqualError(t, err, zip.ErrFormat.Error())
    _, 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 +246,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_test.go b/sheet_test.go
index e393c88..f809fe8 100644
--- a/sheet_test.go
+++ b/sheet_test.go
@@ -386,14 +386,6 @@ func TestGetSheetMap(t *testing.T) {
    assert.EqualError(t, err, "XML syntax error on line 1: invalid UTF-8")
 }
 
-func TestGetSheetMapWithoutRelationship(t *testing.T) {
-   // Sheet where workbook relationship is missing
-   f, err := OpenFile(filepath.Join("test", "WorkbookWithoutRel.xlsx"))
-   assert.NoError(t, err)
-   sheetMap := f.GetSheetMap()
-   assert.Equal(t, sheetMap, map[int]string{1: "Sheet1"})
-}
-
 func TestSetActiveSheet(t *testing.T) {
    f := NewFile()
    f.WorkBook.BookViews = nil

@liron-l liron-l force-pushed the fix_get_sheet_panic branch from f5e08ff to 7103167 Compare January 11, 2023 12:53
@codecov-commenter
Copy link

codecov-commenter commented Jan 11, 2023

Codecov Report

Merging #1443 (7103167) into master (14d7acd) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1443   +/-   ##
=======================================
  Coverage   98.61%   98.61%           
=======================================
  Files          31       31           
  Lines       24015    24018    +3     
=======================================
+ Hits        23682    23685    +3     
  Misses        220      220           
  Partials      113      113           
Flag Coverage Δ
unittests 98.61% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
sheet.go 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@liron-l liron-l requested a review from xuri January 11, 2023 12:53
@liron-l
Copy link
Contributor Author

liron-l commented Jan 11, 2023

Thanks @xuri, fixed!

Copy link
Member

@xuri xuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your contribution.

@xuri xuri merged commit 00c58a7 into qax-os:master Jan 11, 2023
xuri pushed a commit to JDavidVR/excelize that referenced this pull request Jul 11, 2023
…1443)

- Check nil map in the getSheetMap function
- Update unit tests
jenbonzhang pushed a commit to jenbonzhang/excelize that referenced this pull request Oct 22, 2023
…1443)

- Check nil map in the getSheetMap function
- Update unit tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants