SonarSource/sonarqube · error · IllegalStateException

Can not parse message

Error message

Can not parse message

What it means

Protobuf2.parseFrom wraps InvalidProtocolBufferException into an IllegalStateException with message "Can not parse message". It means the bytes in the input stream are not a valid protobuf message for the expected parser/type.

Source

Thrown at server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java:59

      throw new IllegalStateException("Can not write message " + msg, e);
    }
    return this;
  }

  public Protobuf2 writeDelimitedTo(Message msg, OutputStream output) {
    try {
      msg.writeDelimitedTo(output);
    } catch (IOException e) {
      throw new IllegalStateException("Can not write message " + msg, e);
    }
    return this;
  }

  public <M extends Message> M parseFrom(Parser<M> parser, InputStream input) {
    try {
      return parser.parseFrom(input);
    } catch (InvalidProtocolBufferException e) {
      throw new IllegalStateException("Can not parse message", e);
    }
  }

  @CheckForNull
  public <M extends Message> M parseDelimitedFrom(Parser<M> parser, InputStream input) {
    try {
      return parser.parseDelimitedFrom(input);
    } catch (InvalidProtocolBufferException e) {
      throw new IllegalStateException("Can not parse message", e);
    }
  }
}

View on GitHub (pinned to 184c821202)

Solutions

  1. Verify the data was written by a compatible protobuf schema version
  2. Check the stream is not truncated or empty before parsing
  3. Regenerate or re-fetch the corrupted data
  4. Confirm the parser matches the message type that was serialized

Example fix

// before
MyMessage msg = protobuf2.parseFrom(MyMessage.parser(), input);
// after
if (input.available() == 0) {
  throw new IllegalStateException("Empty input, nothing to parse");
}
MyMessage msg = protobuf2.parseFrom(MyMessage.parser(), input);
Defensive patterns

Strategy: try-catch

Validate before calling

byte[] data = input.readAllBytes();
if (data.length == 0) {
  throw new IllegalArgumentException("No data to parse");
}
input = new ByteArrayInputStream(data);

Try / catch

try {
  MyMessage msg = protobuf2.parseFrom(MyMessage.parser(), input);
} catch (IllegalStateException e) {
  LOGGER.warn("Corrupt or incompatible protobuf data", e.getCause());
  // discard record or fail with clear user-facing message
}

Prevention

When it happens

Trigger: Calling parseFrom(parser, input) when the stream contains truncated, corrupted, empty, or wrong-type protobuf bytes.

Common situations: Version mismatch between writer and reader of the data (schema changed); corrupted files on disk; reading a non-protobuf file; truncated transfer.

Understand the failure class

Background: "cannot parse invalid wire-format data", "cannot unmarshal", "failed unmarshalling": protobuf unmarshal errors explained — this error's family across 10 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/894318e4c6815108. Report an issue: GitHub.