libnetwork/iptables: IPTable.exists(): return early on error

Also remove a redundant string cast for the Table value.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-05 15:26:23 +02:00
parent 829374337f
commit 04e54c6bb0
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -450,21 +450,21 @@ func (iptable IPTable) ExistsNative(table Table, chain string, rule ...string) b
}
func (iptable IPTable) exists(native bool, table Table, chain string, rule ...string) bool {
if err := initCheck(); err != nil {
// The exists() signature does not allow us to return an error, but at least
// we can skip the (likely invalid) exec invocation.
return false
}
f := iptable.Raw
if native {
f = iptable.raw
}
if string(table) == "" {
if table == "" {
table = Filter
}
if err := initCheck(); err != nil {
// The exists() signature does not allow us to return an error, but at least
// we can skip the (likely invalid) exec invocation.
return false
}
// if exit status is 0 then return true, the rule exists
_, err := f(append([]string{"-t", string(table), "-C", chain}, rule...)...)
return err == nil