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

MessageParser.ParseFrom throws unexpected exceptions (C#) #5513

Open
Metalnem opened this Issue Dec 27, 2018 · 1 comment

Comments

Projects
None yet
3 participants
@Metalnem
Copy link

Metalnem commented Dec 27, 2018

MessageParser.ParseFrom should throw InvalidProtocolBufferException when parsing invalid message, but it can also throw several other unexpected exceptions:

  • ArgumentException
  • ArgumentOutOfRangeException
  • InvalidOperationException

The following C# program demonstrates this:

using System;
using Sample;

namespace Google.Protobuf.Run
{
  public class Program
  {
    public static void Main(string[] args)
    {
      try
      {
        var bytes = new byte[]
        {
          34, 255, 255, 255, 255,
          7, 34, 255, 255, 101
        };

        Person.Parser.ParseFrom(bytes);
      }
      catch (ArgumentException) { }

      try
      {
        var bytes = new byte[]
        {
          10, 247, 181, 144, 151, 110, 32, 68,
          111, 101, 16, 210, 9, 26, 16, 106, 100,
          111, 101, 64, 101, 120, 97, 109, 112,
          108, 101, 46, 99, 111, 109, 34, 12, 10,
          8, 53, 53, 53, 45, 52, 51, 50, 49, 16, 1
        };

        Person.Parser.ParseFrom(bytes);
      }
      catch (ArgumentOutOfRangeException) { }

      try
      {
        var bytes = new byte[]
        {
          14, 8, 74, 111, 104, 110, 32, 68, 111,
          101, 16, 210, 9, 26, 16, 106, 100, 111,
          101, 64, 101, 120, 97, 109, 112, 108,
          101, 46, 99, 111, 109, 34, 12, 10, 8, 53,
          53, 53, 45, 52, 51, 50, 59, 16, 1
        };

        Person.Parser.ParseFrom(bytes);
      }
      catch (InvalidOperationException) { }
    }
  }
}

Proto definition of the Person class:

syntax = "proto3";
package sample;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    string number = 1;
    PhoneType type = 2;
  }

  repeated PhoneNumber phones = 4;
}

Generated C# code for the Person class is in the attached archive.
I'm using .NET Core 2.1 and the latest NuGet version of the Google.Protobuf package (version 3.6.1).

Found via SharpFuzz.

@anandolee

This comment has been minimized.

Copy link
Contributor

anandolee commented Jan 10, 2019

Thanks to raise it up. But we do not have enough engineer to fix C# issue. Are you able to create a fixing PR and assign to me?

ObsidianMinor added a commit to ObsidianMinor/protobuf that referenced this issue Jan 16, 2019

ObsidianMinor added a commit to ObsidianMinor/protobuf that referenced this issue Jan 16, 2019

@ObsidianMinor ObsidianMinor referenced a pull request that will close this issue Jan 16, 2019

Open

Fix #5513 #5588

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment