Skip to content

Commit 4c3d6cf

Browse files
authored
Create sanitized response file instead of expanding arguments (#480)
The cc_wrapper.sh scripts inspects response files and sanitizes each line within them. How the logic is structured currently causes all of these arguments to the be directly used later in the script. This can run into argument overflows. It can also run into problems where quoting in response files and from args is treated differently. This addresses some of the comments in #430, namely cleaning up files and not overwriting the input files. I have not tried to apply a threshold here as that is best left up to the tooling that previously decided to use response files, which can cause difficult to debug changes in behavior. This conflicts with #479, but is trying to address the same underlying problem of quoted arguments in response files as the result of golang's link actions. Fixes #421
1 parent a5121b2 commit 4c3d6cf

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

toolchain/cc_wrapper.sh.tpl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929

3030
set -euo pipefail
3131

32+
CLEANUP_FILES=()
33+
34+
function cleanup() {
35+
if [[ ${#CLEANUP_FILES[@]} -gt 0 ]]; then
36+
rm -f "${CLEANUP_FILES[@]}"
37+
fi
38+
}
39+
40+
trap cleanup EXIT
41+
3242
# See note in toolchain/internal/configure.bzl where we define
3343
# `wrapper_bin_prefix` for why this wrapper is needed.
3444

@@ -60,14 +70,18 @@ function sanitize_option() {
6070

6171
cmd=()
6272
for ((i = 0; i <= $#; i++)); do
63-
if [[ ${!i} == @* ]]; then
73+
if [[ ${!i} == @* && -r "${i:1}" ]]; then
74+
# Create a new, sanitized file.
75+
tmpfile=$(mktemp)
76+
CLEANUP_FILES+=("${tmpfile}")
6477
while IFS= read -r opt; do
6578
opt="$(
6679
set -e
6780
sanitize_option "${opt}"
6881
)"
69-
cmd+=("${opt}")
82+
echo "${opt}" >>"${tmpfile}"
7083
done <"${!i:1}"
84+
cmd+=("@${tmpfile}")
7185
else
7286
opt="$(
7387
set -e
@@ -78,4 +92,4 @@ for ((i = 0; i <= $#; i++)); do
7892
done
7993

8094
# Call the C++ compiler.
81-
exec "${cmd[@]}"
95+
"${cmd[@]}"

toolchain/osx_cc_wrapper.sh.tpl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ LIBS=
3535
LIB_DIRS=
3636
RPATHS=
3737
OUTPUT=
38+
CLEANUP_FILES=()
39+
40+
function cleanup() {
41+
if [[ ${#CLEANUP_FILES[@]} -gt 0 ]]; then
42+
rm -f "${CLEANUP_FILES[@]}"
43+
fi
44+
}
45+
46+
trap cleanup EXIT
3847

3948
function parse_option() {
4049
local -r opt="$1"
@@ -87,17 +96,19 @@ function sanitize_option() {
8796
cmd=()
8897
for ((i = 0; i <= $#; i++)); do
8998
if [[ ${!i} == @* && -r "${i:1}" ]]; then
99+
tmpfile=$(mktemp)
100+
CLEANUP_FILES+=("${tmpfile}")
90101
while IFS= read -r opt; do
91102
if [[ ${opt} == "-fuse-ld=ld64.lld" ]]; then
92-
cmd+=("-fuse-ld=lld")
103+
echo "-fuse-ld=lld" >>"${tmpfile}"
93104
fi
94105
opt="$(
95106
set -e
96107
sanitize_option "${opt}"
97108
)"
98-
parse_option "${opt}"
99-
cmd+=("${opt}")
109+
parse_option "${opt}" >>"${tmpfile}"
100110
done <"${!i:1}"
111+
cmd+=("@${tmpfile}")
101112
else
102113
opt="$(
103114
set -e

0 commit comments

Comments
 (0)