Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListById function panicked when no rules in target chain #130

Open
Aya0wind opened this issue Dec 6, 2024 · 0 comments
Open

ListById function panicked when no rules in target chain #130

Aya0wind opened this issue Dec 6, 2024 · 0 comments

Comments

@Aya0wind
Copy link

Aya0wind commented Dec 6, 2024

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant