Specify strstr tightly, add strglob/strglobin.

By convention, strstr should be a literal string match. Previously, it
would match as a glob pattern. Some code used that, so add new
functions strglob and strglobin to do what that code expects, and
specify them tightly too. strglob tests whether the glob pattern
matches the entire string (the name strglob is also used in the yorick
language, and that's what it does there), while strglobin tests whether
the glob pattern matches anywhere in the string.

Also tightens str_starts, str_ends, and str_replace to deal with
literal strings only. In a quick grep I did not find code that depended
on these functions matching globs.

Changes the call sites where strstr was used with glob patterns to use
strglobin or strglob as the intention seemed to be (or, in one case,
strstr with the * removed as it did not affect the result anyway).
master
Chapman Flack 2014-04-04 21:11:38 -04:00 committed by Harald Hoyer
parent 2e094b20a6
commit 2c19a5fa78
20 changed files with 51 additions and 32 deletions

View File

@ -33,7 +33,11 @@ if [[ $initdir ]] && ! [[ -d $initdir ]]; then
fi

# Generic substring function. If $2 is in $1, return 0.
strstr() { [[ $1 = *$2* ]]; }
strstr() { [[ $1 = *"$2"* ]]; }
# Generic glob matching function. If glob pattern $2 matches anywhere in $1, OK
strglobin() { [[ $1 = *$2* ]]; }
# Generic glob matching function. If glob pattern $2 matches all of $1, OK
strglob() { [[ $1 = $2 ]]; }

# helper function for check() in module-setup.sh
# to check for required installed binaries

View File

@ -130,12 +130,12 @@ do_ipv6auto() {

# Handle static ip configuration
do_static() {
strstr $ip '*:*:*' && load_ipv6
strglobin $ip '*:*:*' && load_ipv6

linkup $netif
[ -n "$macaddr" ] && ip link set address $macaddr dev $netif
[ -n "$mtu" ] && ip link set mtu $mtu dev $netif
if strstr $ip '*:*:*'; then
if strglobin $ip '*:*:*'; then
# note no ip addr flush for ipv6
ip addr add $ip/$mask ${srv:+peer $srv} dev $netif
wait_for_ipv6_dad $netif

View File

@ -377,7 +377,7 @@ ip_to_var() {
# ip=<ipv4-address> means anaconda-style static config argument cluster:
# ip=<ip> gateway=<gw> netmask=<nm> hostname=<host> mtu=<mtu>
# ksdevice={link|bootif|ibft|<MAC>|<ifname>}
if strstr "$autoconf" "*.*.*.*"; then
if strglob "$autoconf" "*.*.*.*"; then
ip="$autoconf"
gw=$(getarg gateway=)
mask=$(getarg netmask=)

View File

@ -85,7 +85,7 @@ for netup in /tmp/net.*.did-setup ; do

netif=${netup%%.did-setup}
netif=${netif##*/net.}
strstr "$netif" ":*:*:*:*:" && continue
strglobin "$netif" ":*:*:*:*:" && continue
[ -e /tmp/ifcfg/ifcfg-$netif ] && continue
unset bridge
unset bond
@ -132,7 +132,7 @@ for netup in /tmp/net.*.did-setup ; do
else
# If we've booted with static ip= lines, the override file is there
[ -e /tmp/net.$netif.override ] && . /tmp/net.$netif.override
if strstr "$ip" '*:*:*'; then
if strglobin "$ip" '*:*:*'; then
echo "IPV6INIT=yes"
echo "IPV6_AUTOCONF=no"
echo "IPV6ADDR=\"$ip/$mask\""
@ -149,7 +149,7 @@ for netup in /tmp/net.*.did-setup ; do
fi
fi
fi
if strstr "$gw" '*:*:*'; then
if strglobin "$gw" '*:*:*'; then
echo "IPV6_DEFAULTGW=\"$gw\""
elif [ -n "$gw" ]; then
echo "GATEWAY=\"$gw\""

View File

@ -26,7 +26,7 @@ function cms_write_config()

IFCFGFILE=/run/initramfs/state/etc/sysconfig/network-scripts/ifcfg-$DEVICE

strstr "$IPADDR" '*:*:*' && ipv6=1
strglobin "$IPADDR" '*:*:*' && ipv6=1

# to please NetworkManager on startup in loader before loader reconfigures net
cat > /etc/sysconfig/network << EOF

View File

@ -8,7 +8,7 @@ DEVICE=$1

. /tmp/cms.conf

strstr "$IPADDR" '*:*:*' && ipv6=1
strglobin "$IPADDR" '*:*:*' && ipv6=1

if [ "$ipv6" ] && ! str_starts "$IPADDR" "["; then
IPADDR="[$IPADDR]"

View File

@ -40,7 +40,7 @@ nfsroot_to_var() {
arg="${arg##$nfs:}"

# check if we have a server
if strstr "$arg" ':/*' ; then
if strstr "$arg" ':/' ; then
server="${arg%%:/*}"
arg="/${arg##*:/}"
fi

View File

@ -20,19 +20,33 @@ debug_on() {
[ "$RD_DEBUG" = "yes" ] && set -x
}

# returns OK if $1 contains $2
# returns OK if $1 contains literal string $2 (and isn't empty)
strstr() {
[ "${1#*$2*}" != "$1" ]
[ "${1##*"$2"*}" != "$1" ]
}

# returns OK if $1 contains $2 at the beginning
# returns OK if $1 matches (completely) glob pattern $2
# An empty $1 will not be considered matched, even if $2 is * which technically
# matches; as it would match anything, it's not an interesting case.
strglob() {
[ -n "$1" -a -z "${1##$2}" ]
}

# returns OK if $1 contains (anywhere) a match of glob pattern $2
# An empty $1 will not be considered matched, even if $2 is * which technically
# matches; as it would match anything, it's not an interesting case.
strglobin() {
[ -n "$1" -a -z "${1##*$2*}" ]
}

# returns OK if $1 contains literal string $2 at the beginning, and isn't empty
str_starts() {
[ "${1#$2*}" != "$1" ]
[ "${1#"$2"*}" != "$1" ]
}

# returns OK if $1 contains $2 at the end
# returns OK if $1 contains literal string $2 at the end, and isn't empty
str_ends() {
[ "${1%*$2}" != "$1" ]
[ "${1%*"$2"}" != "$1" ]
}

if [ -z "$DRACUT_SYSTEMD" ]; then
@ -85,9 +99,9 @@ str_replace() {
local out=''

while strstr "${in}" "$s"; do
chop="${in%%$s*}"
chop="${in%%"$s"*}"
out="${out}${chop}$r"
in="${in#*$s}"
in="${in#*"$s"}"
done
echo "${out}${in}"
}
@ -555,7 +569,7 @@ nfsroot_to_var() {
arg="${arg##$nfs:}"

# check if we have a server
if strstr "$arg" ':/*' ; then
if strstr "$arg" ':/' ; then
server="${arg%%:/*}"
arg="/${arg##*:/}"
fi

View File

@ -1,7 +1,7 @@
#!/bin/sh
>/dev/watchdog
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec </dev/console >/dev/console 2>&1

View File

@ -1,7 +1,7 @@
#!/bin/sh
>/dev/watchdog
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec </dev/console >/dev/console 2>&1

View File

@ -1,7 +1,7 @@
#!/bin/sh
>/dev/watchdog
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec </dev/console >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
command -v plymouth >/dev/null && plymouth --quit
exec >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
command -v plymouth >/dev/null && plymouth --quit
exec >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec >/dev/console 2>&1

View File

@ -1,6 +1,6 @@
#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
plymouth --quit
exec >/dev/console 2>&1

View File

@ -4,7 +4,7 @@ exec >/dev/console 2>&1
export TERM=linux
export PS1='initramfs-test:\w\$ '
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }

stty sane
strstr "$CMDLINE" "rd.shell" && sh -i

View File

@ -2,14 +2,15 @@
exec >/dev/console 2>&1
set -x
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
strstr() { [ "${1#*$2*}" != "$1" ]; }
strstr() { [ "${1#*"$2"*}" != "$1" ]; }
strglobin() { [ -n "$1" -a -z "${1#*$2*}" ]; }
CMDLINE=$(while read line; do echo $line;done < /proc/cmdline)
export TERM=linux
export PS1='initramfs-test:\w\$ '
stty sane
echo "made it to the rootfs! Powering down."
for i in /run/initramfs/net.*.did-setup; do
strstr "$i" ":*:*:*:*:" && continue
strglobin "$i" ":*:*:*:*:" && continue
i=${i%.did-setup}
IFACES+="${i##*/net.} "
done