Fixes ld fork bomb

Fixes https://github.com/teaxyz/cli/issues/610
This commit is contained in:
Max Howell 2023-06-12 14:49:44 -04:00
parent a36c657dad
commit 59ae625a47
No known key found for this signature in database
GPG key ID: 741BB84EF5BB9EEC
3 changed files with 43 additions and 12 deletions

View file

@ -16,10 +16,23 @@ args = ARGV.map do |arg|
arg unless arg == "-Wl,-rpath,#$tea_prefix" or arg == "-nodefaultrpaths" arg unless arg == "-Wl,-rpath,#$tea_prefix" or arg == "-nodefaultrpaths"
end.compact end.compact
def is_tea? path
path = File.realpath path while File.symlink? path
return File.basename(path) == "tea"
end
# find next example of ourselves # find next example of ourselves
# this will either pick the Apple provided clang or the tea one # this will either pick the Apple provided clang or the tea one
exe_path = ENV['PATH'].split(":").filter { |path| exe_path = ENV['PATH'].split(":").filter { |path|
path != File.dirname(__FILE__) && path != File.join($tea_prefix, ".local/bin") if path == File.dirname(__FILE__)
false
elsif path == File.join($tea_prefix, ".local/bin")
false
elsif is_tea?(path)
false
else
true
end
}.map { |path| }.map { |path|
"#{path}/#{exe}" "#{path}/#{exe}"
}.reject { |path| }.reject { |path|

View file

@ -1,4 +1,6 @@
#!/bin/sh #!/bin/bash
set -e
if test -z "$TEA_PREFIX" if test -z "$TEA_PREFIX"
then then
@ -6,22 +8,37 @@ then
exit 1 exit 1
fi fi
dir=$(dirname "$0") DIR=$(dirname "$0")
exe=$(basename "$0") IFS=: read -r -a PATHS <<< "$PATH"
# Remove us from our path __which() {
PATH=$(echo "$PATH" | tr ":" "\n" | grep -v "$dir" | tr "\n" ":") for dir in "${PATHS[@]}"; do
PATH=$(echo "$PATH" | tr ":" "\n" | grep -v "$TEA_PREFIX/.local/bin" | tr "\n" ":") if test "$dir" == "$DIR"; then
: # no fork bombs thanks
elif [ -L "$dir/$1" ]; then
# no fork bombs thanks
foo="$(realpath "$dir/$1")"
foo="$(basename "$foo")"
if [ "$foo" != "tea" ]; then
exe="$dir/$1"
return 0
fi
elif [ -x "$dir/$1" ]; then
exe="$dir/$1"
return 0
fi
done
return 1
}
# If we can find our specific name, e.g. `lld`, # If we can find our specific name, e.g. `lld`,
# passthrough to that # passthrough to that
if which "$exe" >/dev/null 2>&1; then
exe=$(which "$exe")
# Otherwise, fallback to `ld` # Otherwise, fallback to `ld`
# NB: this might not have the right invocations, sometimes; # NB: this might not have the right invocations, sometimes;
# (invoking `ld` as `lld64.ld`) watch for those potential cases # (invoking `ld` as `lld64.ld`) watch for those potential cases
elif which ld >/dev/null 2>&1; then if __which $(basename "$0") || __which ld; then
exe=$(which ld) :
else else
echo 'ld not found in PATH' >&2 echo 'ld not found in PATH' >&2
exit 127 exit 127
@ -35,4 +52,5 @@ for word in "$@"; do
exec "$exe" "$@" exec "$exe" "$@"
fi fi
done done
exec "$exe" "$@" -rpath "$TEA_PREFIX" exec "$exe" "$@" -rpath "$TEA_PREFIX"

View file

@ -3,7 +3,7 @@ distributable: ~
# FIXME we want the c version eg. c99 # FIXME we want the c version eg. c99
# or should that be some kind of option? so you specify you want a cc that support c99 # or should that be some kind of option? so you specify you want a cc that support c99
versions: versions:
- 0.1.5 - 0.1.6
dependencies: dependencies:
linux: linux: