Compare commits
2 Commits
cdca03f2f7
...
139ffd651e
Author | SHA1 | Date |
---|---|---|
|
139ffd651e | |
|
41a89e5c2e |
|
@ -5,8 +5,7 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// CommentSize is the fixed size of a comment info xt blob, see:
|
||||
// https://elixir.bootlin.com/linux/v6.8.7/source/include/uapi/linux/netfilter/xt_comment.h#L5
|
||||
// CommentSize is the fixed size of a comment info xt blob.
|
||||
const CommentSize = 256
|
||||
|
||||
// Comment gets marshalled and unmarshalled as a fixed-sized char array, filled
|
||||
|
@ -16,8 +15,7 @@ type Comment string
|
|||
|
||||
func (c *Comment) marshal(fam TableFamily, rev uint32) ([]byte, error) {
|
||||
if len(*c) >= CommentSize {
|
||||
return nil, fmt.Errorf("comment must be less than %d bytes, got %d bytes",
|
||||
CommentSize, len(*c))
|
||||
return nil, fmt.Errorf("Comment must be less than %d bytes", CommentSize)
|
||||
}
|
||||
data := make([]byte, CommentSize)
|
||||
copy(data, []byte(*c))
|
||||
|
@ -26,8 +24,7 @@ func (c *Comment) marshal(fam TableFamily, rev uint32) ([]byte, error) {
|
|||
|
||||
func (c *Comment) unmarshal(fam TableFamily, rev uint32, data []byte) error {
|
||||
if len(data) != CommentSize {
|
||||
return fmt.Errorf("malformed comment: got %d bytes, expected exactly %d bytes",
|
||||
len(data), CommentSize)
|
||||
return fmt.Errorf("Comment takes exactly %d bytes", CommentSize)
|
||||
}
|
||||
*c = Comment(bytes.TrimRight(data, "\x00"))
|
||||
return nil
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package xt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
@ -22,7 +23,7 @@ func TestComment(t *testing.T) {
|
|||
{
|
||||
name: "marshal oversized Comment",
|
||||
info: &oversized,
|
||||
errmsg: "comment must be less than 256 bytes, got 600 bytes",
|
||||
errmsg: "Comment must be less than 256 bytes",
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -56,7 +57,8 @@ func TestComment(t *testing.T) {
|
|||
|
||||
oversizeddata := []byte(oversized)
|
||||
var comment Comment
|
||||
if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil {
|
||||
t.Fatalf("unmarshal: expected error, but got nil")
|
||||
if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil || err.Error() != fmt.Sprintf("Comment takes exactly %d bytes", CommentSize) {
|
||||
t.Fatalf("unmarshal: expected error: %v", err)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue