← Back to Pull requests

Shell-quote every compiler option token on POSIX #907

Open
paoloantinori opened this pull request with 169 lines diff across 5 files
View on GitHub
paoloantinori paoloantinori commented on Oct 07, 2026 10:30 UTC
Author

prepare_build_options only quoted tokens that do not start with a dash, and it used double quotes. Both choices are unsafe because the command line reaches clspv through popen(3):

  • A single-token macro like -DXM2S(x)=#x (valid per the OpenCL specification, which allows both -D name and -Dname) is passed through unquoted. The shell aborts with a syntax error before clspv runs, and the application sees CL_BUILD_PROGRAM_FAILURE with an empty build log. hashcat passes exactly this kind of define, which is how I found it.

  • A value wrapped in double quotes still undergoes parameter and command substitution, so -DVALUE=$x expands silently.
  • This change POSIX-quotes every token, using single quotes with the standard '\'' escape for embedded single quotes, so every token reaches clspv verbatim. Windows keeps the previous behavior because cmd.exe treats single quotes as regular characters.

    Verification: I extracted the exact quoting loop into a standalone test program, generated the command line for adversarial tokens (parentheses, dollar signs, single and double quotes, semicolons, backticks, spaces inside a token, globs, pipes), ran it through popen against a receiver script, and compared the child argv. All tokens arrive intact. The full library build is left to CI; the change only touches string assembly in prepare_build_options.

    Found while investigating #906 (different root cause, same session tripped over this first).

    Diff: 169 lines (+135 / -34) across 5 files Reviewer: rjodinchr Review duration: 24090m
    All Vouch review confidence checks passed
    Residual risk 0.06 = Change Risk (0.63) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.63 · 169 lines diff across 5 files
    0.63
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.90 · 5 substantive comment(s)
    0.90
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    90%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.307
    Sensitive Paths sensitive_paths
    0.063
    Real PR Conversation Timeline
    30 items
    rjodinchr rjodinchr commented on Oct 07, 2026 12:35 UTC
    Contributor

    clvk tries to avoid platform specific code outside of utils.cpp, I think it would be better to move that over there.
    Also a test would be welcome here, it should not be too complicated to add.

    paoloantinori paoloantinori commented on Oct 07, 2026 13:15 UTC
    Author

    Moved to utils.cpp as shell_quote_token and quote_options_for_shell, with tests under tests/utils. While moving it I also made the splitter honour double-quoted spans and drop the quotes, so an application option like -DMSG="hello world" now reaches clspv as a single argv entry, and quoted the -I/temp-dir/input/output paths that get appended outside prepare_build_options.

    rjodinchr rjodinchr commented on code on Oct 07, 2026 13:22 UTC
    Code Review
    src/utils.cpp:1
    @@ -30,6 +31,61 @@
     #include <pthread.h>
     #endif
     
    +std::string shell_quote_token(const std::string& token) {
    +#ifdef WIN32
    +    // cmd.exe treats single quotes as regular characters, so keep the
    +    // historical double quoting behaviour there.
    +    if (token.find("-") == 0) {
    +        return token;
    +    }
    +    return "\"" + token + "\"";
    +#else
    +    // The command line is executed through popen(3), so shell-quote the
    +    // token to hand it to the child process verbatim.
    +    std::string quoted = "'";
    +    for (char c : token) {
    +        if (c == '\'') {
    +            quoted += "'\\''";
    +        } else {
    +            quoted += c;
    +        }
    +    }
    +    quoted += "'";
    +    return quoted;
    +#endif
    +}
    +
    +std::string quote_options_for_shell(const std::string& options) {

    This function is not using platform specific code and is only used once. I don't mind keeping it, but I don't think utils.cpp is the right place for it.

    rjodinchr rjodinchr commented on code on Oct 07, 2026 13:23 UTC
    Code Review
    tests/CMakeLists.txt:1
    @@ -68,6 +68,7 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/simple-from-il-binary)
     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/simple-from-llvm-ir-binary)
     endif()
     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sha1)
    +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/utils)

    I don't think we want a new test for those. Can't we have them added inside api_tests?

    paoloantinori paoloantinori commented on Oct 08, 2026 06:57 UTC
    Author

    Done in c339880: shell_quote_token moves to program.cpp next to the file paths that use it, utils.cpp keeps only the option-string splitter with its own local per-token helper, and the tests now live inside api_tests (tests/api/shell_quote.cpp) instead of a separate target.

    rjodinchr rjodinchr commented on code on Oct 08, 2026 07:02 UTC
    Code Review
    src/program.cpp:1
    @@ -977,6 +977,30 @@ size_t cvk_program::binary_size() const {
         return 0;
     }
     
    +std::string shell_quote_token(const std::string& token) {

    This function is platform specific, it should not be here

    rjodinchr rjodinchr commented on code on Oct 08, 2026 07:02 UTC
    Code Review
    src/utils.cpp:1
    @@ -30,6 +31,61 @@
     #include <pthread.h>
     #endif
     
    +std::string shell_quote_token(const std::string& token) {
    +#ifdef WIN32
    +    // cmd.exe treats single quotes as regular characters, so keep the
    +    // historical double quoting behaviour there.
    +    if (token.find("-") == 0) {
    +        return token;
    +    }
    +    return "\"" + token + "\"";
    +#else
    +    // The command line is executed through popen(3), so shell-quote the
    +    // token to hand it to the child process verbatim.
    +    std::string quoted = "'";
    +    for (char c : token) {
    +        if (c == '\'') {
    +            quoted += "'\\''";
    +        } else {
    +            quoted += c;
    +        }
    +    }
    +    quoted += "'";
    +    return quoted;
    +#endif
    +}
    +
    +std::string quote_options_for_shell(const std::string& options) {

    This function is still not platform specific, it should not be here.

    rjodinchr rjodinchr commented on code on Oct 08, 2026 07:04 UTC
    Code Review
    tests/api/shell_quote.cpp:1
    @@ -0,0 +1,55 @@
    +// Copyright 2026 The clvk authors.
    +//
    +// Licensed under the Apache License, Version 2.0 (the "License");
    +// you may not use this file except in compliance with the License.
    +// You may obtain a copy of the License at
    +//
    +//     http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS,
    +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +// See the License for the specific language governing permissions and
    +// limitations under the License.
    +
    +// Exercises the shell-quoting of compiler option tokens in
    +// prepare_build_options (program.cpp); declared here because it is a
    +// file-static helper there, made visible for testing.
    +std::string shell_quote_token(const std::string& token);
    +
    +#include <gtest/gtest.h>
    +
    +#ifndef WIN32

    api_tests is not meant to test clvk inside the function.
    That test should use the OpenCL API to test the quote in the build options.
    Also, I think it should be platform-agnostic.

    paoloantinori paoloantinori commented on Oct 08, 2026 17:43 UTC
    Author

    All comments addressed in bd82dba:

  • shell_quote_token (with the WIN32 path) is back in utils.cpp where platform-specific code belongs; quote_options_for_shell moved to program.cpp as a static helper next to prepare_build_options, its only consumer.

  • The test is now an API-level test: WithContext fixture, clBuildProgram with nine option sets including the hashcat pattern (-DXM2S(x)=#x) and a single-quote case. Success of the build is the assertion, platform-agnostic.

  • The three remaining unquoted file operands on popen command lines (llvmspirv_input_file, the from-IL clspv_input_file) are quoted.
  • rjodinchr rjodinchr commented on code on Oct 09, 2026 06:22 UTC
    Code Review
    src/program.cpp:1
    @@ -1097,29 +1125,11 @@ std::string cvk_program::prepare_build_options(const cvk_device* device) const {
             options += "-cl-arm-non-uniform-work-group-size";
         }
     
    -    // split options into a vector
    -    std::istringstream iss(options);
    -    std::vector<std::string> vector_options;
    -    std::string token;
    -    while (std::getline(iss, token, ' ')) {
    -        vector_options.push_back(token);
    -    }
    -
    -    // loop through the options and quote the ones that need it
    -    std::string quoted_options;
    -    for (size_t i = 0; i < vector_options.size(); i++) {
    -        if (vector_options[i].empty()) {
    -            continue;
    -        }
    -        if (vector_options[i].find("-") == 0) {
    -            quoted_options += vector_options[i];
    -        } else {
    -            quoted_options += "\"" + vector_options[i] + "\"";
    -        }
    -        quoted_options += " ";
    -    }
    -
    -    return quoted_options;
    +    // The clspv command line is executed through popen(3), so shell-quote
    +    // everything: application-provided options can contain parentheses,
    +    // dollar signs, quotes or other metacharacters (e.g. "-DXM2S(x)=#x" or
    +    // "-DVALUE=$x"), which the shell would otherwise interpret or reject.

    Not useful I believe, and also not always true. clspv can be setup to be used "online" instead of executed through popen

    rjodinchr rjodinchr commented on Oct 09, 2026 06:26 UTC
    Reviewer

    I don't understand why all the filenames (llvmspirv_input_file, clspv_input_file, input_file & tmp_folder) need to be quoted. Also if we are to quote them, can we do it only once per file?

    paoloantinori paoloantinori commented on Oct 09, 2026 06:57 UTC
    Author

    Removed. You are right that it is not always true: the online compiler path bypasses popen entirely.

    rjodinchr rjodinchr commented on code on Oct 09, 2026 07:07 UTC
    Code Review
    tests/api/shell_quote.cpp:1
    @@ -0,0 +1,53 @@
    +// Copyright 2026 The clvk authors.
    +//
    +// Licensed under the Apache License, Version 2.0 (the "License");
    +// you may not use this file except in compliance with the License.
    +// You may obtain a copy of the License at
    +//
    +//     http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS,
    +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +// See the License for the specific language governing permissions and
    +// limitations under the License.
    +
    +// Tests that build options containing shell metacharacters are passed to
    +// the compiler verbatim: clBuildProgram must succeed with -D macros whose
    +// values include parentheses, dollar signs, quotes, and single quotes.
    +
    +#include "testcl.hpp"
    +
    +#include <gtest/gtest.h>
    +
    +namespace {
    +
    +const char* source = R"(
    +kernel void test_macro(__global uint* out, uint x) {
    +    out[0] = x;
    +}
    +)";
    +
    +} // namespace
    +
    +TEST_F(WithContext, BuildOptionsWithMetacharacters) {

    My understanding is that the test check that we do not get any segfault. Could we also make sure that the macro is well passed in the test?

    I think we should be able to use printf in the kernel and check the output on the host side for example.
    Have a look at tests/api/printf.cpp for example on how to use printf in the test.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 10:48 UTC
    Code Review
    src/program.cpp:1
    @@ -1097,29 +1125,11 @@ std::string cvk_program::prepare_build_options(const cvk_device* device) const {
             options += "-cl-arm-non-uniform-work-group-size";
         }
     
    -    // split options into a vector
    -    std::istringstream iss(options);
    -    std::vector<std::string> vector_options;
    -    std::string token;
    -    while (std::getline(iss, token, ' ')) {
    -        vector_options.push_back(token);
    -    }
    -
    -    // loop through the options and quote the ones that need it
    -    std::string quoted_options;
    -    for (size_t i = 0; i < vector_options.size(); i++) {
    -        if (vector_options[i].empty()) {
    -            continue;
    -        }
    -        if (vector_options[i].find("-") == 0) {
    -            quoted_options += vector_options[i];
    -        } else {
    -            quoted_options += "\"" + vector_options[i] + "\"";
    -        }
    -        quoted_options += " ";
    -    }
    -
    -    return quoted_options;
    +    // The clspv command line is executed through popen(3), so shell-quote
    +    // everything: application-provided options can contain parentheses,
    +    // dollar signs, quotes or other metacharacters (e.g. "-DXM2S(x)=#x" or
    +    // "-DVALUE=$x"), which the shell would otherwise interpret or reject.

    Done in bbb8a07.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 10:48 UTC
    Code Review
    tests/api/shell_quote.cpp:1
    @@ -0,0 +1,53 @@
    +// Copyright 2026 The clvk authors.
    +//
    +// Licensed under the Apache License, Version 2.0 (the "License");
    +// you may not use this file except in compliance with the License.
    +// You may obtain a copy of the License at
    +//
    +//     http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS,
    +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +// See the License for the specific language governing permissions and
    +// limitations under the License.
    +
    +// Tests that build options containing shell metacharacters are passed to
    +// the compiler verbatim: clBuildProgram must succeed with -D macros whose
    +// values include parentheses, dollar signs, quotes, and single quotes.
    +
    +#include "testcl.hpp"
    +
    +#include <gtest/gtest.h>
    +
    +namespace {
    +
    +const char* source = R"(
    +kernel void test_macro(__global uint* out, uint x) {
    +    out[0] = x;
    +}
    +)";
    +
    +} // namespace
    +
    +TEST_F(WithContext, BuildOptionsWithMetacharacters) {

    Done in 922e88e. The new test builds a kernel with -D XM2S(x)=#x and prints XM2S(hello) with printf, asserting the host-side output is hello. This checks that the macro reaches the compiler with its exact value, not just that the build survives. The build-only loop stays for the wider set of metacharacters.

    paoloantinori paoloantinori commented on Oct 09, 2026 10:49 UTC
    Author

    Both points are addressed in 3545fdf.

    The paths need quoting because they inherit the temp directory (config.compiler_temp_dir(), which defaults to std::filesystem::temp_directory_path()), so a TMPDIR containing spaces or shell special characters would otherwise split the command line. The online path is unaffected; every call site quoting a path is in the offline one. Each file is now quoted exactly once. The SPIR-V branch computes the quoted form of each path next to its construction, and the source branch appends the quoted input file at a single point after the .bc/.cl suffix is chosen.

    rjodinchr rjodinchr commented on code on Oct 09, 2026 10:58 UTC
    Code Review
    tests/api/shell_quote.cpp:1
    @@ -0,0 +1,53 @@
    +// Copyright 2026 The clvk authors.
    +//
    +// Licensed under the Apache License, Version 2.0 (the "License");
    +// you may not use this file except in compliance with the License.
    +// You may obtain a copy of the License at
    +//
    +//     http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS,
    +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +// See the License for the specific language governing permissions and
    +// limitations under the License.
    +
    +// Tests that build options containing shell metacharacters are passed to
    +// the compiler verbatim: clBuildProgram must succeed with -D macros whose
    +// values include parentheses, dollar signs, quotes, and single quotes.
    +
    +#include "testcl.hpp"
    +
    +#include <gtest/gtest.h>
    +
    +namespace {
    +
    +const char* source = R"(
    +kernel void test_macro(__global uint* out, uint x) {
    +    out[0] = x;
    +}
    +)";
    +
    +} // namespace
    +
    +TEST_F(WithContext, BuildOptionsWithMetacharacters) {

    It wasn't a new test I was looking for, but rather a modification of the existing one. Verifying that all options provided here are being accurately relayed to the kernel is essential.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 14:08 UTC
    Code Review
    tests/api/shell_quote.cpp:1
    @@ -0,0 +1,53 @@
    +// Copyright 2026 The clvk authors.
    +//
    +// Licensed under the Apache License, Version 2.0 (the "License");
    +// you may not use this file except in compliance with the License.
    +// You may obtain a copy of the License at
    +//
    +//     http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS,
    +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +// See the License for the specific language governing permissions and
    +// limitations under the License.
    +
    +// Tests that build options containing shell metacharacters are passed to
    +// the compiler verbatim: clBuildProgram must succeed with -D macros whose
    +// values include parentheses, dollar signs, quotes, and single quotes.
    +
    +#include "testcl.hpp"
    +
    +#include <gtest/gtest.h>
    +
    +namespace {
    +
    +const char* source = R"(
    +kernel void test_macro(__global uint* out, uint x) {
    +    out[0] = x;
    +}
    +)";
    +
    +} // namespace
    +
    +TEST_F(WithContext, BuildOptionsWithMetacharacters) {

    Done in c4a77ae. The existing test now drives a table of (options, expression, expected) cases: each row builds a kernel whose printf prints the macro, stringized through a STR(x)=#x helper where needed, and asserts the exact relayed value. The separate test I had added is folded into the table as its first two rows.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 15:33 UTC
    Code Review
    src/utils.cpp:1
    @@ -30,6 +31,61 @@
     #include <pthread.h>
     #endif
     
    +std::string shell_quote_token(const std::string& token) {
    +#ifdef WIN32
    +    // cmd.exe treats single quotes as regular characters, so keep the
    +    // historical double quoting behaviour there.
    +    if (token.find("-") == 0) {
    +        return token;
    +    }
    +    return "\"" + token + "\"";
    +#else
    +    // The command line is executed through popen(3), so shell-quote the
    +    // token to hand it to the child process verbatim.
    +    std::string quoted = "'";
    +    for (char c : token) {
    +        if (c == '\'') {
    +            quoted += "'\\''";
    +        } else {
    +            quoted += c;
    +        }
    +    }
    +    quoted += "'";
    +    return quoted;
    +#endif
    +}
    +
    +std::string quote_options_for_shell(const std::string& options) {

    Addressed in c339880 and 44e07a0: the quote-aware tokenizer stayed in program.cpp next to its only caller, and only the platform-specific single-token quoting moved to utils.cpp.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 15:33 UTC
    Code Review
    tests/CMakeLists.txt:1
    @@ -68,6 +68,7 @@ add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/simple-from-il-binary)
     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/simple-from-llvm-ir-binary)
     endif()
     add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sha1)
    +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/utils)

    Done in 44e07a0: the unit test and its CMake target are gone; the coverage lives in tests/api/shell_quote.cpp and runs with api_tests.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 15:33 UTC
    Code Review
    src/program.cpp:1
    @@ -977,6 +977,30 @@ size_t cvk_program::binary_size() const {
         return 0;
     }
     
    +std::string shell_quote_token(const std::string& token) {

    Done in 44e07a0: shell_quote_token, the platform-specific half, now lives in utils.cpp.

    paoloantinori paoloantinori commented on code on Oct 09, 2026 15:33 UTC
    Code Review
    tests/api/shell_quote.cpp:1
    @@ -0,0 +1,55 @@
    +// Copyright 2026 The clvk authors.
    +//
    +// Licensed under the Apache License, Version 2.0 (the "License");
    +// you may not use this file except in compliance with the License.
    +// You may obtain a copy of the License at
    +//
    +//     http://www.apache.org/licenses/LICENSE-2.0
    +//
    +// Unless required by applicable law or agreed to in writing, software
    +// distributed under the License is distributed on an "AS IS" BASIS,
    +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +// See the License for the specific language governing permissions and
    +// limitations under the License.
    +
    +// Exercises the shell-quoting of compiler option tokens in
    +// prepare_build_options (program.cpp); declared here because it is a
    +// file-static helper there, made visible for testing.
    +std::string shell_quote_token(const std::string& token);
    +
    +#include <gtest/gtest.h>
    +
    +#ifndef WIN32

    Done in 44e07a0: the test drives the OpenCL API (clBuildProgram with the options) and, since 3cc5035, also checks each relayed macro value through a kernel printf.

    rjodinchr rjodinchr commented on Oct 10, 2026 15:10 UTC
    Reviewer

    I'm not sure why the functions include "shell" in their names, but apart from that, LGTM.

    paoloantinori paoloantinori commented on Oct 10, 2026 15:44 UTC
    Author

    The shell in the names is the interpreter on the other side. cvk_exec runs the command line through popen(3), so the quoting rules that make the tokens arrive verbatim are the POSIX shell's (single quotes, embedded quotes escaped as '\''). The Windows branch keeps the previous double-quoting behaviour for cmd.exe. Happy to drop shell from the names if you prefer them shorter.

    rjodinchr rjodinchr commented on code on Oct 12, 2026 14:37 UTC
    Code Review
    src/program.cpp:1
    @@ -977,6 +977,34 @@ size_t cvk_program::binary_size() const {
         return 0;
     }
     
    +static std::string quote_options_for_shell(const std::string& options) {
    +    std::vector<std::string> tokens;
    +    std::string token;
    +    bool in_quotes = false;
    +    for (char c : options) {
    +        if (c == '"') {
    +            in_quotes = !in_quotes;
    +        } else if (c == ' ' && !in_quotes) {
    +            if (!token.empty()) {
    +                tokens.push_back(token);
    +            }
    +            token.clear();
    +        } else {
    +            token += c;
    +        }
    +    }
    +    if (!token.empty()) {
    +        tokens.push_back(token);
    +    }
    +
    +    std::string quoted;
    +    for (const auto& t : tokens) {
    +        quoted += shell_quote_token(t);
    +        quoted += " ";
    +    }
    +    return quoted;

        std::string quoted;
        std::string token;
        bool in_quotes = false;
        auto flush_token = [&]() {
            if (!token.empty()) {
                quoted += shell_quote_token(token);
                quoted += ' ';
                token.clear();
            }
        };
        for (char c : options) {
            if (c == '"') {
                in_quotes = !in_quotes;
            } else if (c == ' ' && !in_quotes) {
                flush_token();
            } else {
                token += c;
            }
        }
        flush_token();
        return quoted;
    

    rjodinchr rjodinchr commented on code on Oct 12, 2026 14:38 UTC
    Code Review
    src/utils.cpp:1
    @@ -30,6 +30,30 @@
     #include <pthread.h>
     #endif
     
    +std::string shell_quote_token(const std::string& token) {
    +#ifdef WIN32
    +    // cmd.exe treats single quotes as regular characters, so keep the
    +    // historical double quoting behaviour there.
    +    if (token.find("-") == 0) {
    +        return token;
    +    }
    +    return "\"" + token + "\"";
    +#else
    +    // The command line is executed through popen(3), so shell-quote the
    +    // token to hand it to the child process verbatim.
    +    std::string quoted = "'";
    +    for (char c : token) {
    +        if (c == '\'') {
    +            quoted += "'\\''";
    +        } else {
    +            quoted += c;
    +        }
    +    }
    +    quoted += "'";
    +    return quoted;
    +#endif

    #ifdef WIN32
        if (!token.empty() && token.front() == '-') {
            return std::string(token);
        }
        return "\"" + std::string(token) + "\"";
    #else
        std::string quoted;
        quoted.reserve(token.size() + 2);
        quoted += '\'';
        size_t pos = 0;
        for (size_t next; (next = token.find('\'', pos)) != std::string_view::npos;
             pos = next + 1) {
            quoted.append(token, pos, next - pos);
            quoted += "'\\''";
        }
        quoted.append(token, pos, token.size() - pos);
        quoted += '\'';
        return quoted;
    #endif
    

    rjodinchr rjodinchr commented on Oct 12, 2026 14:39 UTC
    Reviewer

    I've asked an AI to propose more optimised implementations for shell_quote_token & quote_options_for_shell. Here is what it came with.

    paoloantinori paoloantinori commented on code on Oct 12, 2026 18:01 UTC
    Code Review
    src/program.cpp:1
    @@ -977,6 +977,34 @@ size_t cvk_program::binary_size() const {
         return 0;
     }
     
    +static std::string quote_options_for_shell(const std::string& options) {
    +    std::vector<std::string> tokens;
    +    std::string token;
    +    bool in_quotes = false;
    +    for (char c : options) {
    +        if (c == '"') {
    +            in_quotes = !in_quotes;
    +        } else if (c == ' ' && !in_quotes) {
    +            if (!token.empty()) {
    +                tokens.push_back(token);
    +            }
    +            token.clear();
    +        } else {
    +            token += c;
    +        }
    +    }
    +    if (!token.empty()) {
    +        tokens.push_back(token);
    +    }
    +
    +    std::string quoted;
    +    for (const auto& t : tokens) {
    +        quoted += shell_quote_token(t);
    +        quoted += " ";
    +    }
    +    return quoted;

    Applied in 9469eb0, thanks. The single-pass tokenizer is indeed cleaner than the collect-then-quote two-phase version.

    paoloantinori paoloantinori commented on code on Oct 12, 2026 18:01 UTC
    Code Review
    src/utils.cpp:1
    @@ -30,6 +30,30 @@
     #include <pthread.h>
     #endif
     
    +std::string shell_quote_token(const std::string& token) {
    +#ifdef WIN32
    +    // cmd.exe treats single quotes as regular characters, so keep the
    +    // historical double quoting behaviour there.
    +    if (token.find("-") == 0) {
    +        return token;
    +    }
    +    return "\"" + token + "\"";
    +#else
    +    // The command line is executed through popen(3), so shell-quote the
    +    // token to hand it to the child process verbatim.
    +    std::string quoted = "'";
    +    for (char c : token) {
    +        if (c == '\'') {
    +            quoted += "'\\''";
    +        } else {
    +            quoted += c;
    +        }
    +    }
    +    quoted += "'";
    +    return quoted;
    +#endif

    Applied in 9469eb0 verbatim, with <string_view> added to the includes in utils.cpp for the npos reference.

    Reviewers
    Assignees
    paoloantinori paoloantinori
    Labels
    risk: low confidence: 90.9% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 62.8% (MEDIUM) · Higher = larger blast radius
    62.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.9% · Lower = rushed or shallow review
    90.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 5.7% · Safe to merge
    5.7%
    Repository