Fix single byte overflow case for sized render

This commit is contained in:
lash 2023-04-14 09:09:53 +01:00
parent 59dcb7875d
commit 4b6cc33208
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 33 additions and 17 deletions

View File

@ -3,7 +3,6 @@ package engine
import ( import (
"context" "context"
"io" "io"
"log"
"git.defalsify.org/festive/persist" "git.defalsify.org/festive/persist"
"git.defalsify.org/festive/resource" "git.defalsify.org/festive/resource"
@ -30,17 +29,8 @@ func RunPersisted(cfg Config, rs resource.Resource, pr persist.Persister, input
cfg.Root = location cfg.Root = location
} }
log.Printf("run persisted with state %v %x input %s", st, st.Code, input)
en := NewEngine(cfg, pr.GetState(), rs, pr.GetMemory(), ctx) en := NewEngine(cfg, pr.GetState(), rs, pr.GetMemory(), ctx)
log.Printf("location %s", location)
// if len(input) == 0 {
// log.Printf("init")
// err = en.Init(location, ctx)
// if err != nil {
// return err
// }
c, err := en.WriteResult(w, ctx) c, err := en.WriteResult(w, ctx)
if err != nil { if err != nil {
return err return err
@ -49,7 +39,6 @@ func RunPersisted(cfg Config, rs resource.Resource, pr persist.Persister, input
if err != nil { if err != nil {
return err return err
} }
log.Printf("engine init write %v flags %v", c, st.Flags)
if c > 0 { if c > 0 {
return err return err
} }

View File

@ -31,7 +31,6 @@ func TestPersist(t *testing.T) {
ca := cache.NewCache().WithCacheSize(1024) ca := cache.NewCache().WithCacheSize(1024)
pr := persist.NewFsPersister(persistDir).WithContent(&st, ca) pr := persist.NewFsPersister(persistDir).WithContent(&st, ca)
//w := bytes.NewBuffer(nil)
w := os.Stdout w := os.Stdout
ctx := context.TODO() ctx := context.TODO()
@ -46,7 +45,7 @@ func TestPersist(t *testing.T) {
pr = persist.NewFsPersister(persistDir) pr = persist.NewFsPersister(persistDir)
inputs := []string{ inputs := []string{
"", "", // trigger init, will not exec
"1", "1",
"2", "2",
"00", "00",

View File

@ -251,16 +251,15 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
tb := strings.Builder{} tb := strings.Builder{}
rb := strings.Builder{} rb := strings.Builder{}
netRemaining := remaining netRemaining := remaining - 1
if len(sinkValues) > 1 { if len(sinkValues) > 1 {
log.Printf("menusizes %v", menuSizes)
netRemaining -= menuSizes[1] - 1 netRemaining -= menuSizes[1] - 1
} }
for i, v := range sinkValues { for i, v := range sinkValues {
log.Printf("processing sinkvalue %v: %s", i, v)
l += len(v) l += len(v)
if uint32(l) > netRemaining { log.Printf("processing sinkvalue %v: %s", i, v)
if uint32(l) > netRemaining - 1 {
if tb.Len() == 0 { if tb.Len() == 0 {
return nil, fmt.Errorf("capacity insufficient for sink field %v", i) return nil, fmt.Errorf("capacity insufficient for sink field %v", i)
} }

View File

@ -47,6 +47,7 @@ func(szr *Sizer) Check(s string) (uint32, bool) {
if szr.outputSize > 0 { if szr.outputSize > 0 {
if l > szr.outputSize { if l > szr.outputSize {
log.Printf("sizer check fails with length %v: %s", l, szr) log.Printf("sizer check fails with length %v: %s", l, szr)
log.Printf("sizer contents:\n%s", s)
return 0, false return 0, false
} }
l = szr.outputSize - l l = szr.outputSize - l

View File

@ -192,3 +192,31 @@ eleven twelve
} }
} }
func TestManySizes(t *testing.T) {
for i := 50; i < 128; i++ {
st := state.NewState(0)
ca := cache.NewCache()
mn := NewMenu().WithOutputSize(32)
mrs := resource.NewMenuResource().WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
rs := TestSizeResource{
mrs,
}
szr := NewSizer(uint32(i))
pg := NewPage(ca, rs).WithSizer(szr).WithMenu(mn)
ca.Push()
st.Down("pages")
ca.Add("foo", "inky", 10)
ca.Add("bar", "pinky", 10)
ca.Add("baz", "blinky", 10)
ca.Add("xyzzy", "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", 0)
pg.Map("foo")
pg.Map("bar")
pg.Map("baz")
pg.Map("xyzzy")
_, err := pg.Render("pages", 0)
if err != nil {
t.Fatal(err)
}
}
}