SetSizeLimit() public method

Set the maximum message size.
In order to prevent malicious messages from exhausting memory or causing integer overflows, CodedInputStream limits how large a message may be. The default limit is 64MB. You should set this limit as small as you can without harming your app's functionality. Note that size limits only apply when reading from an InputStream, not when constructed around a raw byte array (nor with ByteString.NewCodedInput). If you want to read several messages from a single CodedInputStream, you can call ResetSizeCounter() after each message to avoid hitting the size limit.
public SetSizeLimit ( int limit ) : int
limit int
return int
Ejemplo n.º 1
0
        public void ResetSizeCounter()
        {
            CodedInputStream input = CodedInputStream.CreateInstance(
                new SmallBlockInputStream(new byte[256], 8));

            input.SetSizeLimit(16);
            input.ReadRawBytes(16);

            try
            {
                input.ReadRawByte();
                Assert.Fail("Should have thrown an exception!");
            }
            catch (InvalidProtocolBufferException)
            {
                // Success.
            }

            input.ResetSizeCounter();
            input.ReadRawByte(); // No exception thrown.

            try
            {
                input.ReadRawBytes(16); // Hits limit again.
                Assert.Fail("Should have thrown an exception!");
            }
            catch (InvalidProtocolBufferException)
            {
                // Success.
            }
        }
Ejemplo n.º 2
0
        public IEnumerator <TMessage> GetEnumerator()
        {
            using (Stream stream = streamProvider())
            {
                CodedInputStream input = CodedInputStream.CreateInstance(stream);
                input.SetSizeLimit(sizeLimit);
                uint   tag;
                string name;
                while (input.ReadTag(out tag, out name))
                {
                    if ((tag == 0 && name == "item") || (tag == ExpectedTag))
                    {
                        IBuilder builder = defaultMessageInstance.WeakCreateBuilderForType();
                        input.ReadMessage(builder, extensionRegistry);
                        yield return((TMessage)builder.WeakBuild());
                    }
                    else
                    {
                        throw InvalidProtocolBufferException.InvalidMessageStreamTag();
                    }

                    input.ResetSizeCounter();
                }
            }
        }
Ejemplo n.º 3
0
        public void SizeLimit()
        {
            // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
            // apply to the latter case.
            MemoryStream     ms    = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
            CodedInputStream input = CodedInputStream.CreateInstance(ms);

            input.SetSizeLimit(16);

            Assert.Throws <InvalidProtocolBufferException>(() => TestAllTypes.ParseFrom(input));
        }
Ejemplo n.º 4
0
        public void ResetSizeCounter()
        {
            CodedInputStream input = CodedInputStream.CreateInstance(
                new SmallBlockInputStream(new byte[256], 8));

            input.SetSizeLimit(16);
            input.ReadRawBytes(16);

            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawByte());

            input.ResetSizeCounter();
            input.ReadRawByte(); // No exception thrown.

            Assert.Throws <InvalidProtocolBufferException>(() => input.ReadRawBytes(16));
        }
        public void SizeLimit()
        {
            // Have to use a Stream rather than ByteString.CreateCodedInput as SizeLimit doesn't
            // apply to the latter case.
            MemoryStream     ms    = new MemoryStream(TestUtil.GetAllSet().ToByteString().ToByteArray());
            CodedInputStream input = CodedInputStream.CreateInstance(ms);

            input.SetSizeLimit(16);

            try {
                TestAllTypes.ParseFrom(input);
                Assert.Fail("Should have thrown an exception!");
            } catch (InvalidProtocolBufferException) {
                // success.
            }
        }
Ejemplo n.º 6
0
        public IEnumerator <TMessage> GetEnumerator()
        {
            using (Stream stream = streamProvider()) {
                CodedInputStream input = CodedInputStream.CreateInstance(stream);
                input.SetSizeLimit(sizeLimit);
                uint tag;
                while ((tag = input.ReadTag()) != 0)
                {
                    if (tag != ExpectedTag)
                    {
                        throw InvalidProtocolBufferException.InvalidMessageStreamTag();
                    }
                    yield return(messageReader(input, extensionRegistry));

                    input.ResetSizeCounter();
                }
            }
        }