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