Giter VIP home page Giter VIP logo

Comments (8)

jhump avatar jhump commented on July 27, 2024 1

@mprimeaux, I'm working on it. Just checking for a nil AST isn't quite enough. A better fix will handle a lack of AST by using the descriptor in the parse result to recursively crawl through imports.

I also realized that the InferImportPaths never had a proper test, which is why this bug has been lurking for so long (certainly broken in v1.15.0). In adding a test, I see a few other issues (unrelated to the panic you found), and am fixing those, too. So the next release will fix the panic as well as improve the behavior of the InferImportPaths flag.

from protoreflect.

mprimeaux avatar mprimeaux commented on July 27, 2024

As an update, I pulled down the protoreflect code and wrote a new test using the same failing .proto file and it parsed it just fine; no SIGSEGV panic.

After further debugging, I was able to trip the exact panic by setting InferImportPaths to true on the protoparse.Parser. I'll update this issue once I've identified the cause.

Also, I updated the related grpcurl issue since the currently workaround is to specify the -import-path option.

from protoreflect.

mprimeaux avatar mprimeaux commented on July 27, 2024

I believe the fix is to the function parseToProtosRecursive in parser.go here by adding a nil check for astRoot.

func parseToProtoRecursive(res protocompile.Resolver, filename string, rep *reporter.Handler, srcPosAddr *SourcePos, results map[string]parser.Result) {
	if _, ok := results[filename]; ok {
		// already processed this one
		return
	}
	results[filename] = nil // placeholder entry

	astRoot, parseResult, _ := parseToAST(res, filename, rep)
	if rep.ReporterError() != nil {
		return
	}
	if parseResult == nil {
		parseResult, _ = parser.ResultFromAST(astRoot, true, rep)
		if rep.ReporterError() != nil {
			return
		}
	}
	results[filename] = parseResult

+	if astRoot == nil {
+		return
+	}

	for _, decl := range astRoot.Decls {
		imp, ok := decl.(*ast2.ImportNode)
		if !ok {
			continue
		}
		func() {
			orig := *srcPosAddr
			*srcPosAddr = astRoot.NodeInfo(imp.Name).Start()
			defer func() {
				*srcPosAddr = orig
			}()

			parseToProtoRecursive(res, imp.Name.AsString(), rep, srcPosAddr, results)
		}()
		if rep.ReporterError() != nil {
			return
		}
	}
}

from protoreflect.

mprimeaux avatar mprimeaux commented on July 27, 2024

Looks like the panic is triggered when an import line is encountered in the .proto file. For example,

syntax = "proto3";
import "google/protobuf/struct.proto";

To reproduce this, I modified the existing test named TestBuilder_PreserveAllCommentsAfterBuild in builder_test.go that was passing before with the above import and now it fails with the exact panic.

The decl.(*ast2.ImportNode) cast here assumes the import is for a local file and doesn't seem to handle the Google protobuf types.

After I add the nil check as in my previous comment the failing test passes.

I'll leave this here until @jhump or another maintainer has an opportunity to review. I still think the nil check above might be a viable option.

from protoreflect.

srebhan avatar srebhan commented on July 27, 2024

@mprimeaux are you willing to submit a PR for the issue as you already do have the code ready!? We are hitting the same issue in Telegraf...

from protoreflect.

mprimeaux avatar mprimeaux commented on July 27, 2024

@srebhan Sure. I'll submit a PR shortly.

from protoreflect.

mprimeaux avatar mprimeaux commented on July 27, 2024

@jhump Sounds great and thanks for your help. Shall I just cancel my PR since you're got a handle on it?

from protoreflect.

jhump avatar jhump commented on July 27, 2024

Fixed in #575.

from protoreflect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.