Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/ast: Fprint misbehaves with user-provided data. #3898

Closed
remyoudompheng opened this issue Aug 2, 2012 · 2 comments
Closed

go/ast: Fprint misbehaves with user-provided data. #3898

remyoudompheng opened this issue Aug 2, 2012 · 2 comments

Comments

@remyoudompheng
Copy link
Contributor

What steps will reproduce the problem?
1. A *ast.Object may contain arbitrary data. ast.Fprint assumes it can call Interface()
on arbitrary reflect.Value's The exp/types package injects such data.

2. Call the following file main.go and go run it (you need exp/types)

package main

import (
      "exp/types"
      "go/ast"
      "go/parser"
      "go/token"
      "os"
)

func main() {
      fs := token.NewFileSet()
      file, err := parser.ParseFile(fs, "main.go", nil, 0)
      if err != nil {
            panic(err)
      }

      astpkg, err := ast.NewPackage(fs, map[string]*ast.File{"main.go": file}, types.GcImport, types.Universe)
      if err != nil {
            panic(err)
      }

      _, err = types.Check(fs, astpkg)
      if err != nil {
            panic(err)
      }

      ast.Fprint(os.Stdout, fs, astpkg, nil)
}

What is the expected output? What do you see instead?

Expected: bunches of AST/struct dumps.

Got: an error on a *big.Int:

   501  .  .  .  .  .  .  .  .  .  .  .  .  .  Data: *ast.Scope {
   502  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Outer: nil
   503  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Objects: map[string]*ast.Object (len = 109) {
   504  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  "LEQ": *ast.Object {
   505  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Kind: const
   506  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Name: "LEQ"
   507  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Decl: nil
   508  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  Data: types.Const {
   509  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  val: *panic: reflect.Value.Interface: cannot return value obtained from unexported field or method [recovered]
    panic: interface conversion: interface is string, not ast.localError

goroutine 1 [running]:
go/ast.func·003(0x7f43ab6fff38, 0x7f43ab5e6100)
    /opt/remy/go/src/pkg/go/ast/print.go:52 +0x67
----- stack segment boundary -----
reflect.valueInterface(0x58a4e8, 0xf840118380, 0x161, 0x1, 0x161, ...)
    /opt/remy/go/src/pkg/reflect/value.go:831 +0xe8
reflect.Value.Interface(0x58a4e8, 0xf840118380, 0x161, 0x0, 0x0, ...)
    /opt/remy/go/src/pkg/reflect/value.go:816 +0x42
go/ast.(*printer).print(0xf84011a600, 0x58a4e8, 0xf840118380, 0x161)
    /opt/remy/go/src/pkg/go/ast/print.go:164 +0x7e8
go/ast.(*printer).print(0xf84011a600, 0x4f05e8, 0xf8400d5f50, 0x143)
    /opt/remy/go/src/pkg/go/ast/print.go:145 +0xeb


Please use labels and text to provide additional information.
@robpike
Copy link
Contributor

robpike commented Aug 2, 2012

Comment 1:

Labels changed: added priority-soon, removed priority-triage.

Owner changed to @griesemer.

Status changed to Accepted.

@griesemer
Copy link
Contributor

Comment 2:

This issue was closed by revision 593c51c.

Status changed to Fixed.

griesemer added a commit that referenced this issue May 11, 2015
… fields

««« backport d134e30c4d29
go/ast: ast.Print must not crash with unexported fields

Don't print unexported struct fields; their values are
not accessible via reflection.

Fixes #3898.

Also:
- added support for arrays
- print empty maps, arrays, slices, structs on one line
  for a denser output
- added respective test cases

R=r
CC=golang-dev
https://golang.org/cl/6454089

»»»
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants