Skip to content

Commit

Permalink
Fix crashing in an irregular column
Browse files Browse the repository at this point in the history
Add a column check outside the array range.
Added a test with irregular columns.
  • Loading branch information
noborus committed Aug 28, 2024
1 parent 0933eac commit b9e2036
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 8 deletions.
25 changes: 18 additions & 7 deletions oviewer/convert_align.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ func (a *align) convertDelm(st *parseState) bool {
lc := make(contents, 0, len(st.lc))
for c := 0; c < len(indexes); c++ {
e := pos.x(indexes[c][0])
lc = append(lc, st.lc[s:e]...)
width := e - s
if width <= 0 {
break
}
lc = append(lc, st.lc[s:e]...)
s = e

if c >= len(a.maxWidths) {
continue
}
// Add space to align columns.
for ; width < a.maxWidths[c]+1; width++ {
lc = append(lc, SpaceContent)
}
s = e
}
lc = append(lc, st.lc[s:]...)
st.lc = lc
Expand All @@ -91,19 +98,23 @@ func (a *align) convertDelm(st *parseState) bool {
func (a *align) convertWidth(st *parseState) bool {
s := 0
lc := make(contents, 0, len(st.lc))
for i := 0; i < len(a.orgWidths); i++ {
e := findColumnEnd(st.lc, a.orgWidths, i) + 1
for c := 0; c < len(a.orgWidths); c++ {
e := findColumnEnd(st.lc, a.orgWidths, c) + 1
e = min(e, len(st.lc))
width := e - s
if s >= e {
if width <= 0 {
break
}
lc = append(lc, st.lc[s:e]...)
s = e

if c >= len(a.maxWidths) {
continue
}
// Add space to align columns.
for ; width <= a.maxWidths[i]; width++ {
for ; width <= a.maxWidths[c]; width++ {
lc = append(lc, SpaceContent)
}
s = e
}
lc = append(lc, st.lc[s:]...)
st.lc = lc
Expand Down
123 changes: 122 additions & 1 deletion oviewer/convert_align_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func Test_align_convert(t *testing.T) {
want: false,
wantStr: "a,b,",
},

{
name: "convertAlignWidth",
fields: fields{
Expand Down Expand Up @@ -156,3 +155,125 @@ func Test_align_convert(t *testing.T) {
})
}
}

func Test_align_convertDelm(t *testing.T) {
type fields struct {
es *escapeSequence
maxWidths []int
orgWidths []int
WidthF bool
delimiter string
delimiterReg *regexp.Regexp
count int
}
type args struct {
st *parseState
}
tests := []struct {
name string
fields fields
args args
want bool
wantStr string
}{
{
name: "convertAlignDelm",
fields: fields{
es: newESConverter(),
maxWidths: []int{1, 2},
WidthF: false,
delimiter: ",",
count: 0,
},
args: args{
st: &parseState{
lc: StrToContents("a,b,c,d,e,f", 8),
mainc: '\n',
},
},
want: false,
wantStr: "a ,b ,c,d,e,f",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &align{
es: tt.fields.es,
maxWidths: tt.fields.maxWidths,
orgWidths: tt.fields.orgWidths,
WidthF: tt.fields.WidthF,
delimiter: tt.fields.delimiter,
delimiterReg: tt.fields.delimiterReg,
count: tt.fields.count,
}
if got := a.convertDelm(tt.args.st); got != tt.want {
t.Errorf("align.convertDelm() = %v, want %v", got, tt.want)
}
goStr, _ := ContentsToStr(tt.args.st.lc)
if goStr != tt.wantStr {
t.Errorf("align.convertDelm() = %v, want %v", goStr, tt.wantStr)
}
})
}
}

func Test_align_convertWidth(t *testing.T) {
type fields struct {
es *escapeSequence
maxWidths []int
orgWidths []int
WidthF bool
delimiter string
delimiterReg *regexp.Regexp
count int
}
type args struct {
st *parseState
}
tests := []struct {
name string
fields fields
args args
want bool
wantStr string
}{
{
name: "convertAlignWidth",
fields: fields{
es: newESConverter(),
maxWidths: []int{3, 3},
orgWidths: []int{2, 5, 8, 11, 14, 17},
WidthF: true,
count: 0,
},
args: args{
st: &parseState{
lc: StrToContents("a b c d e f", 8),
mainc: '\n',
},
},
want: false,
wantStr: "a b c d e f",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &align{
es: tt.fields.es,
maxWidths: tt.fields.maxWidths,
orgWidths: tt.fields.orgWidths,
WidthF: tt.fields.WidthF,
delimiter: tt.fields.delimiter,
delimiterReg: tt.fields.delimiterReg,
count: tt.fields.count,
}
if got := a.convertWidth(tt.args.st); got != tt.want {
t.Errorf("align.convertWidth() = %v, want %v", got, tt.want)
}
goStr, _ := ContentsToStr(tt.args.st.lc)
if goStr != tt.wantStr {
t.Errorf("align.convertWidth() = %v, want %v", goStr, tt.wantStr)
}
})
}
}

0 comments on commit b9e2036

Please sign in to comment.