Skip to content

ListById function panicked when no rules in target chain #130

@Aya0wind

Description

@Aya0wind

Version

v0.8.0

Problem

Current implementation does not check the size of ipt.executeList(args) 's return value, when you try to list an empty chain, this code will cause a index out of range panic.

// List rules in specified table/chain
func (ipt *IPTables) ListById(table, chain string, id int) (string, error) {
	args := []string{"-t", table, "-S", chain, strconv.Itoa(id)}
	rule, err := ipt.executeList(args)
	if err != nil {
		return "", err
	}
	return rule[0], nil
}

Suggestion

Return error when chain is empty

// List rules in specified table/chain
func (ipt *IPTables) ListById(table, chain string, id int) (string, error) {
	args := []string{"-t", table, "-S", chain, strconv.Itoa(id)}
	rule, err := ipt.executeList(args)
	if err != nil {
		return "", err
	}
	if len(rule) == 0 {
		return "", fmt.Errorf("chain %s in table %s is empty", chain, table)
	}
	return rule[0], err
}

Or just return an empty string?

// List rules in specified table/chain
func (ipt *IPTables) ListById(table, chain string, id int) (string, error) {
	args := []string{"-t", table, "-S", chain, strconv.Itoa(id)}
	rule, err := ipt.executeList(args)
	if err != nil {
		return "", err
	}
	if len(rule) == 0 {
		return "", nil
	}
	return rule[0], err
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions