~ubuntu-branches/ubuntu/trusty/bash/trusty-security

« back to all changes in this revision

Viewing changes to tests/printf1.sub

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-03-03 22:52:05 UTC
  • mfrom: (1.3.5) (2.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20140303225205-87ltrt5kspeq0g1b
Tags: 4.3-1ubuntu1
* Merge with Debian; remaining changes:
  - skel.bashrc:
    - Run lesspipe.
    - Enable ls aliases.
    - Set options in ll alias to -alF.
    - Define an alert alias.
    - Enabled colored grep aliases.
  - etc.bash.bashrc:
    - Add sudo hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
LC_ALL=C
 
2
LC_NUMERIC=C
 
3
 
 
4
unset vv
 
5
 
 
6
# this should expand escape sequences in the format string, nothing else
 
7
printf -v vv "\tone\n"
 
8
printf "%s"  "$vv"
 
9
 
 
10
# this should not cut off output after the \c
 
11
printf -v vv "one\ctwo\n"
 
12
printf "%s"  "$vv"
 
13
 
 
14
# and unrecognized backslash escapes should have the backslash preserverd
 
15
printf -v vv "4\.2\n"
 
16
printf "%s"  "$vv"
 
17
 
 
18
printf -v vv "no newline " ; printf "%s" "$vv" ; printf -v vv "now newline\n"
 
19
printf "%s"  "$vv"
 
20
 
 
21
# %% -> %
 
22
printf -v vv "%%\n"
 
23
printf "%s"  "$vv"
 
24
 
 
25
# this was a bug caused by pre-processing the string for backslash escapes
 
26
# before doing the `%' format processing -- all versions before bash-2.04
 
27
printf -v vv "\045"
 
28
printf "%s"  "$vv"
 
29
echo
 
30
printf -v vv "\045d\n"
 
31
printf "%s"  "$vv"
 
32
 
 
33
# simple character output
 
34
printf -v vv "%c\n" ABCD
 
35
printf "%s"  "$vv"
 
36
 
 
37
# test simple string output
 
38
printf -v vv "%s\n" unquoted
 
39
printf "%s"  "$vv"
 
40
 
 
41
# test quoted string output
 
42
printf -v vv "%s %q\n" unquoted quoted
 
43
printf "%s"  "$vv"
 
44
printf -v vv "%s%10q\n" unquoted quoted
 
45
printf "%s"  "$vv"
 
46
 
 
47
printf -v vv "%q\n" 'this&that'
 
48
printf "%s"  "$vv"
 
49
 
 
50
# make sure the format string is reused to use up arguments
 
51
printf -v vv "%d " 1 2 3 4 5
 
52
printf "%s"  "$vv"
 
53
echo
 
54
 
 
55
# make sure that extra format characters get null arguments
 
56
printf -v vv "%s %d %d %d\n" onestring
 
57
printf "%s"  "$vv"
 
58
 
 
59
printf -v vv "%s %d %u %4.2f\n" onestring
 
60
printf "%s"  "$vv"
 
61
 
 
62
printf -v vv -- "--%s %s--\n" 4.2 ''
 
63
printf "%s"  "$vv"
 
64
printf -v vv -- "--%s %s--\n" 4.2
 
65
printf "%s"  "$vv"
 
66
 
 
67
# test %b escapes
 
68
 
 
69
# 8 is a non-octal digit, so the `81' should be output
 
70
#printf -v vv -- "--%b--\n" '\n\081'
 
71
#printf "%s"  "$vv"
 
72
 
 
73
printf -v vv -- "--%b--\n" '\t\0101'
 
74
printf "%s"  "$vv"
 
75
printf -v vv -- "--%b--\n" '\t\101'
 
76
printf "%s"  "$vv"
 
77
 
 
78
# these should all display `A7'
 
79
printf -v vv "%b\n" '\01017'
 
80
printf "%s"  "$vv"
 
81
printf -v vv "%b\n" '\1017'
 
82
printf "%s"  "$vv"
 
83
printf -v vv "%b\n" '\x417'
 
84
printf "%s"  "$vv"
 
85
 
 
86
printf -v vv -- "--%b--\n" '\"abcd\"'
 
87
printf "%s"  "$vv"
 
88
printf -v vv -- "--%b--\n" "\'abcd\'"
 
89
printf "%s"  "$vv"
 
90
 
 
91
printf -v vv -- "--%b--\n" 'a\\x'
 
92
printf "%s"  "$vv"
 
93
 
 
94
printf -v vv -- "--%b--\n" '\x'
 
95
printf "%s"  "$vv"
 
96
 
 
97
Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
 
98
Z2=$'\a\b\e\f\r\v'
 
99
 
 
100
if [ "$Z1" != "$Z2" ]; then
 
101
        printf "%s"  "whoops: printf -v vv %b and $'' differ" >&2
 
102
fi
 
103
unset Z1 Z2
 
104
 
 
105
printf -v vv -- "--%b--\n" ''
 
106
printf "%s"  "$vv"
 
107
printf -v vv -- "--%b--\n"
 
108
printf "%s"  "$vv"
 
109
 
 
110
# the stuff following the \c should be ignored, as well as the rest
 
111
# of the format string
 
112
printf -v vv -- "--%b--\n" '4.2\c5.4\n'
 
113
printf "%s"  "$vv"
 
114
echo
 
115
 
 
116
# unrecognized escape sequences should by displayed unchanged
 
117
printf -v vv -- "--%b--\n" '4\.2'
 
118
printf "%s"  "$vv"
 
119
 
 
120
# a bare \ should not be processed as an escape sequence
 
121
printf -v vv -- "--%b--\n" '\'
 
122
printf "%s"  "$vv"
 
123
 
 
124
# make sure extra arguments are ignored if the format string doesn't
 
125
# actually use them
 
126
printf -v vv "\n" 4.4 BSD
 
127
printf "%s"  "$vv"
 
128
printf -v vv " " 4.4 BSD
 
129
printf "%s"  "$vv"
 
130
echo
 
131
 
 
132
# make sure that a fieldwidth and precision of `*' are handled right
 
133
printf -v vv "%10.8s\n" 4.4BSD
 
134
printf "%s"  "$vv"
 
135
printf -v vv "%*.*s\n" 10 8 4.4BSD
 
136
printf "%s"  "$vv"
 
137
 
 
138
printf -v vv "%10.8q\n" 4.4BSD
 
139
printf "%s"  "$vv"
 
140
printf -v vv "%*.*q\n" 10 8 4.4BSD
 
141
printf "%s"  "$vv"
 
142
 
 
143
printf -v vv "%6b\n" 4.4BSD
 
144
printf "%s"  "$vv"
 
145
printf -v vv "%*b\n" 6 4.4BSD
 
146
printf "%s"  "$vv"
 
147
 
 
148
# we handle this crap with homemade code in printf -v vv.def
 
149
printf -v vv "%10b\n" 4.4BSD
 
150
printf "%s"  "$vv"
 
151
printf -v vv -- "--%-10b--\n" 4.4BSD
 
152
printf "%s"  "$vv"
 
153
printf -v vv "%4.2b\n" 4.4BSD
 
154
printf "%s"  "$vv"
 
155
printf -v vv "%.3b\n" 4.4BSD
 
156
printf "%s"  "$vv"
 
157
printf -v vv -- "--%-8b--\n" 4.4BSD
 
158
printf "%s"  "$vv"
 
159
 
 
160
# test numeric conversions -- these four lines should printf "%s"  identically
 
161
printf -v vv "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
 
162
printf "%s"  "$vv"
 
163
printf -v vv "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
 
164
printf "%s"  "$vv"
 
165
 
 
166
printf -v vv "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
 
167
printf "%s"  "$vv"
 
168
printf -v vv "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
 
169
printf "%s"  "$vv"
 
170
 
 
171
printf -v vv "%10d\n" 42
 
172
printf "%s"  "$vv"
 
173
printf -v vv "%10d\n" -42
 
174
printf "%s"  "$vv"
 
175
 
 
176
printf -v vv "%*d\n" 10 42
 
177
printf "%s"  "$vv"
 
178
printf -v vv "%*d\n" 10 -42
 
179
printf "%s"  "$vv"
 
180
 
 
181
# test some simple floating point formats
 
182
printf -v vv "%4.2f\n" 4.2
 
183
printf "%s"  "$vv"
 
184
printf -v vv "%#4.2f\n" 4.2
 
185
printf "%s"  "$vv"
 
186
printf -v vv "%#4.1f\n" 4.2
 
187
printf "%s"  "$vv"
 
188
 
 
189
printf -v vv "%*.*f\n" 4 2 4.2
 
190
printf "%s"  "$vv"
 
191
printf -v vv "%#*.*f\n" 4 2 4.2
 
192
printf "%s"  "$vv"
 
193
printf -v vv "%#*.*f\n" 4 1 4.2
 
194
printf "%s"  "$vv"
 
195
 
 
196
printf -v vv "%E\n" 4.2
 
197
printf "%s"  "$vv"
 
198
printf -v vv "%e\n" 4.2
 
199
printf "%s"  "$vv"
 
200
printf -v vv "%6.1E\n" 4.2
 
201
printf "%s"  "$vv"
 
202
printf -v vv "%6.1e\n" 4.2
 
203
printf "%s"  "$vv"
 
204
 
 
205
printf -v vv "%G\n" 4.2
 
206
printf "%s"  "$vv"
 
207
printf -v vv "%g\n" 4.2
 
208
printf "%s"  "$vv"
 
209
printf -v vv "%6.2G\n" 4.2
 
210
printf "%s"  "$vv"
 
211
printf -v vv "%6.2g\n" 4.2
 
212
printf "%s"  "$vv"
 
213
 
 
214
# test some of the more esoteric features of POSIX.1 printf -v vv
 
215
printf -v vv "%d\n" "'string'"
 
216
printf "%s"  "$vv"
 
217
printf -v vv "%d\n" '"string"'
 
218
printf "%s"  "$vv"
 
219
 
 
220
printf -v vv "%#o\n" "'string'"
 
221
printf "%s"  "$vv"
 
222
printf -v vv "%#o\n" '"string"'
 
223
printf "%s"  "$vv"
 
224
 
 
225
printf -v vv "%#x\n" "'string'"
 
226
printf "%s"  "$vv"
 
227
printf -v vv "%#X\n" '"string"'
 
228
printf "%s"  "$vv"
 
229
 
 
230
printf -v vv "%6.2f\n" "'string'"
 
231
printf "%s"  "$vv"
 
232
printf -v vv "%6.2f\n" '"string"'
 
233
printf "%s"  "$vv"
 
234
 
 
235
# output from these two lines had better be the same
 
236
printf -v vv -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
 
237
printf "%s"  "$vv"
 
238
printf -v vv -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
 
239
printf "%s"  "$vv"
 
240
 
 
241
# and these two also
 
242
printf -v vv -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
 
243
printf "%s"  "$vv"
 
244
printf -v vv -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
 
245
printf "%s"  "$vv"
 
246
 
 
247
# tests for translating \' to ' and \\ to \
 
248
# printf -v vv translates \' to ' in the format string...
 
249
printf -v vv "\'abcd\'\n"
 
250
printf "%s"  "$vv"
 
251
 
 
252
# but not when the %b format specification is used
 
253
printf -v vv "%b\n" \\\'abcd\\\'
 
254
printf "%s"  "$vv"
 
255
 
 
256
# but both translate \\ to \
 
257
printf -v vv '\\abcd\\\n'
 
258
printf "%s"  "$vv"
 
259
printf -v vv "%b\n" '\\abcd\\'
 
260
printf "%s"  "$vv"
 
261
 
 
262
# this was reported as a bug in bash-2.03
 
263
# these three lines should all printf "%s"  `26'
 
264
printf -v vv "%d\n" 0x1a
 
265
printf "%s"  "$vv"
 
266
printf -v vv "%d\n" 032
 
267
printf "%s"  "$vv"
 
268
printf -v vv "%d\n" 26
 
269
printf "%s"  "$vv"
 
270
 
 
271
# error messages
 
272
 
 
273
# this should be an overflow, but error messages vary between systems
 
274
# printf -v vv "%lu\n" 4294967296
 
275
 
 
276
# ...but we cannot use this because some systems (SunOS4, for example),
 
277
# happily ignore overflow conditions in strtol(3)
 
278
#printf -v vv "%ld\n" 4294967296
 
279
 
 
280
printf -v vv "%10"
 
281
printf -v vv "ab%Mcd\n"
 
282
 
 
283
# this caused an infinite loop in older versions of printf -v vv
 
284
printf -v vv "%y" 0
 
285
 
 
286
# these should print a warning and `0', according to POSIX.2
 
287
printf -v vv "%d\n" GNU
 
288
printf "%s"  "$vv"
 
289
printf -v vv "%o\n" GNU
 
290
printf "%s"  "$vv"
 
291
 
 
292
# failures in all bash versions through bash-2.05
 
293
printf -v vv "%.0s" foo
 
294
printf "%s"  "$vv"
 
295
printf -v vv "%.*s" 0 foo
 
296
printf "%s"  "$vv"
 
297
 
 
298
printf -v vv '%.0b-%.0s\n' foo bar
 
299
printf "%s"  "$vv"
 
300
printf -v vv '(%*b)(%*s)\n' -4 foo -4 bar
 
301
printf "%s"  "$vv"
 
302
 
 
303
format='%'`printf '%0100384d' 0`'d\n' 
 
304
printf -v vv $format 0
 
305
printf "%s"  "$vv"
 
306
 
 
307
# failures in all bash versions through bash-3.0 - undercounted characters
 
308
unset vv
 
309
printf -v vv "  %s %s %s  \n%n" ab cd ef vvv
 
310
printf "%s" "$vv"
 
311
echo $vvv
 
312
 
 
313
# this doesn't work with printf -v vv(3) on all systems
 
314
#printf -v vv "%'s\n" foo
 
315
 
 
316
# test cases from an austin-group list discussion
 
317
# prints ^G as an extension
 
318
printf -v vv '%b\n' '\7'
 
319
printf "%s"  "$vv"
 
320
 
 
321
# prints ^G
 
322
printf -v vv '%b\n' '\0007'
 
323
printf "%s"  "$vv"
 
324
 
 
325
# prints NUL then 7
 
326
#printf -v vv '\0007\n'
 
327
#printf "%s"  "$vv"
 
328
 
 
329
# prints no more than two hex digits
 
330
printf -v vv '\x07e\n'
 
331
printf "%s"  "$vv"
 
332
 
 
333
# additional backslash escapes
 
334
printf -v vv '\"\?\n'
 
335
printf "%s"  "$vv"