Skip to content

Properly escape commandline string.#712

Draft
hysw wants to merge 1 commit intogoogle:mainfrom
hysw:shell
Draft

Properly escape commandline string.#712
hysw wants to merge 1 commit intogoogle:mainfrom
hysw:shell

Conversation

@hysw
Copy link
Collaborator

@hysw hysw commented Jan 15, 2026

No description provided.

@@ -0,0 +1,209 @@
/*
Copyright 2023 Google Inc.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2026

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, this is supposed to be the same file as command_utils.cc, I guess most of the content are changed

Copy link
Collaborator

@wangra-google wangra-google Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a more general question: why do you put the platform dependent func in this file instead of the command_utils_.cpp? (for example, why not put MakeWindowsCommand() in src/dive/os/command_utils_win32.cpp)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MakeWindowsCommand is not platform dependent, it's just a string manipulation function which does string escape according to Windows rules.

MakeUnixCommand is additionally required to escape adb shell command correctly even on Windows.

Copy link
Collaborator Author

@hysw hysw Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. Python expose both Windows and Linux path function, and you can run the following on Linux environment

import pathlib
print(str(pathlib.PureWindowsPath("C:/Program Files")))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that "MakeWindowsCommand" is only used by command_utils_win32.cpp? do you see possible usage for other platforms as well? I understand that MakeWindowsCommand is platform-independent logic (just string manipulation), If we moved MakeWindowsCommand to command_utils_win32.cpp, It would clearly signal that this logic is intended for the Windows platform (or targeting Windows).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name MakeWindowsCommand is clear enough that it is for Windows.

context.mgr.GetDevice()
->Adb()
.Run(absl::StrFormat("shell rm -rf %s", on_device_capture_directory))
.Shell(absl::StrFormat("rm -rf %s", on_device_capture_directory))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would this fail if there is a space in on_device_capture_directory?

i see you have 2 overload Shell(), and this will trigger the

    absl::Status Shell(std::string_view command) const
    {
        return RunCommand("adb", {"-s", m_serial, "shell", std::string(command)}).status();
    }

which doesn't call "MakeUnixCommand?" on the command string?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it will fail.

It should be .Shell("rm", {"-rf", on_device_capture_directory}), however most of these are find-replace regex edit.
There's still a problem if on_device_capture_directory starts with - and rm think it is a parameter rather than a flag. However, I don't aim to solve every problem.

I'd like to have adb.Run(Adb::Remove{.recursive = true, .force = true, .path= on_device_capture_directory}), but that's a lot of code to write.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants