Use -- some more
authorArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Fri, 3 Jan 2014 12:16:54 +0000 (14:16 +0200)
committerArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Wed, 8 Jan 2014 17:33:27 +0000 (19:33 +0200)
Change-Id: I84101a845a725a71bb626e9369b68e95663354ed
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
README
setup-extlinux-conf
setup-gummiboot-conf
setup-ivi-boot
setup-ivi-bootloader-conf
setup-ivi-fstab
setup-ivi-sh-functions

diff --git a/README b/README
index 6cfc7eb..18764d4 100644 (file)
--- a/README
+++ b/README
@@ -17,5 +17,9 @@ Some coding style notes for the shell scripts
 
 5. All the error and verbose output should go to stderr
 
+6. Use -- to separate options and arguments, this is generally a good defensive
+   programming practice to make sure no one tricks your commands by adding
+   options to what should be arguments. E.g., 'rm $file' can be made 'rm -rf /"
+   if one makes "$file" to be "-rf /" somehow. 'rm -- $file' would catch this.
 --
 Artem Bityutskiy
index 9b67c12..adf5969 100755 (executable)
@@ -17,7 +17,7 @@ fi
 # This is a small trick which I use to make sure my scripts are portable -
 # check if 'dash' is present, and if yes - use it.
 if can_switch_to_dash; then
-       exec dash -euf "$srcdir/$PROG" "$@"
+       exec dash -euf -- "$srcdir/$PROG" "$@"
        exit $?
 fi
 
@@ -39,7 +39,7 @@ create_default_conf_file()
 {
        verbose "creating the default configuration file \"$conf_file\""
 
-       mkdir -p $verbose "$conf_dir" >&2
+       mkdir -p $verbose -- "$conf_dir" >&2
        cat > "$conf_file" <<-EOF
        # Generated by $PROG
        ui vesamenu.c32
@@ -97,7 +97,7 @@ remove_label()
                        /$anyl_regexp/!bl    # a different label, stop skipping
                    }
                    /$l_regexp/!p            # print all other lines
-                   " "$conf_file"
+                   " -- "$conf_file"
 
        remove_trailing_empty_lines "$conf_file"
 }
@@ -194,7 +194,7 @@ add_subcommand()
        # Make sure the extlinux configuration file exists
        check_and_create_default_conf_file "$label"
 
-       if LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
+       if LC_ALL=C grep -q -e "$(label_regexp "$label")" -- "$conf_file" && \
           [ -z "$force" ]; then
                fatal "extlinux boot menu label \"$label\" already exists" \
                      "(use -f to force re-creating it)"
@@ -289,7 +289,7 @@ remove_subcommand()
 
        local label="$1"
 
-       if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" "$conf_file" && \
+       if ! LC_ALL=C grep -q -e "$(label_regexp "$label")" -- "$conf_file" && \
           [ -z "$force" ]; then
                fatal "cannot find label \"$label\" in \"$conf_file\"" \
                      "(use -f to ignore this error)"
@@ -322,7 +322,7 @@ get_kernel_by_label()
                /$linux_regexp/ { s/$linux_regexp/\2/p }
                /$kernel_regexp/ { s/$kernel_regexp/\2/p }
                /$anyl_regexp/!bl # Loop till the next label
-       }" "$conf_file")"
+       }" -- "$conf_file")"
 
        printf "%s" "${result##*/}"
 }
@@ -388,7 +388,8 @@ default_subcommand()
 
        # Find the current default label
        local regexp="$(get_regexp "default")"
-       local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
+       local default_label="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+                               "$conf_file")"
 
        if [ -z "$label" ]; then
                printf "%s\n" "label: $default_label"
@@ -397,7 +398,7 @@ default_subcommand()
        fi
 
        local l_regexp="$(label_regexp "$label")"
-       local labels="$(LC_ALL=C grep -e "$l_regexp" "$conf_file" | wc -l)"
+       local labels="$(LC_ALL=C grep -e "$l_regexp" -- "$conf_file" | wc -l)"
 
        if [ "$labels" -eq "0" ] && [ -z "$force" ]; then
                fatal "cannot find label \"$label\" in \"$conf_file\"" \
