This closes #1432, fix panic formattedValue when style is negative (#1433)

This commit is contained in:
Liron Levin 2022-12-28 18:37:37 +02:00 committed by GitHub
parent 0c76766c2b
commit a57203a03a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 3 deletions

View File

@ -1319,7 +1319,7 @@ func (f *File) formattedValue(s int, v string, raw bool) (string, error) {
if styleSheet.CellXfs == nil { if styleSheet.CellXfs == nil {
return v, err return v, err
} }
if s >= len(styleSheet.CellXfs.Xf) { if s >= len(styleSheet.CellXfs.Xf) || s < 0 {
return v, err return v, err
} }
var numFmtID int var numFmtID int

View File

@ -2,6 +2,7 @@ package excelize
import ( import (
"fmt" "fmt"
_ "image/jpeg"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -11,8 +12,6 @@ import (
"testing" "testing"
"time" "time"
_ "image/jpeg"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -755,10 +754,16 @@ func TestFormattedValue(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "43528", result) assert.Equal(t, "43528", result)
// S is too large
result, err = f.formattedValue(15, "43528", false) result, err = f.formattedValue(15, "43528", false)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "43528", result) assert.Equal(t, "43528", result)
// S is too small
result, err = f.formattedValue(-15, "43528", false)
assert.NoError(t, err)
assert.Equal(t, "43528", result)
result, err = f.formattedValue(1, "43528", false) result, err = f.formattedValue(1, "43528", false)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, "43528", result) assert.Equal(t, "43528", result)