Skip to content

Commit

Permalink
Handle malformed dumpsys package output.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pmuetschard committed Apr 20, 2022
1 parent 17d4ffb commit d7f82c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
5 changes: 4 additions & 1 deletion core/os/android/adb/adb_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ Activity Resolver Table:
Packages:
Package [com.google.foo] (ffffffc):
userId=12345
primaryCpuAbi=armeabi-v7a
*** extra whitespace on next line is for testing https://github.com/google/agi/issues/1077 ***
*** Do not remove it, doing so will make the tests fail ***
primaryCpuAbi=armeabi-v7a
secondaryCpuAbi=null
versionCode=902107 minSdk=14 targetSdk=15
flags=[ HAS_CODE ALLOW_CLEAR_USER_DATA ALLOW_BACKUP ]
Expand Down
26 changes: 24 additions & 2 deletions core/os/android/adb/installed_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (t *treeNode) find(name string) *treeNode {

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

// Calculate the line's depth
depth := 0
for i, r := range line {
for _, r := range line {
if r == ' ' {
depth++
} else {
line = line[i:]
break
}
}
line = line[depth:]

if line == "" {
// A line containing only whitespace probably comes from an extra newline
// character being printed before the intended line. So, add the depth that
// would have resulted from this empty line to the next line. Example with
// front spaces shown as '_':
// ...
// Known Packages:
// _ Package [foo]:
// __
//Package categories:
// ___ category bar
// ...
// In the above example "Package categories" was meant to be indented by
// two spaces, which are present on the previous line.
extraDepth += depth
continue
}

depth += extraDepth
extraDepth = 0

// Find the insertion point
for {
Expand Down

0 comments on commit d7f82c3

Please sign in to comment.