From e2e70a8a421fb52d0f17a54d6476df0b8a7d2c83 Mon Sep 17 00:00:00 2001 From: Sydney Acksman Date: Tue, 19 Nov 2019 17:50:46 -0600 Subject: [PATCH 1/3] Fix conformance test failures for Google.Protobuf --- conformance/failure_list_csharp.txt | 31 ------------------- .../src/Google.Protobuf/CodedInputStream.cs | 27 +++++++++++++++- 2 files changed, 26 insertions(+), 32 deletions(-) diff --git a/conformance/failure_list_csharp.txt b/conformance/failure_list_csharp.txt index 2adb30f576da..2a20aa78e7ff 100644 --- a/conformance/failure_list_csharp.txt +++ b/conformance/failure_list_csharp.txt @@ -1,33 +1,2 @@ -Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.PackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.PackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataScalarBinary.BOOL[4].ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataScalarBinary.BOOL[6].ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.PackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.DefaultOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.PackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.UnpackedOutput.ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataScalarBinary.BOOL[4].ProtobufOutput -Recommended.Proto3.ProtobufInput.ValidDataScalarBinary.BOOL[6].ProtobufOutput -Required.Proto2.ProtobufInput.RepeatedScalarSelectsLast.BOOL.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataScalar.BOOL[4].ProtobufOutput -Required.Proto2.ProtobufInput.ValidDataScalar.BOOL[6].ProtobufOutput -Required.Proto3.ProtobufInput.RepeatedScalarSelectsLast.BOOL.JsonOutput -Required.Proto3.ProtobufInput.RepeatedScalarSelectsLast.BOOL.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.JsonOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.JsonOutput -Required.Proto3.ProtobufInput.ValidDataRepeated.BOOL.UnpackedInput.ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataScalar.BOOL[4].JsonOutput -Required.Proto3.ProtobufInput.ValidDataScalar.BOOL[4].ProtobufOutput -Required.Proto3.ProtobufInput.ValidDataScalar.BOOL[6].JsonOutput -Required.Proto3.ProtobufInput.ValidDataScalar.BOOL[6].ProtobufOutput -Recommended.Proto2.ProtobufInput.ValidDataRepeated.BOOL.PackedInput.DefaultOutput.ProtobufOutput Recommended.Proto3.JsonInput.BytesFieldBase64Url.JsonOutput Recommended.Proto3.JsonInput.BytesFieldBase64Url.ProtobufOutput diff --git a/csharp/src/Google.Protobuf/CodedInputStream.cs b/csharp/src/Google.Protobuf/CodedInputStream.cs index 44934f341b25..e405b5b28abb 100644 --- a/csharp/src/Google.Protobuf/CodedInputStream.cs +++ b/csharp/src/Google.Protobuf/CodedInputStream.cs @@ -577,7 +577,32 @@ public uint ReadFixed32() /// public bool ReadBool() { - return ReadRawVarint32() != 0; + int result = 0; // can't do AND on bytes, it gets implicitly upcasted to int + byte index = 0; + if (bufferPos < bufferSize) // check if we have at least one byte for the most likely case of a one byte 1 or 0 + { + result = buffer[bufferPos++]; + if (result < 128) + { + return result != 0; + } + result &= 0x7f; + index++; + } + + do + { + byte b = ReadRawByte(); + result |= b & 0x7F; // OR all bytes together since we don't care about the actual value, just whether is more than zero + if (b < 0x80) + { + return result != 0; + } + index++; + } + while (index < 10); + + throw InvalidProtocolBufferException.MalformedVarint(); } /// From 71ac3e5c0e8e3a706d97277f381471a3a32a4a01 Mon Sep 17 00:00:00 2001 From: Sydney Acksman Date: Tue, 19 Nov 2019 17:50:53 -0600 Subject: [PATCH 2/3] Fix exception message on unsupported request output format --- csharp/src/Google.Protobuf.Conformance/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csharp/src/Google.Protobuf.Conformance/Program.cs b/csharp/src/Google.Protobuf.Conformance/Program.cs index 728a3b968238..d1093abee1f3 100644 --- a/csharp/src/Google.Protobuf.Conformance/Program.cs +++ b/csharp/src/Google.Protobuf.Conformance/Program.cs @@ -143,7 +143,7 @@ private static ConformanceResponse PerformRequest(ConformanceRequest request, Ty case global::Conformance.WireFormat.Protobuf: return new ConformanceResponse { ProtobufPayload = message.ToByteString() }; default: - throw new Exception("Unsupported request output format: " + request.PayloadCase); + throw new Exception("Unsupported request output format: " + request.RequestedOutputFormat); } } catch (InvalidOperationException e) From 77d33734cf69cb431c7f82f61141f17765d45f7c Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Wed, 20 Nov 2019 12:48:49 +0100 Subject: [PATCH 3/3] simpler fix --- .../src/Google.Protobuf/CodedInputStream.cs | 31 ++----------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/csharp/src/Google.Protobuf/CodedInputStream.cs b/csharp/src/Google.Protobuf/CodedInputStream.cs index e405b5b28abb..bea6bff34f2b 100644 --- a/csharp/src/Google.Protobuf/CodedInputStream.cs +++ b/csharp/src/Google.Protobuf/CodedInputStream.cs @@ -577,32 +577,7 @@ public uint ReadFixed32() /// public bool ReadBool() { - int result = 0; // can't do AND on bytes, it gets implicitly upcasted to int - byte index = 0; - if (bufferPos < bufferSize) // check if we have at least one byte for the most likely case of a one byte 1 or 0 - { - result = buffer[bufferPos++]; - if (result < 128) - { - return result != 0; - } - result &= 0x7f; - index++; - } - - do - { - byte b = ReadRawByte(); - result |= b & 0x7F; // OR all bytes together since we don't care about the actual value, just whether is more than zero - if (b < 0x80) - { - return result != 0; - } - index++; - } - while (index < 10); - - throw InvalidProtocolBufferException.MalformedVarint(); + return ReadRawVarint64() != 0; } /// @@ -870,7 +845,7 @@ public bool MaybeConsumeTag(uint tag) internal static bool? ReadBoolWrapper(CodedInputStream input) { - return ReadUInt32Wrapper(input) != 0; + return ReadUInt64Wrapper(input) != 0; } internal static uint? ReadUInt32Wrapper(CodedInputStream input) @@ -1679,4 +1654,4 @@ private void SkipImpl(int amountToSkip) } #endregion } -} \ No newline at end of file +}