Skip to content

Commit 3bcbdc5

Browse files
rjobredeaux3copybara-github
authored andcommitted
Validate version in build tools directory discovery for android sdk repository
Inspired from #146 PiperOrigin-RevId: 582341456 Change-Id: I40930d75746d9217ae2eb197f343f35d0276260b
1 parent 6290ae0 commit 3bcbdc5

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

rules/android_revision.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ AndroidRevisionInfo = provider(
2626
},
2727
)
2828

29+
def is_android_revision(input):
30+
return all([c.isdigit() for c in input.strip().split(".")])
31+
2932
def parse_android_revision(input):
3033
"""Parse and Android revision string and return an AndroidRevisionInfo.
3134
@@ -35,10 +38,10 @@ def parse_android_revision(input):
3538
Returns:
3639
An AndroidRevisionInfo provider representing the input.
3740
"""
38-
input = input.strip()
39-
parts = input.split(".")
40-
if len(parts) < 1:
41+
if not is_android_revision(input):
4142
fail("Invalid Android revision %s" % input)
43+
parts = input.strip().split(".")
44+
4245
major = int(parts[0]) if len(parts) >= 1 else 0
4346
minor = int(parts[1]) if len(parts) >= 2 else 0
4447
micro = int(parts[2]) if len(parts) >= 3 else 0

rules/android_sdk_repository/rule.bzl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
"""Bazel rule for Android sdk repository."""
1616

17-
load("//rules:android_revision.bzl", "compare_android_revisions", "parse_android_revision")
17+
load(
18+
"//rules:android_revision.bzl",
19+
"compare_android_revisions",
20+
"is_android_revision",
21+
"parse_android_revision",
22+
)
1823

1924
_SDK_REPO_TEMPLATE = Label(":template.bzl")
2025
_EMPTY_SDK_REPO_TEMPLATE = Label(":empty.template.bzl")
@@ -59,8 +64,9 @@ def _newest_build_tools(repo_ctx, android_sdk_path):
5964
return None
6065
for entry in build_tools_path.readdir():
6166
name = entry.basename
62-
revision = parse_android_revision(name)
63-
highest = compare_android_revisions(highest, revision)
67+
if is_android_revision(name):
68+
revision = parse_android_revision(name)
69+
highest = compare_android_revisions(highest, revision)
6470
return highest
6571

6672
def _find_system_images(repo_ctx, android_sdk_path):

test/rules/android_sdk_repository/test_lib.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ EOF
110110
}
111111

112112
function test_build_tools_largest() {
113-
# create several build tools
113+
# create several build tools, including ones with malformed names
114+
# (e.g. .DStore) to confirm they are properly excluded and do not cause
115+
# crashes.
114116
local sdk_path="$(create_android_sdk)"
115117
add_platforms "${sdk_path}" 31
116-
add_build_tools "${sdk_path}" 10.1.2 20.2.3 30.3.4
118+
add_build_tools "${sdk_path}" 10.1.2 20.2.3 30.3.4 .DStore
117119

118120
# Add to repository.
119121
cat >> WORKSPACE <<EOF

0 commit comments

Comments
 (0)