| 100 | } |
| 101 | |
| 102 | static Result CalculateDynamicDescriptorSize(LoadContext* load_context, InputBuffer* ib, const Descriptor* desc, bool is_dynamic_type) |
| 103 | { |
| 104 | while (!ib->Eof()) |
| 105 | { |
| 106 | uint32_t tag; |
| 107 | if (!ib->ReadVarInt32(&tag)) |
| 108 | return RESULT_WIRE_FORMAT_ERROR; |
| 109 | |
| 110 | uint32_t key = tag >> 3; |
| 111 | uint32_t type = tag & 0x7; |
| 112 | |
| 113 | if (key == 0) |
| 114 | return RESULT_WIRE_FORMAT_ERROR; |
| 115 | |
| 116 | const FieldDescriptor* field = FindField(desc, key, 0); |
| 117 | if (!field) |
| 118 | { |
| 119 | // Unknown field, just skip |
| 120 | Result e = SkipField(ib, type); |
| 121 | DDF_CHECK_RESULT(e); |
| 122 | continue; |
| 123 | } |
| 124 | |
| 125 | if (field->m_Type == TYPE_MESSAGE) |
| 126 | { |
| 127 | // All submessages are length-delimited |
| 128 | uint32_t length; |
| 129 | if (!ib->ReadVarInt32(&length)) |
| 130 | return RESULT_WIRE_FORMAT_ERROR; |
| 131 | |
| 132 | // Create a view of just this submessage's bytes |
| 133 | InputBuffer sub_ib; |
| 134 | if (!ib->SubBuffer(length, &sub_ib)) |
| 135 | return RESULT_WIRE_FORMAT_ERROR; |
| 136 | |
| 137 | is_dynamic_type = is_dynamic_type || !field->m_FullyDefinedType; |
| 138 | if (is_dynamic_type) |
| 139 | { |
| 140 | // We need to account for the injected oneof index value that we insert into the structs via ddfc.py! |
| 141 | uint32_t dynamic_size = field->m_MessageDescriptor->m_Size; |
| 142 | if (field->m_OneOfIndex != DDF_NO_ONE_OF_INDEX) |
| 143 | { |
| 144 | dynamic_size += sizeof(uint32_t); |
| 145 | } |
| 146 | |
| 147 | load_context->AddDynamicMessageSize(dynamic_size); |
| 148 | } |
| 149 | |
| 150 | // Recurse into the submessage descriptor |
| 151 | Result e = CalculateDynamicDescriptorSize(load_context, &sub_ib, field->m_MessageDescriptor, is_dynamic_type); |
| 152 | DDF_CHECK_RESULT(e); |
| 153 | |
| 154 | // Ensure the sub-buffer is fully consumed |
| 155 | if (!sub_ib.Eof()) |
| 156 | { |
| 157 | return RESULT_WIRE_FORMAT_ERROR; |
| 158 | } |
| 159 | } |
no test coverage detected