]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: fix a couple of hidden crashes that become
authorRobert Griesemer <gri@golang.org>
Fri, 12 Mar 2010 22:01:52 +0000 (14:01 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 12 Mar 2010 22:01:52 +0000 (14:01 -0800)
visible only when enabling internal debug mode:
- in rare cases expression depth can underflow
- when printing a single labeled statement, indentation
  may underflow if not setup correctly

R=rsc
CC=golang-dev
https://golang.org/cl/484041

src/pkg/go/printer/nodes.go
src/pkg/go/printer/printer.go
src/pkg/go/printer/testdata/expressions.golden
src/pkg/go/printer/testdata/expressions.input
src/pkg/go/printer/testdata/expressions.raw

index d4f6d9d0e7defa6032d47a97f9922b2dbb5703f4..9e2a8c8568723862f005571fb826acdb4eb72faf 100644 (file)
@@ -562,6 +562,15 @@ func diffPrec(expr ast.Expr, prec int) int {
 }
 
 
+func reduceDepth(depth int) int {
+       depth--
+       if depth < 1 {
+               depth = 1
+       }
+       return depth
+}
+
+
 // Format the binary expression: decide the cutoff and then format.
 // Let's call depth == 1 Normal mode, and depth > 1 Compact mode.
 // (Algorithm suggestion by Russ Cox.)
@@ -604,7 +613,7 @@ func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int, multiL
                // Note: The parser inserts an ast.ParenExpr node; thus this case
                //       can only occur if the AST is created in a different way.
                p.print(token.LPAREN)
-               p.expr0(x, depth-1, multiLine) // parentheses undo one level of depth
+               p.expr0(x, reduceDepth(depth), multiLine) // parentheses undo one level of depth
                p.print(token.RPAREN)
                return
        }
@@ -707,7 +716,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
 
        case *ast.ParenExpr:
                p.print(token.LPAREN)
-               p.expr0(x.X, depth-1, multiLine) // parentheses undo one level of depth
+               p.expr0(x.X, reduceDepth(depth), multiLine) // parentheses undo one level of depth
                p.print(x.Rparen, token.RPAREN)
 
        case *ast.SelectorExpr:
@@ -925,7 +934,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) {
 
        case *ast.LabeledStmt:
                // a "correcting" unindent immediately following a line break
-               // is applied before the line break  if there is no comment
+               // is applied before the line break if there is no comment
                // between (see writeWhitespace)
                p.print(unindent)
                p.expr(s.Label, multiLine)
index f35663eb88498b7d9b5ab1c55db0dc3c6b95444b..0d5760ff56c04c4dcb38efcedd8dca608b9b26e1 100644 (file)
@@ -1002,6 +1002,11 @@ func (cfg *Config) Fprint(output io.Writer, node interface{}) (int, os.Error) {
                        p.expr(n, ignoreMultiLine)
                case ast.Stmt:
                        p.useNodeComments = true
+                       // A labeled statement will un-indent to position the
+                       // label. Set indent to 1 so we don't get indent "underflow".
+                       if _, labeledStmt := n.(*ast.LabeledStmt); labeledStmt {
+                               p.indent = 1
+                       }
                        p.stmt(n, ignoreMultiLine)
                case ast.Decl:
                        p.useNodeComments = true
index 6626c546b7a7ac61f6c7f5dcd6155f80a8498b1b..c35efb83032004c35a496929be2e9d15771cafb4 100644 (file)
@@ -20,6 +20,7 @@ var (
 
 func _() {
        // no spaces around simple or parenthesized expressions
+       _ = (a + 0)
        _ = a + b
        _ = a + b + c
        _ = a + b - c
index 0b67a763ef909784d0b64c506590e6237ae1abb5..b9fc976a9b9a5969d9800f310a7fd5704dad1a04 100644 (file)
@@ -20,6 +20,7 @@ var (
 
 func _() {
        // no spaces around simple or parenthesized expressions
+       _ = (a+0)
        _ = a+b
        _ = a+b+c
        _ = a+b-c
index 406fbf695a64bc000278c31e8eb1ccc50df61f3b..3f3b460bc2d0068d313e5e610f1863ea96be4c70 100644 (file)
@@ -20,6 +20,7 @@ var (
 
 func _() {
        // no spaces around simple or parenthesized expressions
+       _ = (a + 0)
        _ = a + b
        _ = a + b + c
        _ = a + b - c