mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 08:52:18 +01:00
Bounds check "if" statements.
This commit is contained in:
1 parent
f686adc3c0
commit
84bf437013
4 files changed
+149
-5
No files matched your search
+30
-1
@@ -596,7 +596,36 @@ func (g *gen) writeStatement(n *a.Node, depth uint32) error {
|
|||||||
return nil
|
return nil
|
||||||
|
|
||||||
case a.KIf:
|
case a.KIf:
|
||||||
// TODO, including considering suspendible calls.
|
// TODO: consider suspendible calls.
|
||||||
|
n := n.If()
|
||||||
|
for {
|
||||||
|
g.writes("if (")
|
||||||
|
if err := g.writeExpr(n.Condition(), replaceCallSuspendibles, 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
g.writes(") {\n")
|
||||||
|
for _, o := range n.BodyIfTrue() {
|
||||||
|
if err := g.writeStatement(o, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if bif := n.BodyIfFalse(); len(bif) > 0 {
|
||||||
|
g.writes("} else {")
|
||||||
|
for _, o := range bif {
|
||||||
|
if err := g.writeStatement(o, depth); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n = n.ElseIf()
|
||||||
|
if n == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
g.writes("} else ")
|
||||||
|
}
|
||||||
|
g.writes("}\n")
|
||||||
|
return nil
|
||||||
|
|
||||||
case a.KJump:
|
case a.KJump:
|
||||||
n := n.Jump()
|
n := n.Jump()
|
||||||
|
|||||||
@@ -209,10 +209,12 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
|||||||
|
|
||||||
uint32_t v_bits;
|
uint32_t v_bits;
|
||||||
uint32_t v_n_bits;
|
uint32_t v_n_bits;
|
||||||
|
uint32_t v_clear_code;
|
||||||
uint32_t v_width;
|
uint32_t v_width;
|
||||||
|
|
||||||
v_bits = 0;
|
v_bits = 0;
|
||||||
v_n_bits = 0;
|
v_n_bits = 0;
|
||||||
|
v_clear_code = (((uint32_t)(1)) << self->f_literal_width);
|
||||||
v_width = (self->f_literal_width + 1);
|
v_width = (self->f_literal_width + 1);
|
||||||
while (1) {
|
while (1) {
|
||||||
while ((v_n_bits < v_width)) {
|
while ((v_n_bits < v_width)) {
|
||||||
@@ -226,6 +228,9 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self,
|
|||||||
}
|
}
|
||||||
v_bits >>= v_width;
|
v_bits >>= v_width;
|
||||||
v_n_bits -= v_width;
|
v_n_bits -= v_width;
|
||||||
|
if ((v_bits < v_clear_code)) {
|
||||||
|
goto label_0_break;
|
||||||
|
}
|
||||||
goto label_0_break;
|
goto label_0_break;
|
||||||
}
|
}
|
||||||
label_0_break:;
|
label_0_break:;
|
||||||
|
|||||||
+107
-1
@@ -157,7 +157,7 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
|||||||
return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS())
|
return q.bcheckAssignment(n.LHS(), n.Operator(), n.RHS())
|
||||||
|
|
||||||
case a.KIf:
|
case a.KIf:
|
||||||
// TODO.
|
return q.bcheckIf(n.If())
|
||||||
|
|
||||||
case a.KJump:
|
case a.KJump:
|
||||||
// No-op.
|
// No-op.
|
||||||
@@ -306,6 +306,112 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// terminates returns whether a block of statements terminates. In other words,
|
||||||
|
// whether the block is non-empty and its final statement is a "return",
|
||||||
|
// "break", "continue" or an "if-else" chain where all branches terminate.
|
||||||
|
//
|
||||||
|
// TODO: strengthen this to include "while" statements? For inspiration, the Go
|
||||||
|
// spec has https://golang.org/ref/spec#Terminating_statements
|
||||||
|
func terminates(body []*a.Node) bool {
|
||||||
|
if len(body) > 0 {
|
||||||
|
n := body[len(body)-1]
|
||||||
|
switch n.Kind() {
|
||||||
|
case a.KReturn, a.KJump:
|
||||||
|
return true
|
||||||
|
case a.KIf:
|
||||||
|
n := n.If()
|
||||||
|
for {
|
||||||
|
if !terminates(n.BodyIfTrue()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if bif := n.BodyIfFalse(); len(bif) > 0 && !terminates(bif) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n = n.ElseIf()
|
||||||
|
if n == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func snapshot(facts []*a.Expr) []*a.Expr {
|
||||||
|
return append([]*a.Expr(nil), facts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func unify(dst []*a.Expr, branches [][]*a.Expr) ([]*a.Expr, error) {
|
||||||
|
switch len(branches) {
|
||||||
|
case 0:
|
||||||
|
// No-op.
|
||||||
|
case 1:
|
||||||
|
dst = append(dst, branches[0]...)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("TODO: unify multiple if branches")
|
||||||
|
}
|
||||||
|
return dst, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *checker) bcheckIf(n *a.If) error {
|
||||||
|
branches := [][]*a.Expr(nil)
|
||||||
|
for n != nil {
|
||||||
|
snap := snapshot(q.facts)
|
||||||
|
// Check the if condition.
|
||||||
|
//
|
||||||
|
// TODO: check that n.Condition() has no side effects.
|
||||||
|
if _, _, err := q.bcheckExpr(n.Condition(), 0); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if cv := n.Condition().ConstValue(); cv != nil {
|
||||||
|
return fmt.Errorf("TODO: bcheckIf for \"if true\" or \"if false\"")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the if-true branch, assuming the if condition.
|
||||||
|
q.facts.appendFact(n.Condition())
|
||||||
|
for _, o := range n.BodyIfTrue() {
|
||||||
|
if err := q.bcheckStatement(o); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !terminates(n.BodyIfTrue()) {
|
||||||
|
branches = append(branches, snapshot(q.facts))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the if-false branch, assuming the inverted if condition.
|
||||||
|
q.facts = append(q.facts[:0], snap...)
|
||||||
|
if inverse, err := invert(q.idMap, n.Condition()); err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
q.facts.appendFact(inverse)
|
||||||
|
}
|
||||||
|
if bif := n.BodyIfFalse(); len(bif) > 0 {
|
||||||
|
for _, o := range bif {
|
||||||
|
if err := q.bcheckStatement(o); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !terminates(bif) {
|
||||||
|
branches = append(branches, snapshot(q.facts))
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
n = n.ElseIf()
|
||||||
|
if n == nil {
|
||||||
|
branches = append(branches, snapshot(q.facts))
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
u, err := unify(q.facts[:0], branches)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
q.facts = u
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (q *checker) bcheckWhile(n *a.While) error {
|
func (q *checker) bcheckWhile(n *a.While) error {
|
||||||
// Check the pre and inv conditions on entry.
|
// Check the pre and inv conditions on entry.
|
||||||
for _, o := range n.Asserts() {
|
for _, o := range n.Asserts() {
|
||||||
|
|||||||
@@ -8,9 +8,8 @@ pub struct lzw_decoder?(
|
|||||||
pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
||||||
var bits u32
|
var bits u32
|
||||||
var n_bits u32
|
var n_bits u32
|
||||||
|
var clear_code u32 = (1 as u32) << this.literal_width
|
||||||
// TODO: set clear_code, end_code, etc.
|
// TODO: set end_code.
|
||||||
|
|
||||||
var width u32[..12] = this.literal_width + 1
|
var width u32[..12] = this.literal_width + 1
|
||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
@@ -31,6 +30,11 @@ pub func lzw_decoder.decode?(dst ptr buf1, src ptr buf1, src_final bool)() {
|
|||||||
bits >>= width
|
bits >>= width
|
||||||
n_bits -= width
|
n_bits -= width
|
||||||
|
|
||||||
|
// TODO: s/bits/code/
|
||||||
|
if bits < clear_code {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: implement.
|
// TODO: implement.
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user