-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Fix panic in getSheetMap #1443
Conversation
Return early if relationship was not found for the given workbook
There was a problem hiding this 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
f5e08ff
to
7103167
Compare
Codecov Report
@@ 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
Flags with carried forward coverage won't be shown. Click here to find out more.
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Thanks @xuri, fixed! |
There was a problem hiding this 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.
…1443) - Check nil map in the getSheetMap function - Update unit tests
…1443) - Check nil map in the getSheetMap function - Update unit tests
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 nilDescription
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
Checklist