Skip to content

Commit

Permalink
Library changes (end tag review)
Browse files Browse the repository at this point in the history
  • Loading branch information
ObsidianMinor committed Oct 30, 2018
1 parent 7b7b436 commit 97b7da7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
38 changes: 1 addition & 37 deletions csharp/src/Google.Protobuf/CodedInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public sealed class CodedInputStream : IDisposable
/// The absolute position of the end of the current message.
/// </summary>
private int currentLimit = int.MaxValue;
private uint tagLimit = uint.MaxValue;

private int recursionDepth = 0;

Expand Down Expand Up @@ -384,7 +383,7 @@ public uint ReadTag()
// If we actually read a tag with a field of 0, that's not a valid tag.
throw InvalidProtocolBufferException.InvalidTag();
}
if (ReachedLimit || ReachedTagLimit)
if (ReachedLimit)
{
return 0;
}
Expand Down Expand Up @@ -605,15 +604,9 @@ public void ReadGroup(IMessage builder)
{
throw InvalidProtocolBufferException.RecursionLimitExceeded();
}
uint oldTag = PushTag();
++recursionDepth;
builder.MergeFrom(this);
if (!ReachedTagLimit)
{
throw InvalidProtocolBufferException.TruncatedMessage();
}
--recursionDepth;
PopTag(oldTag);
}

/// <summary>
Expand Down Expand Up @@ -978,19 +971,6 @@ internal int PushLimit(int byteLimit)
return oldLimit;
}

/// <summary>
/// Sets tagLimit to the limit calculated by the last tag.
/// This is called when descending into a group message. The previous limit is returns.
/// </summary>
/// <returns>The old limit</returns>
internal uint PushTag()
{
uint oldLimit = tagLimit;
uint newLimit = WireFormat.MakeTag(WireFormat.GetTagFieldNumber(LastTag), WireFormat.WireType.EndGroup);
tagLimit = newLimit;
return oldLimit;
}

private void RecomputeBufferSizeAfterLimit()
{
bufferSize += bufferSizeAfterLimit;
Expand All @@ -1016,14 +996,6 @@ internal void PopLimit(int oldLimit)
RecomputeBufferSizeAfterLimit();
}

/// <summary>
/// Discards the current limit, returning the previous limit
/// </summary>
internal void PopTag(uint oldTag)
{
tagLimit = oldTag;
}

/// <summary>
/// Returns whether or not all the data before the limit has been read.
/// </summary>
Expand All @@ -1041,14 +1013,6 @@ internal bool ReachedLimit
}
}

internal bool ReachedTagLimit
{
get
{
return tagLimit == lastTag;
}
}

/// <summary>
/// Returns true if the stream has reached the end of the input. This is the
/// case if either the end of the underlying input source has been reached or
Expand Down
42 changes: 34 additions & 8 deletions csharp/src/Google.Protobuf/UnknownFieldSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ internal UnknownFieldSet AddOrReplaceField(int number, UnknownField field)
/// </summary>
/// <param name="input">The coded input stream containing the field</param>
/// <returns>false if the tag is an "end group" tag, true otherwise</returns>
private void MergeFieldFrom(CodedInputStream input)
private bool MergeFieldFrom(CodedInputStream input)
{
uint tag = input.LastTag;
int number = WireFormat.GetTagFieldNumber(tag);
Expand All @@ -193,25 +193,25 @@ private void MergeFieldFrom(CodedInputStream input)
{
ulong uint64 = input.ReadUInt64();
GetOrAddField(number).AddVarint(uint64);
return;
return true;
}
case WireFormat.WireType.Fixed32:
{
uint uint32 = input.ReadFixed32();
GetOrAddField(number).AddFixed32(uint32);
return;
return true;
}
case WireFormat.WireType.Fixed64:
{
ulong uint64 = input.ReadFixed64();
GetOrAddField(number).AddFixed64(uint64);
return;
return true;
}
case WireFormat.WireType.LengthDelimited:
{
ByteString bytes = input.ReadBytes();
GetOrAddField(number).AddLengthDelimited(bytes);
return;
return true;
}
case WireFormat.WireType.StartGroup:
{
Expand All @@ -222,11 +222,11 @@ private void MergeFieldFrom(CodedInputStream input)
set.MergeFieldFrom(input);
}
GetOrAddField(number).AddGroup(set);
return;
return true;
}
case WireFormat.WireType.EndGroup:
{
throw new InvalidProtocolBufferException("Merge an unknown field of end-group tag, indicating that the corresponding start-group was missing.");
return false;
}
default:
throw new InvalidOperationException("Wire Type is invalid.");
Expand Down Expand Up @@ -254,8 +254,34 @@ public static UnknownFieldSet MergeFieldFrom(UnknownFieldSet unknownFields,
{
unknownFields = new UnknownFieldSet();
}
unknownFields.MergeFieldFrom(input);
if (!unknownFields.MergeFieldFrom(input))
{
throw new InvalidProtocolBufferException("Merge an unknown field of end-group tag, indicating that the corresponding start-group was missing."); // match the old code-gen
}
return unknownFields;
}

/// <summary>
/// Create a new UnknownFieldSet if unknownFields is null.
/// Parse a single field from <paramref name="input"/> and merge it
/// into unknownFields. If <paramref name="input"/> is configured to discard unknown fields,
/// <paramref name="unknownFields"/> will be returned as-is and the field will be skipped.
/// </summary>
/// <param name="unknownFields">The UnknownFieldSet which need to be merged</param>
/// <param name="input">The coded input stream containing the field</param>
/// <returns>The merged UnknownFieldSet</returns>
public static bool MergeFieldFrom(ref UnknownFieldSet unknownFields, CodedInputStream input)
{
if (input.DiscardUnknownFields)
{
input.SkipLastField();
return true;
}
if (unknownFields == null)
{
unknownFields = new UnknownFieldSet();
}
return unknownFields.MergeFieldFrom(input);
}

/// <summary>
Expand Down

0 comments on commit 97b7da7

Please sign in to comment.