Skip to content

Commit 1a73f6f

Browse files
committed
fix: prevent metadata offset overflow into array space and convert shorts to uints before addition
1 parent 11e40ce commit 1a73f6f

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

test-app/build-tools/android-metadata-generator/src/src/com/telerik/metadata/Writer.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void writeTree(TreeNode root) throws Exception {
305305
outStringsStream.close();
306306
writeInt(0, outValueStream);
307307

308-
final int array_offset = 1000 * 1000 * 1000;
308+
final int array_offset = Integer.MAX_VALUE; // 2147483647, which is half of uint32
309309

310310
d.push(root);
311311
while (!d.isEmpty()) {
@@ -328,6 +328,10 @@ public void writeTree(TreeNode root) throws Exception {
328328
throw new Exception("should not happen");
329329
}
330330

331+
if ((n.nodeType & TreeNode.Array) != TreeNode.Array && Integer.toUnsignedLong(n.offsetValue) >= Integer.toUnsignedLong(array_offset)) {
332+
throw new Exception("Non-array metadata has overflown array space. Please report this issue.");
333+
}
334+
331335
d.addAll(n.children);
332336
}
333337

@@ -339,7 +343,7 @@ public void writeTree(TreeNode root) throws Exception {
339343
TreeNode n = d.pollFirst();
340344

341345
if (n.arrayElement != null) {
342-
n.offsetValue = array_offset + n.arrayElement.id;
346+
n.offsetValue = array_offset + Short.toUnsignedInt(n.arrayElement.id);
343347
}
344348

345349
if (!n.children.isEmpty()) {
@@ -387,6 +391,8 @@ public void writeTree(TreeNode root) throws Exception {
387391
obj.addProperty("id", Short.toUnsignedInt(n.id));
388392
obj.addProperty("nextSiblingId", Short.toUnsignedInt(n.nextSiblingId));
389393
obj.addProperty("firstChildId", Short.toUnsignedInt(n.firstChildId));
394+
obj.addProperty("offsetName", Integer.toUnsignedLong(n.offsetName));
395+
obj.addProperty("offsetValue", Integer.toUnsignedLong(n.offsetValue));
390396
obj.addProperty("name", n.getName());
391397
obj.addProperty("nodeType", n.nodeType);
392398
rootArray.add(obj);

test-app/runtime/src/main/cpp/MetadataReader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class MetadataReader {
167167

168168
private:
169169

170-
static const uint32_t ARRAY_OFFSET = 1000000000;
170+
static const uint32_t ARRAY_OFFSET = INT32_MAX; // 2147483647
171171

172172
MetadataTreeNode* BuildTree();
173173

0 commit comments

Comments
 (0)