Gentoo Archives: gentoo-portage-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-portage-dev@l.g.o, R0b0t1 <r030t1@×××××.com>, Zac Medico <zmedico@g.o>
Subject: Re: [gentoo-portage-dev] Fix all misc. bash errors.
Date: Wed, 07 Feb 2018 05:14:07
Message-Id: beb7578c-8839-6aca-02a8-b8906552a3fc@gentoo.org
In Reply to: Re: [gentoo-portage-dev] Fix all misc. bash errors. by R0b0t1
1 On 02/05/2018 06:33 PM, R0b0t1 wrote:
2 > On Sun, Feb 4, 2018 at 10:45 PM, Zac Medico <zmedico@g.o> wrote:
3 >> On 02/04/2018 07:22 PM, R0b0t1 wrote:
4 >>> diff --git a/bin/isolated-functions.sh b/bin/isolated-functions.sh
5 >>> index b28e44f18..377b32d90 100644
6 >>> --- a/bin/isolated-functions.sh
7 >>> +++ b/bin/isolated-functions.sh
8 >>> @@ -82,7 +82,7 @@ __dump_trace() {
9 >>> lineno=${BASH_LINENO[${n} - 1]}
10 >>> # Display function arguments
11 >>> args=
12 >>> - if [[ -n "${BASH_ARGV[@]}" ]]; then
13 >>> + if [[ -n "${BASH_ARGV[*]}" ]]; then
14 >>
15 >> I feel like the shellcheck authors might be willing to accept [[ -n
16 >> ${BASH_ARGV[@]} ]] or [[ ${BASH_ARGV[@]} ]] as correct, since the
17 >> "Problematic code" that they cite involves an incorrect comparison:
18 >>
19 >> https://github.com/koalaman/shellcheck/wiki/SC2199#problematic-code
20 >>
21 >
22 > This example might be more illustrative:
23 >
24 > argc () {
25 > echo $#
26 > }
27 >
28 > argc "${BASH_ARGV[*]}"
29 > argc "${BASH_ARGV[@]}"
30 >
31 >
32 > Output (./test.sh a b):
33 >
34 > 1
35 > 2
36 >
37 >
38 > Which changes the semantics of the tests in which it is present. It is
39 > hard to know what [[ is doing; if the same is attempted with [ it
40 > would be an error for the same reason that globbing produces errors.
41 > See:
42 >
43 > [ "${BASH_ARGV[*]}" ]
44 > [ "${BASH_ARGV[@]}" ] # Fails; [: b: unary operator expected
45 > [[ "${BASH_ARGV[*]}" ]]
46 > [[ "${BASH_ARGV[@]}" ]]
47 >
48 >
49 > It is possible [[ ignores the extra elements. I can't think of a
50 > reason where this would make the results of the test different. At the
51 > same time, it might give people the wrong impression of the operation
52 > of [.
53 >
54 > In the sense that [ and [[ represent test(1), anything but the "[*]"
55 > expansion is incorrect, as the error message indicates. That [[ treats
56 > its arguments specially because it is a feature of the syntax just
57 > makes the situation more confusing.
58
59 I've turned this into an optimization by testing the array length
60 instead of expanding the elements:
61
62 https://gitweb.gentoo.org/proj/portage.git/commit/?id=2306b8f4a2d781db87ee61707f6dea1c5f717936
63 --
64 Thanks,
65 Zac

Attachments

File name MIME type
signature.asc application/pgp-signature