proto icon indicating copy to clipboard operation
proto copied to clipboard

embedded comment parse fail

Open Pramy opened this issue 3 years ago • 1 comments

test.proto

syntax = "proto3";
package test;
import "google/protobuf/descriptor.proto";

message FooOption {
  optional bool from = 1;
  optional bool to = 2;
}
extend google.protobuf.FieldOptions {

  optional FooOption foo_option = 1;
}

message Foo {

  message  /* ddd

  */
      InnerFoo {
    optional  // aaa
        uint64 /* bbb
     */value = 1 // ccc
    [(foo_option) // 123
        = {from: true, // kkkkk
      to: true}
      // 123123
    ];
  }
}

main.go

package main

import (
	"fmt"
	"github.com/emicklei/proto"
	"os"
)

func main() {
	reader, _ := os.Open("test.proto")
	defer reader.Close()

	parser := proto.NewParser(reader)
	definition, err := parser.Parse()
	if err != nil {
		fmt.Println(err)
	}

	proto.Walk(definition,
		proto.WithService(handleService),
		proto.WithMessage(handleMessage))
}

func handleService(s *proto.Service) {
	fmt.Println(s.Name)
}

func handleMessage(m *proto.Message) {
	fmt.Println(m.Name)
}

output

<input>:16:12: found "/* ddd\n\n  */" but expected [message identifier]
FooOption
google.protobuf.FieldOptions

Pramy avatar Apr 14 '22 17:04 Pramy

thank for reporting this issue. It got from my attention radar. Will have a look at it soon

ernest-ag5 avatar Jun 30 '22 12:06 ernest-ag5

i am closing this as the issue addresses a very uncommon case. Handling this would complicate the package even more.

emicklei avatar Apr 22 '23 08:04 emicklei

@emicklei

I ran into this as well, and I am sad to see this resolution. This parser should handle proto files as intended, not disregarding edge cases as being unlikely.

zack-littke-smith-ai avatar Jan 10 '24 17:01 zack-littke-smith-ai

@emicklei

I ran into this as well, and I am sad to see this resolution. This parser should handle proto files as intended, not disregarding edge cases as being unlikely.

At the time of reporting this issue, I found that it was a usecase that very synthesized, almost with the purpose of finding a problem with the parser (like fuzzing). Next it wasn't given any attention for over a year, so I did not felt it was urgent enough to spent much time on it.

So, maybe if you @zack-littke-smith can provide me with an example to replicates your problem, we could work on a solution for that first.

emicklei avatar Jan 10 '24 21:01 emicklei

I discovered this via usage of this parser in a downstream library - it is not yet clear to me if the fault is with this parser or that library. Copying my example from the issue I filed with them:

message Example {
  optional /* i'm a comment */ bool field = 1;
}

Seems to not be parsed properly, and seems like a reasonable place for a comment. Though again, I am not sure if this parsing library is the one at fault here.

zack-littke-smith-ai avatar Jan 10 '24 21:01 zack-littke-smith-ai

see https://github.com/emicklei/proto/pull/142 that parses this:

message Example {
		/* i'm */ optional /* a comment */ bool /* too */ field /* hard */ = /* to */ 1 /* read */;
}

emicklei avatar Jan 11 '24 21:01 emicklei

Closed by https://github.com/emicklei/proto/pull/142

zack-littke-smith-ai avatar Jan 30 '24 23:01 zack-littke-smith-ai