Compare commits
2 Commits
139ffd651e
...
cdca03f2f7
Author | SHA1 | Date |
---|---|---|
|
cdca03f2f7 | |
|
e9249a316d |
|
@ -5,7 +5,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CommentSize is the fixed size of a comment info xt blob.
|
// 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
|
||||||
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
|
||||||
|
@ -15,7 +16,8 @@ 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", CommentSize)
|
return nil, fmt.Errorf("comment must be less than %d bytes, got %d bytes",
|
||||||
|
CommentSize, len(*c))
|
||||||
}
|
}
|
||||||
data := make([]byte, CommentSize)
|
data := make([]byte, CommentSize)
|
||||||
copy(data, []byte(*c))
|
copy(data, []byte(*c))
|
||||||
|
@ -24,7 +26,8 @@ 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("Comment takes exactly %d bytes", CommentSize)
|
return fmt.Errorf("malformed comment: got %d bytes, expected exactly %d bytes",
|
||||||
|
len(data), CommentSize)
|
||||||
}
|
}
|
||||||
*c = Comment(bytes.TrimRight(data, "\x00"))
|
*c = Comment(bytes.TrimRight(data, "\x00"))
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package xt
|
package xt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -23,7 +22,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",
|
errmsg: "comment must be less than 256 bytes, got 600 bytes",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,8 +56,7 @@ 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 || err.Error() != fmt.Sprintf("Comment takes exactly %d bytes", CommentSize) {
|
if err := (&comment).unmarshal(0, 0, oversizeddata); err == nil {
|
||||||
t.Fatalf("unmarshal: expected error: %v", err)
|
t.Fatalf("unmarshal: expected error, but got nil")
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue