Skip to content

Commit

Permalink
Minor refactor of constructor for comparison operator.
Browse files Browse the repository at this point in the history
Justification: Since the comparison operator is only valid for bytesData, the
constructor should recieve a byte slice as input and then convert it to
registerData. It makes less sense to have the caller create the registerData
from the byte slice if bytes data is the only type of registerData, we accept.

This change removes unnecessary checks and makes the code a lot less wordy by
removing the profuse amount of newBytesData calls for comparison construction.

PiperOrigin-RevId: 670652077
  • Loading branch information
Jayden Nyamiaka authored and gvisor-bot committed Sep 3, 2024
1 parent 57902f6 commit 341a018
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 145 deletions.
11 changes: 6 additions & 5 deletions pkg/tcpip/nftables/nftables.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,18 +638,19 @@ func validateComparisonOp(cop cmpOp) error {
}

// newComparison creates a new Comparison operation.
func newComparison(sreg uint8, op int, data registerData) (*comparison, error) {
func newComparison(sreg uint8, op int, data []byte) (*comparison, error) {
if sreg == linux.NFT_REG_VERDICT {
return nil, fmt.Errorf("comparison operation cannot use verdict register as source")
}
if err := data.validateRegister(sreg); err != nil {
bytesData := newBytesData(data)
if err := bytesData.validateRegister(sreg); err != nil {
return nil, err
}
cop := cmpOp(op)
if err := validateComparisonOp(cop); err != nil {
return nil, err
}
return &comparison{sreg: sreg, cop: cop, data: data}, nil
return &comparison{sreg: sreg, cop: cop, data: bytesData}, nil
}

// evaluate for Comparison compares the data in the source register to the given
Expand Down Expand Up @@ -1003,7 +1004,7 @@ type verdictData struct {
data Verdict
}

// newVerdictData creates a RegisterData for a verdict.
// newVerdictData creates a registerData for a verdict.
func newVerdictData(verdict Verdict) registerData { return verdictData{data: verdict} }

// String returns a string representation of the verdict data.
Expand Down Expand Up @@ -1044,7 +1045,7 @@ type bytesData struct {
data []byte
}

// newBytesData creates a RegisterData for <= 16 bytes of data.
// newBytesData creates a registerData for <= 16 bytes of data.
func newBytesData(bytes []byte) registerData {
if len(bytes) == 0 {
panic("bytes data cannot be empty")
Expand Down
Loading

0 comments on commit 341a018

Please sign in to comment.