]> git.proxmox.com Git - mirror_iproute2.git/commit
utils: don't match empty strings as prefixes
authorMatteo Croce <mcroce@redhat.com>
Mon, 15 Jul 2019 18:04:30 +0000 (20:04 +0200)
committerStephen Hemminger <stephen@networkplumber.org>
Mon, 15 Jul 2019 20:48:48 +0000 (13:48 -0700)
commit1f420318bda3cc62156e89e1b56d60cc744b48ad
treed26cfba55bb9dca5639417deee0c3add7b49eb02
parent6bc13e4a20f50e9c37d5a504c78222913c433fd3
utils: don't match empty strings as prefixes

iproute has an utility function which checks if a string is a prefix for
another one, to allow use of abbreviated commands, e.g. 'addr' or 'a'
instead of 'address'.

This routine unfortunately considers an empty string as prefix
of any pattern, leading to undefined behaviour when an empty
argument is passed to ip:

    # ip ''
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
        inet6 ::1/128 scope host
           valid_lft forever preferred_lft forever

    # tc ''
    qdisc noqueue 0: dev lo root refcnt 2

    # ip address add 192.0.2.0/24 '' 198.51.100.1 dev dummy0
    # ip addr show dev dummy0
    6: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
        link/ether 02:9d:5e:e9:3f:c0 brd ff:ff:ff:ff:ff:ff
        inet 192.0.2.0/24 brd 198.51.100.1 scope global dummy0
           valid_lft forever preferred_lft forever

Rewrite matches() so it takes care of an empty input, and doesn't
scan the input strings three times: the actual implementation
does 2 strlen and a memcpy to accomplish the same task.

Signed-off-by: Matteo Croce <mcroce@redhat.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
include/utils.h
lib/utils.c