@@ -418,14 +419,14 @@ default_subcommand()
                        $                 { s/.*/&\n$def/; q }
                        /^[[:blank:]]*$/  { s/.*/$def\n&/; q }
                        /$(anyl_regexp)/  { s/.*/$def\n&/; q }
-                       " "$conf_file"
+                       " -- "$conf_file"
                return 0
        fi
 
        # Escape special sed characters in "$entry" and replace the old default
        # entry with the new one
        local esc_label="$(esc_sed_replacement "$label")"
-       LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" "$conf_file"
+       LC_ALL=C sed -i -e "s/$regexp/\1$esc_label\3/" -- "$conf_file"
        verbose "set the default boot kernel to \"$label"\"
 }
 
index b344029..4fa7fbe 100755 (executable)
@@ -17,7 +17,7 @@ fi
 # This is a small trick which I use to make sure my scripts are portable -
 # check if 'dash' is present, and if yes - use it.
 if can_switch_to_dash; then
-       exec dash -euf "$srcdir/$PROG" "$@"
+       exec dash -euf -- "$srcdir/$PROG" "$@"
        exit $?
 fi
 
@@ -51,7 +51,7 @@ create_default_conf_file()
 {
        verbose "creating the default configuration file \"$conf_file\""
 
-       mkdir -p $verbose "$conf_dir" >&2
+       mkdir -p $verbose -- "$conf_dir" >&2
        cat > "$conf_file" <<-EOF
        # Generated by $PROG
        timeout 0
@@ -183,7 +183,7 @@ add_subcommand()
                fatal "cannot fetch kernel version from \"$kernel\""
 
        # Create the new entry
-       mkdir -p $verbose "$entries_dir" >&2
+       mkdir -p $verbose -- "$entries_dir" >&2
        cat > "$entry_path" <<-EOF
        # Generated by $PROG
        title $title
@@ -194,7 +194,7 @@ add_subcommand()
 
        if [ -n "$verbose" ]; then
                verbose "contents of \"$entry_path\":"
-               cat "$entry_path" >&2
+               cat -- "$entry_path" >&2
        fi
 }
 
@@ -271,7 +271,7 @@ remove_subcommand()
                      "(use -f to ignore this error)"
        fi
 
-       rm -rf $verbose "$entry_path" >&2
+       rm -rf $verbose -- "$entry_path" >&2
        verbose "removed $entry_path"
 }
 
@@ -289,11 +289,13 @@ get_kernel_from_entry()
 
        if [ -f "$entry" ]; then
                local regexp="$(get_regexp "efi")"
-               local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
+               local result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+                               "$entry")"
 
                if [ -z "$result" ]; then
                        regexp="$(get_regexp "linux")"
-                       result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$entry")"
+                       result="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+                                 "$entry")"
 
                        [ -n "$result" ] || return 0
                fi
@@ -365,7 +367,8 @@ default_subcommand()
 
        # Find the current default entry
        local regexp="$(get_regexp "default")"
-       local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" "$conf_file")"
+       local default_entry="$(LC_ALL=C sed -n -e "s/$regexp/\2/p" -- \
+                               "$conf_file")"
 
        if [ -z "$entry" ]; then
                printf "%s\n" "entry: $default_entry"
@@ -391,7 +394,7 @@ default_subcommand()
        # Escape special sed characters in "$entry" and replace the old default
        # entry with the new one
        local entry_esc="$(esc_sed_replacement "$entry")"
-       LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" "$conf_file"
+       LC_ALL=C sed -i -e "s/$regexp/\1$entry_esc\3/" -- "$conf_file"
 
        verbose "set the default boot kernel to \"$entry"\"
 }
index b020d12..ba167fc 100755 (executable)
@@ -21,7 +21,7 @@ fi
 # This is a small trick which I use to make sure my scripts are portable -
 # check if 'dash' is present, and if yes - use it.
 if can_switch_to_dash; then
