adb: fix infinite loop when attempting to push to //foo.

dirname (on glibc, at least) preserves multiple leading slashes, and we
were looping until path != "/", which would lead to an infinite loop
when attempting to push to a path like //data/local/tmp.

Bug: http://b/141311284
Test: python -m unittest test_device.FileOperationsTest.test_push_multiple_slash_root
Change-Id: I182b3e89ef52579c716fdb525e9215f1fe822477
This commit is contained in:
Josh Gao 2019-09-26 01:49:56 +08:00
parent 983f76b3c6
commit 76b64ba826
2 changed files with 24 additions and 2 deletions

View file

@ -849,6 +849,16 @@ static bool local_build_list(SyncConnection& sc, std::vector<copyinfo>* file_lis
return true;
}
// dirname("//foo") returns "//", so we can't do the obvious `path == "/"`.
static bool is_root_dir(std::string_view path) {
for (char c : path) {
if (c != '/') {
return false;
}
}
return true;
}
static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath,
std::string rpath, bool check_timestamps,
bool list_only) {
@ -862,8 +872,8 @@ static bool copy_local_dir_remote(SyncConnection& sc, std::string lpath,
std::vector<copyinfo> file_list;
std::vector<std::string> directory_list;
for (std::string dirpath = rpath; dirpath != "/"; dirpath = android::base::Dirname(dirpath)) {
directory_list.push_back(dirpath);
for (std::string path = rpath; !is_root_dir(path); path = android::base::Dirname(path)) {
directory_list.push_back(path);
}
std::reverse(directory_list.begin(), directory_list.end());

View file

@ -884,6 +884,18 @@ class FileOperationsTest(DeviceTest):
remote_path += '/filename'
self.device.push(local=tmp_file.name, remote=remote_path)
def test_push_multiple_slash_root(self):
"""Regression test for pushing to //data/local/tmp.
Bug: http://b/141311284
"""
with tempfile.NamedTemporaryFile() as tmp_file:
tmp_file.write('\0' * 1024 * 1024)
tmp_file.flush()
remote_path = '/' + self.DEVICE_TEMP_DIR + '/test_push_multiple_slash_root'
self.device.shell(['rm', '-rf', remote_path])
self.device.push(local=tmp_file.name, remote=remote_path)
def _test_pull(self, remote_file, checksum):
tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False)
tmp_write.close()