public void MaliciousRecursion() { ByteString data64 = MakeRecursiveMessage(64).ToByteString(); ByteString data65 = MakeRecursiveMessage(65).ToByteString(); AssertMessageDepth(TestRecursiveMessage.ParseFrom(data64), 64); try { TestRecursiveMessage.ParseFrom(data65); Assert.Fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException) { // success. } CodedInputStream input = data64.CreateCodedInput(); input.SetRecursionLimit(8); try { TestRecursiveMessage.ParseFrom(input); Assert.Fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException) { // success. } }
/// <summary> /// Parses <paramref name="data"/> as an UnknownFieldSet and merge it /// with the set being built. This is just a small wrapper around /// MergeFrom(ICodedInputStream). /// </summary> public Builder MergeFrom(ByteString data) { CodedInputStream input = data.CreateCodedInput(); MergeFrom(input); input.CheckLastTagWas(0); return(this); }
public TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry) { CodedInputStream input = data.CreateCodedInput(); MergeFrom(input, extensionRegistry); input.CheckLastTagWas(0); return(ThisBuilder); }
public TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry) { CodedInputStream codedInputStream = data.CreateCodedInput(); this.MergeFrom(codedInputStream, extensionRegistry); codedInputStream.CheckLastTagWas(0u); return(this.ThisBuilder); }
public void MaliciousRecursion() { ByteString data64 = MakeRecursiveMessage(64).ToByteString(); ByteString data65 = MakeRecursiveMessage(65).ToByteString(); AssertMessageDepth(TestRecursiveMessage.ParseFrom(data64), 64); Assert.Throws <InvalidProtocolBufferException>(() => TestRecursiveMessage.ParseFrom(data65)); CodedInputStream input = data64.CreateCodedInput(); input.SetRecursionLimit(8); Assert.Throws <InvalidProtocolBufferException>(() => TestRecursiveMessage.ParseFrom(input)); }
private static void AssertFieldsInOrder(ByteString data) { CodedInputStream input = data.CreateCodedInput(); uint previousTag = 0; uint tag; string name; while (input.ReadTag(out tag, out name)) { Assert.IsTrue(tag > previousTag); previousTag = tag; input.SkipField(); } }
private void AssertFieldsInOrder(ByteString data) { CodedInputStream input = data.CreateCodedInput(); uint previousTag = 0; while (true) { uint tag = input.ReadTag(); if (tag == 0) { break; } Assert.IsTrue(tag > previousTag); previousTag = tag; input.SkipField(tag); } }
/// <summary> /// Called by MergeFieldFrom to parse a MessageSet extension. /// </summary> private void MergeMessageSetExtensionFromCodedStream(ICodedInputStream input, ExtensionRegistry extensionRegistry, IBuilder builder) { MessageDescriptor type = builder.DescriptorForType; // The wire format for MessageSet is: // message MessageSet { // repeated group Item = 1 { // required int32 typeId = 2; // required bytes message = 3; // } // } // "typeId" is the extension's field number. The extension can only be // a message type, where "message" contains the encoded bytes of that // message. // // In practice, we will probably never see a MessageSet item in which // the message appears before the type ID, or where either field does not // appear exactly once. However, in theory such cases are valid, so we // should be prepared to accept them. int typeId = 0; ByteString rawBytes = null; // If we encounter "message" before "typeId" IBuilderLite subBuilder = null; FieldDescriptor field = null; uint lastTag = WireFormat.MessageSetTag.ItemStart; uint tag; string name; while (input.ReadTag(out tag, out name)) { if (tag == 0 && name != null) { if (name == "type_id") { tag = WireFormat.MessageSetTag.TypeID; } else if (name == "message") { tag = WireFormat.MessageSetTag.Message; } } if (tag == 0) { if (input.SkipField()) { continue; //can't merge unknown without field tag } break; } lastTag = tag; if (tag == WireFormat.MessageSetTag.TypeID) { typeId = 0; // Zero is not a valid type ID. if (input.ReadInt32(ref typeId) && typeId != 0) { ExtensionInfo extension = extensionRegistry[type, typeId]; if (extension != null) { field = extension.Descriptor; subBuilder = extension.DefaultInstance.WeakCreateBuilderForType(); IMessageLite originalMessage = (IMessageLite)builder[field]; if (originalMessage != null) { subBuilder.WeakMergeFrom(originalMessage); } if (rawBytes != null) { // We already encountered the message. Parse it now. // TODO(jonskeet): Check this is okay. It's subtly different from the Java, as it doesn't create an input stream from rawBytes. // In fact, why don't we just call MergeFrom(rawBytes)? And what about the extension registry? subBuilder.WeakMergeFrom(rawBytes.CreateCodedInput()); rawBytes = null; } } else { // Unknown extension number. If we already saw data, put it // in rawBytes. if (rawBytes != null) { MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build()); rawBytes = null; } } } } else if (tag == WireFormat.MessageSetTag.Message) { if (subBuilder != null) { // We already know the type, so we can parse directly from the input // with no copying. Hooray! input.ReadMessage(subBuilder, extensionRegistry); } else if (input.ReadBytes(ref rawBytes)) { if (typeId != 0) { // We don't know how to parse this. Ignore it. MergeField(typeId, UnknownField.CreateBuilder().AddLengthDelimited(rawBytes).Build()); } } } else { // Unknown tag. Skip it. if (!input.SkipField()) { break; // end of group } } } if (lastTag != WireFormat.MessageSetTag.ItemEnd) { throw InvalidProtocolBufferException.InvalidEndTag(); } if (subBuilder != null) { builder[field] = subBuilder.WeakBuild(); } }