Skip to content

Commit 7c40cbd

Browse files
mateusz834gopherbot
authored andcommitted
dns/dnsmessage: use map[string]uint16 instead of map[string]int
The compression pointer is limited to 14 bits, so there is no need to use int, uint16 is fine. Change-Id: I2276cbf63761e26a7e8590f0337930db87895ea5 GitHub-Last-Rev: e04b451 GitHub-Pull-Request: #192 Reviewed-on: https://go-review.googlesource.com/c/net/+/528955 Reviewed-by: Matthew Dempsky <[email protected]> Run-TryBot: Mateusz Poliwczak <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent b3f1f23 commit 7c40cbd

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

dns/dnsmessage/message.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ func (r *Resource) GoString() string {
492492
// A ResourceBody is a DNS resource record minus the header.
493493
type ResourceBody interface {
494494
// pack packs a Resource except for its header.
495-
pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error)
495+
pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error)
496496

497497
// realType returns the actual type of the Resource. This is used to
498498
// fill in the header Type field.
@@ -503,7 +503,7 @@ type ResourceBody interface {
503503
}
504504

505505
// pack appends the wire format of the Resource to msg.
506-
func (r *Resource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
506+
func (r *Resource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
507507
if r.Body == nil {
508508
return msg, errNilResouceBody
509509
}
@@ -1129,7 +1129,7 @@ func (m *Message) AppendPack(b []byte) ([]byte, error) {
11291129
// DNS messages can be a maximum of 512 bytes long. Without compression,
11301130
// many DNS response messages are over this limit, so enabling
11311131
// compression will help ensure compliance.
1132-
compression := map[string]int{}
1132+
compression := map[string]uint16{}
11331133

11341134
for i := range m.Questions {
11351135
var err error
@@ -1220,7 +1220,7 @@ type Builder struct {
12201220

12211221
// compression is a mapping from name suffixes to their starting index
12221222
// in msg.
1223-
compression map[string]int
1223+
compression map[string]uint16
12241224
}
12251225

12261226
// NewBuilder creates a new builder with compression disabled.
@@ -1257,7 +1257,7 @@ func NewBuilder(buf []byte, h Header) Builder {
12571257
//
12581258
// Compression should be enabled before any sections are added for best results.
12591259
func (b *Builder) EnableCompression() {
1260-
b.compression = map[string]int{}
1260+
b.compression = map[string]uint16{}
12611261
}
12621262

12631263
func (b *Builder) startCheck(s section) error {
@@ -1673,7 +1673,7 @@ func (h *ResourceHeader) GoString() string {
16731673
// pack appends the wire format of the ResourceHeader to oldMsg.
16741674
//
16751675
// lenOff is the offset in msg where the Length field was packed.
1676-
func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]int, compressionOff int) (msg []byte, lenOff int, err error) {
1676+
func (h *ResourceHeader) pack(oldMsg []byte, compression map[string]uint16, compressionOff int) (msg []byte, lenOff int, err error) {
16771677
msg = oldMsg
16781678
if msg, err = h.Name.pack(msg, compression, compressionOff); err != nil {
16791679
return oldMsg, 0, &nestedError{"Name", err}
@@ -1946,7 +1946,7 @@ func (n *Name) GoString() string {
19461946
//
19471947
// The compression map will be updated with new domain suffixes. If compression
19481948
// is nil, compression will not be used.
1949-
func (n *Name) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
1949+
func (n *Name) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
19501950
oldMsg := msg
19511951

19521952
if n.Length > nonEncodedNameMax {
@@ -2010,7 +2010,7 @@ func (n *Name) pack(msg []byte, compression map[string]int, compressionOff int)
20102010
// multiple times (for next labels).
20112011
nameAsStr = string(n.Data[:n.Length])
20122012
}
2013-
compression[nameAsStr[i:]] = newPtr
2013+
compression[nameAsStr[i:]] = uint16(newPtr)
20142014
}
20152015
}
20162016
}
@@ -2150,7 +2150,7 @@ type Question struct {
21502150
}
21512151

21522152
// pack appends the wire format of the Question to msg.
2153-
func (q *Question) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2153+
func (q *Question) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
21542154
msg, err := q.Name.pack(msg, compression, compressionOff)
21552155
if err != nil {
21562156
return msg, &nestedError{"Name", err}
@@ -2246,7 +2246,7 @@ func (r *CNAMEResource) realType() Type {
22462246
}
22472247

22482248
// pack appends the wire format of the CNAMEResource to msg.
2249-
func (r *CNAMEResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2249+
func (r *CNAMEResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
22502250
return r.CNAME.pack(msg, compression, compressionOff)
22512251
}
22522252

@@ -2274,7 +2274,7 @@ func (r *MXResource) realType() Type {
22742274
}
22752275

22762276
// pack appends the wire format of the MXResource to msg.
2277-
func (r *MXResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2277+
func (r *MXResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
22782278
oldMsg := msg
22792279
msg = packUint16(msg, r.Pref)
22802280
msg, err := r.MX.pack(msg, compression, compressionOff)
@@ -2313,7 +2313,7 @@ func (r *NSResource) realType() Type {
23132313
}
23142314

23152315
// pack appends the wire format of the NSResource to msg.
2316-
func (r *NSResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2316+
func (r *NSResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
23172317
return r.NS.pack(msg, compression, compressionOff)
23182318
}
23192319

@@ -2340,7 +2340,7 @@ func (r *PTRResource) realType() Type {
23402340
}
23412341

23422342
// pack appends the wire format of the PTRResource to msg.
2343-
func (r *PTRResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2343+
func (r *PTRResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
23442344
return r.PTR.pack(msg, compression, compressionOff)
23452345
}
23462346

@@ -2377,7 +2377,7 @@ func (r *SOAResource) realType() Type {
23772377
}
23782378

23792379
// pack appends the wire format of the SOAResource to msg.
2380-
func (r *SOAResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2380+
func (r *SOAResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
23812381
oldMsg := msg
23822382
msg, err := r.NS.pack(msg, compression, compressionOff)
23832383
if err != nil {
@@ -2449,7 +2449,7 @@ func (r *TXTResource) realType() Type {
24492449
}
24502450

24512451
// pack appends the wire format of the TXTResource to msg.
2452-
func (r *TXTResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2452+
func (r *TXTResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
24532453
oldMsg := msg
24542454
for _, s := range r.TXT {
24552455
var err error
@@ -2505,7 +2505,7 @@ func (r *SRVResource) realType() Type {
25052505
}
25062506

25072507
// pack appends the wire format of the SRVResource to msg.
2508-
func (r *SRVResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2508+
func (r *SRVResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
25092509
oldMsg := msg
25102510
msg = packUint16(msg, r.Priority)
25112511
msg = packUint16(msg, r.Weight)
@@ -2556,7 +2556,7 @@ func (r *AResource) realType() Type {
25562556
}
25572557

25582558
// pack appends the wire format of the AResource to msg.
2559-
func (r *AResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2559+
func (r *AResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
25602560
return packBytes(msg, r.A[:]), nil
25612561
}
25622562

@@ -2590,7 +2590,7 @@ func (r *AAAAResource) GoString() string {
25902590
}
25912591

25922592
// pack appends the wire format of the AAAAResource to msg.
2593-
func (r *AAAAResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2593+
func (r *AAAAResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
25942594
return packBytes(msg, r.AAAA[:]), nil
25952595
}
25962596

@@ -2630,7 +2630,7 @@ func (r *OPTResource) realType() Type {
26302630
return TypeOPT
26312631
}
26322632

2633-
func (r *OPTResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2633+
func (r *OPTResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
26342634
for _, opt := range r.Options {
26352635
msg = packUint16(msg, opt.Code)
26362636
l := uint16(len(opt.Data))
@@ -2688,7 +2688,7 @@ func (r *UnknownResource) realType() Type {
26882688
}
26892689

26902690
// pack appends the wire format of the UnknownResource to msg.
2691-
func (r *UnknownResource) pack(msg []byte, compression map[string]int, compressionOff int) ([]byte, error) {
2691+
func (r *UnknownResource) pack(msg []byte, compression map[string]uint16, compressionOff int) ([]byte, error) {
26922692
return packBytes(msg, r.Data[:]), nil
26932693
}
26942694

dns/dnsmessage/message_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestQuestionPackUnpack(t *testing.T) {
164164
Type: TypeA,
165165
Class: ClassINET,
166166
}
167-
buf, err := want.pack(make([]byte, 1, 50), map[string]int{}, 1)
167+
buf, err := want.pack(make([]byte, 1, 50), map[string]uint16{}, 1)
168168
if err != nil {
169169
t.Fatal("Question.pack() =", err)
170170
}
@@ -243,7 +243,7 @@ func TestNamePackUnpack(t *testing.T) {
243243

244244
for _, test := range tests {
245245
in := MustNewName(test.in)
246-
buf, err := in.pack(make([]byte, 0, 30), map[string]int{}, 0)
246+
buf, err := in.pack(make([]byte, 0, 30), map[string]uint16{}, 0)
247247
if err != test.err {
248248
t.Errorf("got %q.pack() = %v, want = %v", test.in, err, test.err)
249249
continue
@@ -305,7 +305,7 @@ func TestNameUnpackTooLongName(t *testing.T) {
305305

306306
func TestIncompressibleName(t *testing.T) {
307307
name := MustNewName("example.com.")
308-
compression := map[string]int{}
308+
compression := map[string]uint16{}
309309
buf, err := name.pack(make([]byte, 0, 100), compression, 0)
310310
if err != nil {
311311
t.Fatal("first Name.pack() =", err)
@@ -623,7 +623,7 @@ func TestVeryLongTxt(t *testing.T) {
623623
strings.Repeat(".", 255),
624624
}},
625625
}
626-
buf, err := want.pack(make([]byte, 0, 8000), map[string]int{}, 0)
626+
buf, err := want.pack(make([]byte, 0, 8000), map[string]uint16{}, 0)
627627
if err != nil {
628628
t.Fatal("Resource.pack() =", err)
629629
}
@@ -647,7 +647,7 @@ func TestVeryLongTxt(t *testing.T) {
647647

648648
func TestTooLongTxt(t *testing.T) {
649649
rb := TXTResource{[]string{strings.Repeat(".", 256)}}
650-
if _, err := rb.pack(make([]byte, 0, 8000), map[string]int{}, 0); err != errStringTooLong {
650+
if _, err := rb.pack(make([]byte, 0, 8000), map[string]uint16{}, 0); err != errStringTooLong {
651651
t.Errorf("packing TXTResource with 256 character string: got err = %v, want = %v", err, errStringTooLong)
652652
}
653653
}

0 commit comments

Comments
 (0)