Skip to content
Myr

Handle int32 overflow

Before adoption: this commit came in with the history the project adopted, so no landing records it.

yes | fzf --tail=10 --preview 'echo "{n}"'

authored by Junegunn Choi · committed by Junegunn Choi · parent 93bbb30 · Browse files · 2 files+27 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−8 removed

  • other:+27 added−8 removed

2 files+27 added−8 removed

modified:src/item.go+3 added-1 removed
1package fzf23import (4 "math"56 "github.com/junegunn/fzf/src/util"7)89// Item represents each input line. 56 bytes.
8 unmodified lines
18func (item *Item) Index() int32 {19 return item.text.Index20}2120var minItem = Item{text: util.Chars{Index: -1}}22var minItem = Item{text: util.Chars{Index: math.MinInt32}}2324func (item *Item) TrimLength() uint16 {25 return item.text.TrimLength()26}
20 unmodified lines
modified:src/terminal.go+24 added-7 removed
1174 unmodified lines
1175 }1176 t.progress = 1001177 t.merger = merger1178 if !t.revision.equals(newRevision) {1179 if !t.revision.compatible(newRevision) { // Reloaded: clear selection1179 if !t.revision.compatible(newRevision) {1180 // Reloaded: clear selection1181 t.selected = make(map[int32]selectedItem)1182 } else {1181 } else { // Trimmed by --tail: filter selection by index1183 // Trimmed by --tail: filter selection by index1184 filtered := make(map[int32]selectedItem)1185 minIndex := merger.minIndex1186 maxIndex := minIndex + int32(merger.Length())1187 for k, v := range t.selected {1188 var included bool1184 if k >= merger.minIndex {1189 if maxIndex > minIndex {1190 included = k >= minIndex && k < maxIndex1191 } else { // int32 overflow [==> <==]1192 included = k >= minIndex || k < maxIndex1193 }1194 if included {1195 filtered[k] = v1196 }1197 }1198 t.selected = filtered
1666 unmodified lines
2865 case match == "{}":2866 replace = func(item *Item) string {2867 switch {2868 case flags.number:2859 n := int(item.text.Index)2860 if n < 0 {2861 return ""2869 n := item.text.Index2870 if n == minItem.Index() {2871 // NOTE: Item index should normally be positive, but if there's no2872 // match, it will be set to math.MinInt32, and we don't want to2873 // show that value. However, int32 can overflow, especially when2874 // `--tail` is used with an endless input stream, and the index of2875 // an item actually can be math.MinInt32. In that case, you're2876 // getting an incorrect value, but we're going to ignore that for2877 // now.2878 return "''"2879 }2863 return strconv.Itoa(n)2880 return strconv.Itoa(int(n))2881 case flags.file:2882 return item.AsString(params.stripAnsi)2883 default:2884 return params.executor.QuoteEntry(item.AsString(params.stripAnsi))
1858 unmodified lines