1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
# Copyright 2011-2025 Free Software Foundation, Inc.
#
# Copying and distribution of this file, with or without modification,
# in any medium, are permitted without royalty provided the copyright
# notice and this notice are preserved.
. $srcdir/test-lib.sh
use_local_patch
use_tmpdir
# ================================================================
emit_patch()
{
cat <<EOF
--- /dev/null
+++ $1
@@ -0,0 +1 @@
+x
EOF
}
# Ensure that patch rejects an output file name that is absolute
# or that contains a ".." component.
check 'emit_patch ../z | patch -f -p1 --dry-run || echo status: $?' <<EOF
checking file z
EOF
check 'emit_patch /absolute/path | patch -f -p0 --dry-run || echo status: $?' <<EOF
Ignoring potentially dangerous file name /absolute/path
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- /dev/null
|+++ /absolute/path
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
status: 1
EOF
for dir in "$TMPDIR" "$TMP" "$TEMP" "/tmp"; do
if test -n "$dir"; then
filename=$dir/patch-$$-$RANDOM
break
fi
done
check 'emit_patch $filename | (cd / && patch -f -p0 --dry-run) || echo status: $?' <<EOF
checking file $filename
EOF
check 'emit_patch a/../z | patch -f -p0 --dry-run || echo status: $?' <<EOF
Ignoring potentially dangerous file name a/../z
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- /dev/null
|+++ a/../z
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
status: 1
EOF
check 'emit_patch a/../z | patch -f -p1 --dry-run || echo status: $?' <<EOF
Ignoring potentially dangerous file name ../z
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- /dev/null
|+++ a/../z
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
status: 1
EOF
check 'emit_patch ../z | patch -f -p0 --dry-run || echo status: $?' <<EOF
Ignoring potentially dangerous file name ../z
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- /dev/null
|+++ ../z
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
status: 1
EOF
cat > d.diff <<EOF
--- f
+++ ../g
@@ -1 +1 @@
-1
+1
EOF
check 'patch -f -p0 --dry-run < d.diff || echo status: $?' <<EOF
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- f
|+++ ../g
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
status: 1
EOF
echo 1 > f
check 'patch -f -p0 --dry-run < d.diff || echo status: $?' <<EOF
checking file f
EOF
echo 1 > g
check 'patch -f -p1 --dry-run < d.diff || echo status: $?' <<EOF
checking file g
EOF
fixup='s/ file ab\.[^ ]*/ file ab.*/'
check_ignoring_temp_file_name() {
pat=\'$1\'
unset arg1; eval ${2+"arg1=\\''$2'\\'"}
unset arg2; eval ${3+"arg2=\\''$3'\\'"}
check '
emit_patch '"$pat"' | patch '"$arg1"' '"$arg2"' >patch.out 2>&1
status=$?
sed "$fixup" patch.out
echo status: $status
'
}
nl='
'
nlname="a${nl}b"
quoted_nlname='"a\nb"'
check_ignoring_temp_file_name "$quoted_nlname" <<EOF
patching file 'a
b'
$PATCH: **** Can't rename file ab.* to 'a
b' : Invalid byte sequence
status: 2
EOF
check_ignoring_temp_file_name foo "$nlname" <<EOF
patching file 'a
b'
$PATCH: **** Can't rename file ab.* to 'a
b' : Invalid byte sequence
status: 2
EOF
check_ignoring_temp_file_name foo -o "$nlname" <<EOF
$PATCH: **** Can't create file 'a
b' : Invalid byte sequence
status: 2
EOF
emit_patch_with_NUL()
{
printf '%s\n' '--- /dev/null'
printf '+++ a\0b\n'
printf '%s\n' '@@ -0,0 +1 @@' '+x'
}
check 'emit_patch_with_NUL | patch; echo status: $?' <<EOF
$PATCH: **** patch line 2 contains NUL byte
status: 2
EOF
mkdir d
cd d
cat > d.diff <<EOF
--- ../h
+++ ../h
@@ -0,0 +1 @@
+x
EOF
touch ../h
check 'patch -f -p0 < d.diff || echo status: $?' <<EOF
Ignoring potentially dangerous file name ../h
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- ../h
|+++ ../h
--------------------------
No file to patch. Skipping patch.
1 out of 1 hunk ignored
status: 1
EOF
# Empty filenames are not allowed:
check 'emit_patch f | patch -r- "" || echo status: $?' <<EOF
File '' is not a regular file -- refusing to patch
1 out of 1 hunk ignored
status: 1
EOF
|