@@ -103,26 +103,48 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
103
103
104
104
// Get format-version
105
105
uint8_t FormatVersion = De.getU8 (Cursor);
106
+ if (!Cursor)
107
+ return Cursor.takeError ();
106
108
if (ELFAttrs::Format_Version != FormatVersion)
107
109
return createStringError (errc::invalid_argument,
108
110
" unrecognized format-version: 0x" +
109
111
utohexstr (FormatVersion));
110
112
111
113
while (!De.eof (Cursor)) {
112
114
uint32_t ExtBASubsectionLength = De.getU32 (Cursor);
113
- // Minimal valid Extended Build Attributes subsection header size is at
115
+ if (!Cursor)
116
+ return Cursor.takeError ();
117
+ // Minimal valid Extended Build Attributes subsection size is at
114
118
// least 8: length(4) name(at least a single char + null) optionality(1) and
115
119
// type(1)
116
- if (ExtBASubsectionLength < 8 )
120
+ // Extended Build Attributes subsection has to fit inside the section.
121
+ if (ExtBASubsectionLength < 8 ||
122
+ ExtBASubsectionLength > (Section.size () - Cursor.tell () + 4 ))
117
123
return createStringError (
118
124
errc::invalid_argument,
119
125
" invalid Extended Build Attributes subsection size at offset: " +
120
126
utohexstr (Cursor.tell () - 4 ));
121
127
122
128
StringRef VendorName = De.getCStrRef (Cursor);
129
+ if (!Cursor)
130
+ return Cursor.takeError ();
123
131
uint8_t IsOptional = De.getU8 (Cursor);
132
+ if (!Cursor)
133
+ return Cursor.takeError ();
134
+ if (!(0 == IsOptional || 1 == IsOptional))
135
+ return createStringError (
136
+ errc::invalid_argument,
137
+ " \n invalid Optionality at offset " + utohexstr (Cursor.tell () - 4 ) +
138
+ " : " + utohexstr (IsOptional) + " (Options are 1|0)" );
124
139
StringRef IsOptionalStr = IsOptional ? " optional" : " required" ;
125
140
uint8_t Type = De.getU8 (Cursor);
141
+ if (!Cursor)
142
+ return Cursor.takeError ();
143
+ if (!(0 == Type || 1 == Type))
144
+ return createStringError (errc::invalid_argument,
145
+ " \n invalid Type at offset " +
146
+ utohexstr (Cursor.tell () - 4 ) + " : " +
147
+ utohexstr (Type) + " (Options are 1|0)" );
126
148
StringRef TypeStr = Type ? " ntbs" : " uleb128" ;
127
149
128
150
BuildAttributeSubSection BASubSection;
@@ -144,23 +166,29 @@ Error ELFExtendedAttrParser::parse(ArrayRef<uint8_t> Section,
144
166
// Offset in Section
145
167
uint64_t OffsetInSection = Cursor.tell ();
146
168
// Size: 4 bytes, Vendor Name: VendorName.size() + 1 (null termination),
147
- // optionality: 1, size : 1
169
+ // optionality: 1, type : 1
148
170
uint32_t BytesAllButAttributes = 4 + (VendorName.size () + 1 ) + 1 + 1 ;
149
171
while (Cursor.tell () <
150
172
(OffsetInSection + ExtBASubsectionLength - BytesAllButAttributes)) {
151
173
152
174
uint64_t Tag = De.getULEB128 (Cursor);
175
+ if (!Cursor)
176
+ return Cursor.takeError ();
153
177
154
178
StringRef TagName = getTagName (VendorName, Tag);
155
179
156
180
uint64_t ValueInt = 0 ;
157
181
std::string ValueStr = " " ;
158
182
if (Type) { // type==1 --> ntbs
159
183
ValueStr = De.getCStrRef (Cursor);
184
+ if (!Cursor)
185
+ return Cursor.takeError ();
160
186
if (Sw)
161
187
Sw->printString (" " != TagName ? TagName : utostr (Tag), ValueStr);
162
188
} else { // type==0 --> uleb128
163
189
ValueInt = De.getULEB128 (Cursor);
190
+ if (!Cursor)
191
+ return Cursor.takeError ();
164
192
if (Sw)
165
193
Sw->printNumber (" " != TagName ? TagName : utostr (Tag), ValueInt);
166
194
}
0 commit comments