Skip to content

Commit d7f82c3

Browse files
committed
Handle malformed dumpsys package output.
Some devices produce malformed dumpsys package output, adding in extra newlines. This change handles a specific version of how this extra whitespace breaks the parser. Fixes #1077
1 parent 17d4ffb commit d7f82c3

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

core/os/android/adb/adb_data_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ Activity Resolver Table:
9595
Packages:
9696
Package [com.google.foo] (ffffffc):
9797
userId=12345
98-
primaryCpuAbi=armeabi-v7a
98+
*** extra whitespace on next line is for testing https://github.com/google/agi/issues/1077 ***
99+
*** Do not remove it, doing so will make the tests fail ***
100+
101+
primaryCpuAbi=armeabi-v7a
99102
secondaryCpuAbi=null
100103
versionCode=902107 minSdk=14 targetSdk=15
101104
flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ]

core/os/android/adb/installed_package.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ func (t *treeNode) find(name string) *treeNode {
211211

212212
func parseTabbedTree(str string) *treeNode {
213213
head := &treeNode{depth: -1}
214+
extraDepth := 0
214215
for _, line := range strings.Split(str, "\n") {
215216
line = strings.TrimRight(line, "\r")
216217
if line == "" {
@@ -219,14 +220,35 @@ func parseTabbedTree(str string) *treeNode {
219220

220221
// Calculate the line's depth
221222
depth := 0
222-
for i, r := range line {
223+
for _, r := range line {
223224
if r == ' ' {
224225
depth++
225226
} else {
226-
line = line[i:]
227227
break
228228
}
229229
}
230+
line = line[depth:]
231+
232+
if line == "" {
233+
// A line containing only whitespace probably comes from an extra newline
234+
// character being printed before the intended line. So, add the depth that
235+
// would have resulted from this empty line to the next line. Example with
236+
// front spaces shown as '_':
237+
// ...
238+
// Known Packages:
239+
// _ Package [foo]:
240+
// __
241+
//Package categories:
242+
// ___ category bar
243+
// ...
244+
// In the above example "Package categories" was meant to be indented by
245+
// two spaces, which are present on the previous line.
246+
extraDepth += depth
247+
continue
248+
}
249+
250+
depth += extraDepth
251+
extraDepth = 0
230252

231253
// Find the insertion point
232254
for {

0 commit comments

Comments
 (0)