square / square/wire

StackoverflowError when model have circular reference

Open
#776 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
4.4k
Forks
627
Avg merge
3d 15m
Merged PRs (30d)
20

Description

pb

//CardModelEntryPB.proto
import "ChildModelEntryPB.proto";
package com_foo_alert;

option java_package = "com.foo.alert";

message CardModelEntryPB {
  optional string cardTypeId = 1;
  optional string cellId = 2;
  optional ChildModelEntryPB childModel = 3;
}
//ChildModelEntryPB.proto
import "CardModelEntryPB.proto";
package com_foo_alert;

option java_package = "com.foo.alert";

message ChildModelEntryPB {
  repeated string cells = 1;
  repeated CardModelEntryPB children = 2;
  optional int64 serverTimestamp = 3;
}
// protos.include
ChildModelEntryPB.proto
CardModelEntryPB.proto

cmd

java -jar wire-compiler/target/wire-compiler-1.8.0-jar-with-dependencies.jar \
--proto_path=pb \
--java_out=out \
--files=protos.include

Error

Exception in thread "main" java.lang.StackOverflowError
	at java.lang.StringCoding$StringEncoder.encode(StringCoding.java:304)
	at java.lang.StringCoding.encode(StringCoding.java:344)
	at java.lang.String.getBytes(String.java:918)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileInputStream.<init>(FileInputStream.java:93)
	at com.squareup.wire.IO$FileIO.parse(IO.java:41)
	at com.squareup.wire.WireCompiler.loadSymbolsHelper(WireCompiler.java:621)
	at com.squareup.wire.WireCompiler.loadSymbolsHelper(WireCompiler.java:622)
        .......
	at com.squareup.wire.WireCompiler.loadSymbolsHelper(WireCompiler.java:622)

Ref code

  // WireCompiler.java
  // Call with pass == LOAD_TYPES, then pass == LOAD_FIELDS
  private void loadSymbolsHelper(ProtoFile protoFile, Set<String> loadedDependencies,
      LoadSymbolsPass pass) throws IOException {
    // Load symbols from imports
    for (String dependency : protoFile.dependencies()) {
      if (!loadedDependencies.contains(dependency)) {
        String dep = repoPath + File.separator + dependency;
        ProtoFile dependencyFile = io.parse(dep);
        loadSymbolsHelper(dependencyFile, loadedDependencies, pass); // line 622
        loadedDependencies.add(dependency); // line 623
      }
    }

Should we switch the order of #622 and #623

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in wire-compiler/src/main/java/com/squareup/wire/WireCompiler.java at loadSymbolsHelper, especially lines 622-623, and reproduce the provided circular-import command with the two .proto files. Verify that circular dependencies no longer cause a StackOverflowError and that normal symbol loading still completes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.