Skip to content

Commit 2ae6313

Browse files
committed
add limits for total columns, row and filename length
1 parent c168233 commit 2ae6313

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

cell.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func (f *File) SetCellStr(sheet, axis, value string) error {
281281
// setCellString provides a function to set string type to shared string
282282
// table.
283283
func (f *File) setCellString(value string) (t string, v string, ns xml.Attr) {
284-
if len(value) > 32767 {
285-
value = value[0:32767]
284+
if len(value) > TotalCellChars {
285+
value = value[0:TotalCellChars]
286286
}
287287
// Leading and ending space(s) character detection.
288288
if len(value) > 0 && (value[0] == 32 || value[len(value)-1] == 32) {
@@ -311,8 +311,8 @@ func (f *File) setSharedString(val string) int {
311311

312312
// setCellStr provides a function to set string type to cell.
313313
func setCellStr(value string) (t string, v string, ns xml.Attr) {
314-
if len(value) > 32767 {
315-
value = value[0:32767]
314+
if len(value) > TotalCellChars {
315+
value = value[0:TotalCellChars]
316316
}
317317
// Leading and ending space(s) character detection.
318318
if len(value) > 0 && (value[0] == 32 || value[len(value)-1] == 32) {
@@ -476,7 +476,7 @@ func (f *File) SetCellHyperLink(sheet, axis, link, linkType string) error {
476476
xlsx.Hyperlinks = new(xlsxHyperlinks)
477477
}
478478

479-
if len(xlsx.Hyperlinks.Hyperlink) > 65529 {
479+
if len(xlsx.Hyperlinks.Hyperlink) > TotalSheetHyperlinks {
480480
return errors.New("over maximum limit hyperlinks in a worksheet")
481481
}
482482

excelize_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ func TestOpenFile(t *testing.T) {
166166
assert.NoError(t, f.SetCellStr("Sheet2", "c"+strconv.Itoa(i), strconv.Itoa(i)))
167167
}
168168
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOpenFile.xlsx")))
169+
assert.EqualError(t, f.SaveAs(filepath.Join("test", strings.Repeat("c", 199), ".xlsx")), "file name length exceeds maximum limit")
169170
}
170171

171172
func TestSaveFile(t *testing.T) {

file.go

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ package excelize
1212
import (
1313
"archive/zip"
1414
"bytes"
15+
"errors"
1516
"fmt"
1617
"io"
1718
"os"
@@ -62,6 +63,9 @@ func (f *File) Save() error {
6263
// SaveAs provides a function to create or update to an xlsx file at the
6364
// provided path.
6465
func (f *File) SaveAs(name string) error {
66+
if len(name) > FileNameLength {
67+
return errors.New("file name length exceeds maximum limit")
68+
}
6569
file, err := os.OpenFile(name, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
6670
if err != nil {
6771
return err

lib.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ func ColumnNameToNumber(name string) (int, error) {
135135
}
136136
multi *= 26
137137
}
138+
if col > TotalColumns {
139+
return -1, fmt.Errorf("column number exceeds maximum limit")
140+
}
138141
return col, nil
139142
}
140143

@@ -172,7 +175,9 @@ func CellNameToCoordinates(cell string) (int, int, error) {
172175
if err != nil {
173176
return -1, -1, fmt.Errorf(msg, cell, err)
174177
}
175-
178+
if row > TotalRows {
179+
return -1, -1, fmt.Errorf("row number exceeds maximum limit")
180+
}
176181
col, err := ColumnNameToNumber(colname)
177182
return col, row, err
178183
}

lib_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ var validColumns = []struct {
2323
{Name: "AZ", Num: 26 + 26},
2424
{Name: "ZZ", Num: 26 + 26*26},
2525
{Name: "AAA", Num: 26 + 26*26 + 1},
26-
{Name: "ZZZ", Num: 26 + 26*26 + 26*26*26},
2726
}
2827

2928
var invalidColumns = []struct {
@@ -72,6 +71,8 @@ func TestColumnNameToNumber_Error(t *testing.T) {
7271
assert.Equalf(t, col.Num, out, msg, col.Name)
7372
}
7473
}
74+
_, err := ColumnNameToNumber("XFE")
75+
assert.EqualError(t, err, "column number exceeds maximum limit")
7576
}
7677

7778
func TestColumnNumberToName_OK(t *testing.T) {
@@ -172,6 +173,8 @@ func TestCellNameToCoordinates_Error(t *testing.T) {
172173
assert.Equalf(t, -1, r, msg, cell)
173174
}
174175
}
176+
_, _, err := CellNameToCoordinates("A1048577")
177+
assert.EqualError(t, err, "row number exceeds maximum limit")
175178
}
176179

177180
func TestCoordinatesToCellName_OK(t *testing.T) {

xmlDrawing.go

+9
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ const (
8080
ExtURIMacExcelMX = "{64002731-A6B0-56B0-2670-7721B7C09600}"
8181
)
8282

83+
// Excel specifications and limits
84+
const (
85+
FileNameLength = 207
86+
TotalRows = 1048576
87+
TotalColumns = 16384
88+
TotalSheetHyperlinks = 65529
89+
TotalCellChars = 32767
90+
)
91+
8392
var supportImageTypes = map[string]string{".gif": ".gif", ".jpg": ".jpeg", ".jpeg": ".jpeg", ".png": ".png", ".tif": ".tiff", ".tiff": ".tiff"}
8493

8594
// xlsxCNvPr directly maps the cNvPr (Non-Visual Drawing Properties). This

0 commit comments

Comments
 (0)