-       exec dash -euf "$srcdir/$PROG" "$@"
+       exec dash -euf -- "$srcdir/$PROG" "$@"
        exit $?
 fi
 
@@ -39,7 +39,7 @@ install_gummiboot()
        fi
 
        # Install gummiboot
-       mkdir -p $verbose "$installdir" >&2
+       mkdir -p $verbose -- "$installdir" >&2
        [ -f "$gummiboot_path/gummibootia32.efi" ] && \
                cp $verbose "$gummiboot_path/gummibootia32.efi" \
                            "$installdir/bootia32.efi" >&2
@@ -73,7 +73,7 @@ install_extlinux()
                      "variable is not defined"
 
        # Install extlinux
-       mkdir -p $verbose "$installdir" >&2
+       mkdir -p $verbose -- "$installdir" >&2
        "$extlinux" --device "$boot_devnode" -i "$installdir" || \
                fatal "cannot install extlinux to \"$installdir\"" \
                      "(requires extlinux version 5 or greater)"
@@ -163,7 +163,7 @@ else
 fi
 
 # Create bootloader entries for each kernel
-kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
+kernels="$(ls -1 "$bootdir" | LC_ALL=C grep -- "^vmlinuz-" | sort -r)"
 
 [ -n "$kernels" ] || \
        fatal "no kernels (vmlinuz-*) found in \"$bootdir\""
index 85d42b0..01e689c 100755 (executable)
@@ -21,7 +21,7 @@ fi
 # This is a small trick which I use to make sure my scripts are portable -
 # check if 'dash' is present, and if yes - use it.
 if can_switch_to_dash; then
-       exec dash -euf "$srcdir/$PROG" "$@"
+       exec dash -euf -- "$srcdir/$PROG" "$@"
        exit $?
 fi
 
index dca3ff8..7f1440f 100755 (executable)
@@ -21,7 +21,7 @@ fi
 # This is a small trick which I use to make sure my scripts are portable -
 # check if 'dash' is present, and if yes - use it.
 if can_switch_to_dash; then
-       exec dash -euf "$srcdir/$PROG" "$@"
+       exec dash -euf -- "$srcdir/$PROG" "$@"
        exit $?
 fi
 
index 2a973f3..4831048 100644 (file)
@@ -34,7 +34,8 @@ get_os_name()
                fatal "the \"$osrelease_path\" file not found"
 
        # Get the OS name
-       local __os_name="$(LC_ALL=C sed -n -e 's/^PRETTY_NAME="\(.*\)"$/\1/p' "$osrelease_path")"
+       local __os_name="$(LC_ALL=C sed -n -e 's/^PRETTY_NAME="\(.*\)"$/\1/p' \
+                          -- "$osrelease_path")"
        [ -n "$__os_name" ] || \
                fatal "cannot find \"PRETTY_NAME\" variable in \"$osrelease_path\""
 
@@ -92,12 +93,14 @@ get_newest_kernel()
        local bootdir="$1"; shift
 
        # Generate the list of installed kernels
-       local kernels="$(ls -1 "$bootdir" | LC_ALL=C grep "^vmlinuz-" | sort -r)"
+       local kernels="$(ls -1 "$bootdir" | LC_ALL=C grep -- "^vmlinuz-" | \
+                        sort -r)"
 
        # Exclude the unwanted kernel, if any
        if [ -n "${1:-}" ]; then
                local kernel="$(esc_regexp "$1")"
-               kernels="$(printf "%s" "$kernels" | LC_ALL=C grep -v "^$kernel$")"
+               kernels="$(printf "%s" "$kernels" | LC_ALL=C grep -v -- \
+                          "^$kernel$")"
        fi
 
        printf "%s" "$kernels" | head -n1
@@ -118,5 +121,5 @@ remove_trailing_empty_lines()
                 bl              # and start over
            }
            /^[[:blank:]]*$/!p   # print the pattern buffer for non-blank lines
-           ' "$file"
+           ' -- "$file"
 }