• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nats-server / 19658562439

24 Nov 2025 12:55PM UTC coverage: 69.106% (-17.0%) from 86.129%
19658562439

push

github

web-flow
NRG: Don't reset WAL when failing to load last snapshot (#7580)

In most cases we can either install a new snapshot before shutting down,
or if not, we can better detect the situation on the next startup.

ref: #7556

Signed-off-by: Neil Twigg <neil@nats.io>

60221 of 87143 relevant lines covered (69.11%)

271294.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.48
/server/parser.go
1
// Copyright 2012-2025 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package server
15

16
import (
17
        "bufio"
18
        "bytes"
19
        "fmt"
20
        "net/http"
21
        "net/textproto"
22
)
23

24
type parserState int
25
type parseState struct {
26
        state   parserState
27
        op      byte
28
        as      int
29
        drop    int
30
        pa      pubArg
31
        argBuf  []byte
32
        msgBuf  []byte
33
        header  http.Header // access via getHeader
34
        scratch [MAX_CONTROL_LINE_SIZE]byte
35
}
36

37
type pubArg struct {
38
        arg       []byte
39
        pacache   []byte
40
        origin    []byte
41
        account   []byte
42
        subject   []byte
43
        deliver   []byte
44
        mapped    []byte
45
        reply     []byte
46
        szb       []byte
47
        hdb       []byte
48
        queues    [][]byte
49
        size      int
50
        hdr       int
51
        psi       []*serviceImport
52
        trace     *msgTrace
53
        delivered bool // Only used for service imports
54
}
55

56
// Parser constants
57
const (
58
        OP_START parserState = iota
59
        OP_PLUS
60
        OP_PLUS_O
61
        OP_PLUS_OK
62
        OP_MINUS
63
        OP_MINUS_E
64
        OP_MINUS_ER
65
        OP_MINUS_ERR
66
        OP_MINUS_ERR_SPC
67
        MINUS_ERR_ARG
68
        OP_C
69
        OP_CO
70
        OP_CON
71
        OP_CONN
72
        OP_CONNE
73
        OP_CONNEC
74
        OP_CONNECT
75
        CONNECT_ARG
76
        OP_H
77
        OP_HP
78
        OP_HPU
79
        OP_HPUB
80
        OP_HPUB_SPC
81
        HPUB_ARG
82
        OP_HM
83
        OP_HMS
84
        OP_HMSG
85
        OP_HMSG_SPC
86
        HMSG_ARG
87
        OP_P
88
        OP_PU
89
        OP_PUB
90
        OP_PUB_SPC
91
        PUB_ARG
92
        OP_PI
93
        OP_PIN
94
        OP_PING
95
        OP_PO
96
        OP_PON
97
        OP_PONG
98
        MSG_PAYLOAD
99
        MSG_END_R
100
        MSG_END_N
101
        OP_S
102
        OP_SU
103
        OP_SUB
104
        OP_SUB_SPC
105
        SUB_ARG
106
        OP_A
107
        OP_ASUB
108
        OP_ASUB_SPC
109
        ASUB_ARG
110
        OP_AUSUB
111
        OP_AUSUB_SPC
112
        AUSUB_ARG
113
        OP_L
114
        OP_LS
115
        OP_R
116
        OP_RS
117
        OP_U
118
        OP_UN
119
        OP_UNS
120
        OP_UNSU
121
        OP_UNSUB
122
        OP_UNSUB_SPC
123
        UNSUB_ARG
124
        OP_M
125
        OP_MS
126
        OP_MSG
127
        OP_MSG_SPC
128
        MSG_ARG
129
        OP_I
130
        OP_IN
131
        OP_INF
132
        OP_INFO
133
        INFO_ARG
134
)
135

136
func (c *client) parse(buf []byte) error {
6,087,281✔
137
        // Branch out to mqtt clients. c.mqtt is immutable, but should it become
6,087,281✔
138
        // an issue (say data race detection), we could branch outside in readLoop
6,087,281✔
139
        if c.isMqtt() {
6,087,281✔
140
                return c.mqttParse(buf)
×
141
        }
×
142
        var i int
6,087,281✔
143
        var b byte
6,087,281✔
144
        var lmsg bool
6,087,281✔
145

6,087,281✔
146
        // Snapshots
6,087,281✔
147
        c.mu.Lock()
6,087,281✔
148
        // Snapshot and then reset when we receive a
6,087,281✔
149
        // proper CONNECT if needed.
6,087,281✔
150
        authSet := c.awaitingAuth()
6,087,281✔
151
        // Snapshot max control line as well.
6,087,281✔
152
        s, mcl, trace := c.srv, c.mcl, c.trace
6,087,281✔
153
        c.mu.Unlock()
6,087,281✔
154

6,087,281✔
155
        // Move to loop instead of range syntax to allow jumping of i
6,087,281✔
156
        for i = 0; i < len(buf); i++ {
494,072,322✔
157
                b = buf[i]
487,985,041✔
158

487,985,041✔
159
                switch c.state {
487,985,041✔
160
                case OP_START:
14,592,932✔
161
                        c.op = b
14,592,932✔
162
                        if b != 'C' && b != 'c' {
29,160,260✔
163
                                if authSet {
14,567,342✔
164
                                        if s == nil {
14✔
165
                                                goto authErr
×
166
                                        }
167
                                        var ok bool
14✔
168
                                        // Check here for NoAuthUser. If this is set allow non CONNECT protos as our first.
14✔
169
                                        // E.g. telnet proto demos.
14✔
170
                                        if noAuthUser := s.getOpts().NoAuthUser; noAuthUser != _EMPTY_ {
16✔
171
                                                s.mu.Lock()
2✔
172
                                                user, exists := s.users[noAuthUser]
2✔
173
                                                s.mu.Unlock()
2✔
174
                                                if exists {
4✔
175
                                                        c.RegisterUser(user)
2✔
176
                                                        c.mu.Lock()
2✔
177
                                                        c.clearAuthTimer()
2✔
178
                                                        c.flags.set(connectReceived)
2✔
179
                                                        c.mu.Unlock()
2✔
180
                                                        authSet, ok = false, true
2✔
181
                                                }
2✔
182
                                        }
183
                                        if !ok {
26✔
184
                                                goto authErr
12✔
185
                                        }
186
                                }
187
                                // If the connection is a gateway connection, make sure that
188
                                // if this is an inbound, it starts with a CONNECT.
189
                                if c.kind == GATEWAY && !c.gw.outbound && !c.gw.connected {
14,567,316✔
190
                                        // Use auth violation since no CONNECT was sent.
×
191
                                        // It could be a parseErr too.
×
192
                                        goto authErr
×
193
                                }
194
                        }
195
                        switch b {
14,592,920✔
196
                        case 'P', 'p':
7,396,539✔
197
                                c.state = OP_P
7,396,539✔
198
                        case 'H', 'h':
660,864✔
199
                                c.state = OP_H
660,864✔
200
                        case 'S', 's':
127,183✔
201
                                c.state = OP_S
127,183✔
202
                        case 'U', 'u':
11,211✔
203
                                c.state = OP_U
11,211✔
204
                        case 'R', 'r':
6,290,475✔
205
                                if c.kind == CLIENT {
6,290,475✔
206
                                        goto parseErr
×
207
                                } else {
6,290,475✔
208
                                        c.state = OP_R
6,290,475✔
209
                                }
6,290,475✔
210
                        case 'L', 'l':
32,462✔
211
                                if c.kind != LEAF && c.kind != ROUTER {
32,462✔
212
                                        goto parseErr
×
213
                                } else {
32,462✔
214
                                        c.state = OP_L
32,462✔
215
                                }
32,462✔
216
                        case 'A', 'a':
17✔
217
                                if c.kind == CLIENT {
17✔
218
                                        goto parseErr
×
219
                                } else {
17✔
220
                                        c.state = OP_A
17✔
221
                                }
17✔
222
                        case 'C', 'c':
25,604✔
223
                                c.state = OP_C
25,604✔
224
                        case 'I', 'i':
48,460✔
225
                                c.state = OP_I
48,460✔
226
                        case '+':
×
227
                                c.state = OP_PLUS
×
228
                        case '-':
102✔
229
                                c.state = OP_MINUS
102✔
230
                        default:
3✔
231
                                goto parseErr
3✔
232
                        }
233
                case OP_H:
660,864✔
234
                        switch b {
660,864✔
235
                        case 'P', 'p':
255,980✔
236
                                c.state = OP_HP
255,980✔
237
                        case 'M', 'm':
404,884✔
238
                                c.state = OP_HM
404,884✔
239
                        default:
×
240
                                goto parseErr
×
241
                        }
242
                case OP_HP:
255,980✔
243
                        switch b {
255,980✔
244
                        case 'U', 'u':
255,980✔
245
                                c.state = OP_HPU
255,980✔
246
                        default:
×
247
                                goto parseErr
×
248
                        }
249
                case OP_HPU:
255,980✔
250
                        switch b {
255,980✔
251
                        case 'B', 'b':
255,980✔
252
                                c.state = OP_HPUB
255,980✔
253
                        default:
×
254
                                goto parseErr
×
255
                        }
256
                case OP_HPUB:
255,980✔
257
                        switch b {
255,980✔
258
                        case ' ', '\t':
255,980✔
259
                                c.state = OP_HPUB_SPC
255,980✔
260
                        default:
×
261
                                goto parseErr
×
262
                        }
263
                case OP_HPUB_SPC:
255,980✔
264
                        switch b {
255,980✔
265
                        case ' ', '\t':
×
266
                                continue
×
267
                        default:
255,980✔
268
                                c.pa.hdr = 0
255,980✔
269
                                c.state = HPUB_ARG
255,980✔
270
                                c.as = i
255,980✔
271
                        }
272
                case HPUB_ARG:
9,349,756✔
273
                        switch b {
9,349,756✔
274
                        case '\r':
255,980✔
275
                                c.drop = 1
255,980✔
276
                        case '\n':
255,980✔
277
                                var arg []byte
255,980✔
278
                                if c.argBuf != nil {
256,031✔
279
                                        arg = c.argBuf
51✔
280
                                        c.argBuf = nil
51✔
281
                                } else {
255,980✔
282
                                        arg = buf[c.as : i-c.drop]
255,929✔
283
                                }
255,929✔
284
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
255,980✔
285
                                        return err
×
286
                                }
×
287
                                if trace {
255,984✔
288
                                        c.traceInOp("HPUB", arg)
4✔
289
                                }
4✔
290
                                var remaining []byte
255,980✔
291
                                if i < len(buf) {
511,960✔
292
                                        remaining = buf[i+1:]
255,980✔
293
                                }
255,980✔
294
                                if err := c.processHeaderPub(arg, remaining); err != nil {
255,982✔
295
                                        return err
2✔
296
                                }
2✔
297

298
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
255,978✔
299
                                // If we don't have a saved buffer then jump ahead with
255,978✔
300
                                // the index. If this overruns what is left we fall out
255,978✔
301
                                // and process split buffer.
255,978✔
302
                                if c.msgBuf == nil {
511,956✔
303
                                        i = c.as + c.pa.size - LEN_CR_LF
255,978✔
304
                                }
255,978✔
305
                        default:
8,837,796✔
306
                                if c.argBuf != nil {
8,838,474✔
307
                                        c.argBuf = append(c.argBuf, b)
678✔
308
                                }
678✔
309
                        }
310
                case OP_HM:
404,884✔
311
                        switch b {
404,884✔
312
                        case 'S', 's':
404,884✔
313
                                c.state = OP_HMS
404,884✔
314
                        default:
×
315
                                goto parseErr
×
316
                        }
317
                case OP_HMS:
404,884✔
318
                        switch b {
404,884✔
319
                        case 'G', 'g':
404,884✔
320
                                c.state = OP_HMSG
404,884✔
321
                        default:
×
322
                                goto parseErr
×
323
                        }
324
                case OP_HMSG:
404,884✔
325
                        switch b {
404,884✔
326
                        case ' ', '\t':
404,884✔
327
                                c.state = OP_HMSG_SPC
404,884✔
328
                        default:
×
329
                                goto parseErr
×
330
                        }
331
                case OP_HMSG_SPC:
404,884✔
332
                        switch b {
404,884✔
333
                        case ' ', '\t':
×
334
                                continue
×
335
                        default:
404,884✔
336
                                c.pa.hdr = 0
404,884✔
337
                                c.state = HMSG_ARG
404,884✔
338
                                c.as = i
404,884✔
339
                        }
340
                case HMSG_ARG:
27,513,135✔
341
                        switch b {
27,513,135✔
342
                        case '\r':
404,884✔
343
                                c.drop = 1
404,884✔
344
                        case '\n':
404,884✔
345
                                var arg []byte
404,884✔
346
                                if c.argBuf != nil {
407,497✔
347
                                        arg = c.argBuf
2,613✔
348
                                        c.argBuf = nil
2,613✔
349
                                } else {
404,884✔
350
                                        arg = buf[c.as : i-c.drop]
402,271✔
351
                                }
402,271✔
352
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
404,884✔
353
                                        return err
×
354
                                }
×
355
                                var err error
404,884✔
356
                                if c.kind == ROUTER || c.kind == GATEWAY {
809,738✔
357
                                        if trace {
406,576✔
358
                                                c.traceInOp("HMSG", arg)
1,722✔
359
                                        }
1,722✔
360
                                        err = c.processRoutedHeaderMsgArgs(arg)
404,854✔
361
                                } else if c.kind == LEAF {
60✔
362
                                        if trace {
30✔
363
                                                c.traceInOp("HMSG", arg)
×
364
                                        }
×
365
                                        err = c.processLeafHeaderMsgArgs(arg)
30✔
366
                                }
367
                                if err != nil {
404,884✔
368
                                        return err
×
369
                                }
×
370
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
404,884✔
371

404,884✔
372
                                // jump ahead with the index. If this overruns
404,884✔
373
                                // what is left we fall out and process split
404,884✔
374
                                // buffer.
404,884✔
375
                                i = c.as + c.pa.size - LEN_CR_LF
404,884✔
376
                        default:
26,703,367✔
377
                                if c.argBuf != nil {
26,805,625✔
378
                                        c.argBuf = append(c.argBuf, b)
102,258✔
379
                                }
102,258✔
380
                        }
381
                case OP_P:
7,396,539✔
382
                        switch b {
7,396,539✔
383
                        case 'U', 'u':
7,359,833✔
384
                                c.state = OP_PU
7,359,833✔
385
                        case 'I', 'i':
21,312✔
386
                                c.state = OP_PI
21,312✔
387
                        case 'O', 'o':
15,394✔
388
                                c.state = OP_PO
15,394✔
389
                        default:
×
390
                                goto parseErr
×
391
                        }
392
                case OP_PU:
7,359,833✔
393
                        switch b {
7,359,833✔
394
                        case 'B', 'b':
7,359,833✔
395
                                c.state = OP_PUB
7,359,833✔
396
                        default:
×
397
                                goto parseErr
×
398
                        }
399
                case OP_PUB:
7,359,833✔
400
                        switch b {
7,359,833✔
401
                        case ' ', '\t':
7,359,832✔
402
                                c.state = OP_PUB_SPC
7,359,832✔
403
                        default:
1✔
404
                                goto parseErr
1✔
405
                        }
406
                case OP_PUB_SPC:
7,359,829✔
407
                        switch b {
7,359,829✔
408
                        case ' ', '\t':
×
409
                                continue
×
410
                        default:
7,359,829✔
411
                                c.pa.hdr = -1
7,359,829✔
412
                                c.state = PUB_ARG
7,359,829✔
413
                                c.as = i
7,359,829✔
414
                        }
415
                case PUB_ARG:
86,425,657✔
416
                        switch b {
86,425,657✔
417
                        case '\r':
7,359,825✔
418
                                c.drop = 1
7,359,825✔
419
                        case '\n':
7,359,825✔
420
                                var arg []byte
7,359,825✔
421
                                if c.argBuf != nil {
7,362,800✔
422
                                        arg = c.argBuf
2,975✔
423
                                        c.argBuf = nil
2,975✔
424
                                } else {
7,359,825✔
425
                                        arg = buf[c.as : i-c.drop]
7,356,850✔
426
                                }
7,356,850✔
427
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
7,359,825✔
428
                                        return err
×
429
                                }
×
430
                                if trace {
7,447,224✔
431
                                        c.traceInOp("PUB", arg)
87,399✔
432
                                }
87,399✔
433
                                if err := c.processPub(arg); err != nil {
7,359,829✔
434
                                        return err
4✔
435
                                }
4✔
436

437
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
7,359,821✔
438
                                // If we don't have a saved buffer then jump ahead with
7,359,821✔
439
                                // the index. If this overruns what is left we fall out
7,359,821✔
440
                                // and process split buffer.
7,359,821✔
441
                                if c.msgBuf == nil {
14,719,642✔
442
                                        i = c.as + c.pa.size - LEN_CR_LF
7,359,821✔
443
                                }
7,359,821✔
444
                        default:
71,706,007✔
445
                                if c.argBuf != nil {
71,767,900✔
446
                                        c.argBuf = append(c.argBuf, b)
61,893✔
447
                                }
61,893✔
448
                        }
449
                case MSG_PAYLOAD:
13,814,263✔
450
                        if c.msgBuf != nil {
14,706,252✔
451
                                // copy as much as we can to the buffer and skip ahead.
891,989✔
452
                                toCopy := c.pa.size - len(c.msgBuf)
891,989✔
453
                                avail := len(buf) - i
891,989✔
454
                                if avail < toCopy {
1,168,374✔
455
                                        toCopy = avail
276,385✔
456
                                }
276,385✔
457
                                if toCopy > 0 {
1,783,978✔
458
                                        start := len(c.msgBuf)
891,989✔
459
                                        // This is needed for copy to work.
891,989✔
460
                                        c.msgBuf = c.msgBuf[:start+toCopy]
891,989✔
461
                                        copy(c.msgBuf[start:], buf[i:i+toCopy])
891,989✔
462
                                        // Update our index
891,989✔
463
                                        i = (i + toCopy) - 1
891,989✔
464
                                } else {
891,989✔
465
                                        // Fall back to append if needed.
×
466
                                        c.msgBuf = append(c.msgBuf, b)
×
467
                                }
×
468
                                if len(c.msgBuf) >= c.pa.size {
1,507,593✔
469
                                        c.state = MSG_END_R
615,604✔
470
                                }
615,604✔
471
                        } else if i-c.as+1 >= c.pa.size {
25,844,548✔
472
                                c.state = MSG_END_R
12,922,274✔
473
                        }
12,922,274✔
474
                case MSG_END_R:
13,537,877✔
475
                        if b != '\r' {
13,537,878✔
476
                                goto parseErr
1✔
477
                        }
478
                        if c.msgBuf != nil {
14,165,461✔
479
                                c.msgBuf = append(c.msgBuf, b)
627,585✔
480
                        }
627,585✔
481
                        c.state = MSG_END_N
13,537,876✔
482
                case MSG_END_N:
13,537,876✔
483
                        if b != '\n' {
13,537,876✔
484
                                goto parseErr
×
485
                        }
486
                        if c.msgBuf != nil {
14,166,753✔
487
                                c.msgBuf = append(c.msgBuf, b)
628,877✔
488
                        } else {
13,537,876✔
489
                                c.msgBuf = buf[c.as : i+1]
12,908,999✔
490
                        }
12,908,999✔
491

492
                        var mt *msgTrace
13,537,876✔
493
                        if c.pa.hdr > 0 {
14,198,802✔
494
                                mt = c.initMsgTrace()
660,926✔
495
                        }
660,926✔
496
                        // Check for mappings.
497
                        if (c.kind == CLIENT || c.kind == LEAF) && c.in.flags.isSet(hasMappings) {
13,549,903✔
498
                                changed := c.selectMappedSubject()
12,027✔
499
                                if changed {
22,319✔
500
                                        if trace {
10,292✔
501
                                                c.traceInOp("MAPPING", []byte(fmt.Sprintf("%s -> %s", c.pa.mapped, c.pa.subject)))
×
502
                                        }
×
503
                                        // c.pa.subject is the subject the original is now mapped to.
504
                                        mt.addSubjectMappingEvent(c.pa.subject)
10,292✔
505
                                }
506
                        }
507
                        if trace {
13,631,051✔
508
                                c.traceMsg(c.msgBuf)
93,175✔
509
                        }
93,175✔
510

511
                        c.processInboundMsg(c.msgBuf)
13,537,876✔
512

13,537,876✔
513
                        mt.sendEvent()
13,537,876✔
514
                        c.argBuf, c.msgBuf, c.header = nil, nil, nil
13,537,876✔
515
                        c.drop, c.as, c.state = 0, i+1, OP_START
13,537,876✔
516
                        // Drop all pub args
13,537,876✔
517
                        c.pa.arg, c.pa.pacache, c.pa.origin, c.pa.account, c.pa.subject, c.pa.mapped = nil, nil, nil, nil, nil, nil
13,537,876✔
518
                        c.pa.reply, c.pa.hdr, c.pa.size, c.pa.szb, c.pa.hdb, c.pa.queues = nil, -1, 0, nil, nil, nil
13,537,876✔
519
                        c.pa.trace = nil
13,537,876✔
520
                        c.pa.delivered = false
13,537,876✔
521
                        lmsg = false
13,537,876✔
522
                case OP_A:
17✔
523
                        switch b {
17✔
524
                        case '+':
4✔
525
                                c.state = OP_ASUB
4✔
526
                        case '-', 'u':
13✔
527
                                c.state = OP_AUSUB
13✔
528
                        default:
×
529
                                goto parseErr
×
530
                        }
531
                case OP_ASUB:
4✔
532
                        switch b {
4✔
533
                        case ' ', '\t':
4✔
534
                                c.state = OP_ASUB_SPC
4✔
535
                        default:
×
536
                                goto parseErr
×
537
                        }
538
                case OP_ASUB_SPC:
4✔
539
                        switch b {
4✔
540
                        case ' ', '\t':
×
541
                                continue
×
542
                        default:
4✔
543
                                c.state = ASUB_ARG
4✔
544
                                c.as = i
4✔
545
                        }
546
                case ASUB_ARG:
14✔
547
                        switch b {
14✔
548
                        case '\r':
4✔
549
                                c.drop = 1
4✔
550
                        case '\n':
4✔
551
                                var arg []byte
4✔
552
                                if c.argBuf != nil {
4✔
553
                                        arg = c.argBuf
×
554
                                        c.argBuf = nil
×
555
                                } else {
4✔
556
                                        arg = buf[c.as : i-c.drop]
4✔
557
                                }
4✔
558
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
4✔
559
                                        return err
×
560
                                }
×
561
                                if trace {
4✔
562
                                        c.traceInOp("A+", arg)
×
563
                                }
×
564
                                if err := c.processAccountSub(arg); err != nil {
4✔
565
                                        return err
×
566
                                }
×
567
                                c.drop, c.as, c.state = 0, i+1, OP_START
4✔
568
                        default:
6✔
569
                                if c.argBuf != nil {
6✔
570
                                        c.argBuf = append(c.argBuf, b)
×
571
                                }
×
572
                        }
573
                case OP_AUSUB:
13✔
574
                        switch b {
13✔
575
                        case ' ', '\t':
13✔
576
                                c.state = OP_AUSUB_SPC
13✔
577
                        default:
×
578
                                goto parseErr
×
579
                        }
580
                case OP_AUSUB_SPC:
13✔
581
                        switch b {
13✔
582
                        case ' ', '\t':
×
583
                                continue
×
584
                        default:
13✔
585
                                c.state = AUSUB_ARG
13✔
586
                                c.as = i
13✔
587
                        }
588
                case AUSUB_ARG:
47✔
589
                        switch b {
47✔
590
                        case '\r':
13✔
591
                                c.drop = 1
13✔
592
                        case '\n':
13✔
593
                                var arg []byte
13✔
594
                                if c.argBuf != nil {
13✔
595
                                        arg = c.argBuf
×
596
                                        c.argBuf = nil
×
597
                                } else {
13✔
598
                                        arg = buf[c.as : i-c.drop]
13✔
599
                                }
13✔
600
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
13✔
601
                                        return err
×
602
                                }
×
603
                                if trace {
15✔
604
                                        c.traceInOp("A-", arg)
2✔
605
                                }
2✔
606
                                c.processAccountUnsub(arg)
13✔
607
                                c.drop, c.as, c.state = 0, i+1, OP_START
13✔
608
                        default:
21✔
609
                                if c.argBuf != nil {
21✔
610
                                        c.argBuf = append(c.argBuf, b)
×
611
                                }
×
612
                        }
613
                case OP_S:
127,183✔
614
                        switch b {
127,183✔
615
                        case 'U', 'u':
127,183✔
616
                                c.state = OP_SU
127,183✔
617
                        default:
×
618
                                goto parseErr
×
619
                        }
620
                case OP_SU:
127,183✔
621
                        switch b {
127,183✔
622
                        case 'B', 'b':
127,183✔
623
                                c.state = OP_SUB
127,183✔
624
                        default:
×
625
                                goto parseErr
×
626
                        }
627
                case OP_SUB:
786,340✔
628
                        switch b {
786,340✔
629
                        case ' ', '\t':
786,339✔
630
                                c.state = OP_SUB_SPC
786,339✔
631
                        default:
1✔
632
                                goto parseErr
1✔
633
                        }
634
                case OP_SUB_SPC:
786,337✔
635
                        switch b {
786,337✔
636
                        case ' ', '\t':
×
637
                                continue
×
638
                        default:
786,337✔
639
                                c.state = SUB_ARG
786,337✔
640
                                c.as = i
786,337✔
641
                        }
642
                case SUB_ARG:
26,556,503✔
643
                        switch b {
26,556,503✔
644
                        case '\r':
786,326✔
645
                                c.drop = 1
786,326✔
646
                        case '\n':
786,325✔
647
                                var arg []byte
786,325✔
648
                                if c.argBuf != nil {
802,822✔
649
                                        arg = c.argBuf
16,497✔
650
                                        c.argBuf = nil
16,497✔
651
                                } else {
786,325✔
652
                                        arg = buf[c.as : i-c.drop]
769,828✔
653
                                }
769,828✔
654
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
786,325✔
655
                                        return err
×
656
                                }
×
657
                                var err error
786,325✔
658

786,325✔
659
                                switch c.kind {
786,325✔
660
                                case CLIENT:
127,181✔
661
                                        if trace {
137,744✔
662
                                                c.traceInOp("SUB", arg)
10,563✔
663
                                        }
10,563✔
664
                                        err = c.parseSub(arg, false)
127,181✔
665
                                case ROUTER:
591,604✔
666
                                        switch c.op {
591,604✔
667
                                        case 'R', 'r':
586,045✔
668
                                                if trace {
586,134✔
669
                                                        c.traceInOp("RS+", arg)
89✔
670
                                                }
89✔
671
                                                err = c.processRemoteSub(arg, false)
586,045✔
672
                                        case 'L', 'l':
5,559✔
673
                                                if trace {
5,559✔
674
                                                        c.traceInOp("LS+", arg)
×
675
                                                }
×
676
                                                err = c.processRemoteSub(arg, true)
5,559✔
677
                                        }
678
                                case GATEWAY:
53,003✔
679
                                        if trace {
53,177✔
680
                                                c.traceInOp("RS+", arg)
174✔
681
                                        }
174✔
682
                                        err = c.processGatewayRSub(arg)
53,003✔
683
                                case LEAF:
14,537✔
684
                                        if trace {
14,537✔
685
                                                c.traceInOp("LS+", arg)
×
686
                                        }
×
687
                                        err = c.processLeafSub(arg)
14,537✔
688
                                }
689
                                if err != nil {
786,325✔
690
                                        return err
×
691
                                }
×
692
                                c.drop, c.as, c.state = 0, i+1, OP_START
786,325✔
693
                        default:
24,983,852✔
694
                                if c.argBuf != nil {
25,378,244✔
695
                                        c.argBuf = append(c.argBuf, b)
394,392✔
696
                                }
394,392✔
697
                        }
698
                case OP_L:
32,462✔
699
                        switch b {
32,462✔
700
                        case 'S', 's':
27,278✔
701
                                c.state = OP_LS
27,278✔
702
                        case 'M', 'm':
5,184✔
703
                                c.state = OP_M
5,184✔
704
                        default:
×
705
                                goto parseErr
×
706
                        }
707
                case OP_LS:
27,278✔
708
                        switch b {
27,278✔
709
                        case '+':
20,096✔
710
                                c.state = OP_SUB
20,096✔
711
                        case '-':
7,182✔
712
                                c.state = OP_UNSUB
7,182✔
713
                        default:
×
714
                                goto parseErr
×
715
                        }
716
                case OP_R:
6,290,475✔
717
                        switch b {
6,290,475✔
718
                        case 'S', 's':
778,447✔
719
                                c.state = OP_RS
778,447✔
720
                        case 'M', 'm':
5,512,028✔
721
                                c.state = OP_M
5,512,028✔
722
                        default:
×
723
                                goto parseErr
×
724
                        }
725
                case OP_RS:
778,447✔
726
                        switch b {
778,447✔
727
                        case '+':
639,061✔
728
                                c.state = OP_SUB
639,061✔
729
                        case '-':
139,386✔
730
                                c.state = OP_UNSUB
139,386✔
731
                        default:
×
732
                                goto parseErr
×
733
                        }
734
                case OP_U:
11,211✔
735
                        switch b {
11,211✔
736
                        case 'N', 'n':
11,211✔
737
                                c.state = OP_UN
11,211✔
738
                        default:
×
739
                                goto parseErr
×
740
                        }
741
                case OP_UN:
11,211✔
742
                        switch b {
11,211✔
743
                        case 'S', 's':
11,211✔
744
                                c.state = OP_UNS
11,211✔
745
                        default:
×
746
                                goto parseErr
×
747
                        }
748
                case OP_UNS:
11,211✔
749
                        switch b {
11,211✔
750
                        case 'U', 'u':
11,211✔
751
                                c.state = OP_UNSU
11,211✔
752
                        default:
×
753
                                goto parseErr
×
754
                        }
755
                case OP_UNSU:
11,211✔
756
                        switch b {
11,211✔
757
                        case 'B', 'b':
11,211✔
758
                                c.state = OP_UNSUB
11,211✔
759
                        default:
×
760
                                goto parseErr
×
761
                        }
762
                case OP_UNSUB:
157,779✔
763
                        switch b {
157,779✔
764
                        case ' ', '\t':
157,779✔
765
                                c.state = OP_UNSUB_SPC
157,779✔
766
                        default:
×
767
                                goto parseErr
×
768
                        }
769
                case OP_UNSUB_SPC:
157,779✔
770
                        switch b {
157,779✔
771
                        case ' ', '\t':
×
772
                                continue
×
773
                        default:
157,779✔
774
                                c.state = UNSUB_ARG
157,779✔
775
                                c.as = i
157,779✔
776
                        }
777
                case UNSUB_ARG:
3,878,024✔
778
                        switch b {
3,878,024✔
779
                        case '\r':
157,777✔
780
                                c.drop = 1
157,777✔
781
                        case '\n':
157,776✔
782
                                var arg []byte
157,776✔
783
                                if c.argBuf != nil {
161,447✔
784
                                        arg = c.argBuf
3,671✔
785
                                        c.argBuf = nil
3,671✔
786
                                } else {
157,776✔
787
                                        arg = buf[c.as : i-c.drop]
154,105✔
788
                                }
154,105✔
789
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
157,776✔
790
                                        return err
×
791
                                }
×
792
                                var err error
157,776✔
793

157,776✔
794
                                switch c.kind {
157,776✔
795
                                case CLIENT:
11,211✔
796
                                        if trace {
21,660✔
797
                                                c.traceInOp("UNSUB", arg)
10,449✔
798
                                        }
10,449✔
799
                                        err = c.processUnsub(arg)
11,211✔
800
                                case ROUTER:
128,638✔
801
                                        if trace && c.srv != nil {
128,665✔
802
                                                switch c.op {
27✔
803
                                                case 'R', 'r':
27✔
804
                                                        c.traceInOp("RS-", arg)
27✔
805
                                                case 'L', 'l':
×
806
                                                        c.traceInOp("LS-", arg)
×
807
                                                }
808
                                        }
809
                                        leafUnsub := c.op == 'L' || c.op == 'l'
128,638✔
810
                                        err = c.processRemoteUnsub(arg, leafUnsub)
128,638✔
811
                                case GATEWAY:
16,144✔
812
                                        if trace {
16,310✔
813
                                                c.traceInOp("RS-", arg)
166✔
814
                                        }
166✔
815
                                        err = c.processGatewayRUnsub(arg)
16,144✔
816
                                case LEAF:
1,783✔
817
                                        if trace {
1,783✔
818
                                                c.traceInOp("LS-", arg)
×
819
                                        }
×
820
                                        err = c.processLeafUnsub(arg)
1,783✔
821
                                }
822
                                if err != nil {
157,776✔
823
                                        return err
×
824
                                }
×
825
                                c.drop, c.as, c.state = 0, i+1, OP_START
157,776✔
826
                        default:
3,562,471✔
827
                                if c.argBuf != nil {
3,623,685✔
828
                                        c.argBuf = append(c.argBuf, b)
61,214✔
829
                                }
61,214✔
830
                        }
831
                case OP_PI:
21,312✔
832
                        switch b {
21,312✔
833
                        case 'N', 'n':
21,312✔
834
                                c.state = OP_PIN
21,312✔
835
                        default:
×
836
                                goto parseErr
×
837
                        }
838
                case OP_PIN:
21,312✔
839
                        switch b {
21,312✔
840
                        case 'G', 'g':
21,312✔
841
                                c.state = OP_PING
21,312✔
842
                        default:
×
843
                                goto parseErr
×
844
                        }
845
                case OP_PING:
42,624✔
846
                        switch b {
42,624✔
847
                        case '\n':
21,312✔
848
                                if trace {
21,728✔
849
                                        c.traceInOp("PING", nil)
416✔
850
                                }
416✔
851
                                c.processPing()
21,312✔
852
                                c.drop, c.state = 0, OP_START
21,312✔
853
                        }
854
                case OP_PO:
15,394✔
855
                        switch b {
15,394✔
856
                        case 'N', 'n':
15,394✔
857
                                c.state = OP_PON
15,394✔
858
                        default:
×
859
                                goto parseErr
×
860
                        }
861
                case OP_PON:
15,394✔
862
                        switch b {
15,394✔
863
                        case 'G', 'g':
15,394✔
864
                                c.state = OP_PONG
15,394✔
865
                        default:
×
866
                                goto parseErr
×
867
                        }
868
                case OP_PONG:
30,788✔
869
                        switch b {
30,788✔
870
                        case '\n':
15,394✔
871
                                if trace {
15,401✔
872
                                        c.traceInOp("PONG", nil)
7✔
873
                                }
7✔
874
                                c.processPong()
15,394✔
875
                                c.drop, c.state = 0, OP_START
15,394✔
876
                        }
877
                case OP_C:
25,604✔
878
                        switch b {
25,604✔
879
                        case 'O', 'o':
25,604✔
880
                                c.state = OP_CO
25,604✔
881
                        default:
×
882
                                goto parseErr
×
883
                        }
884
                case OP_CO:
25,604✔
885
                        switch b {
25,604✔
886
                        case 'N', 'n':
25,604✔
887
                                c.state = OP_CON
25,604✔
888
                        default:
×
889
                                goto parseErr
×
890
                        }
891
                case OP_CON:
25,604✔
892
                        switch b {
25,604✔
893
                        case 'N', 'n':
25,604✔
894
                                c.state = OP_CONN
25,604✔
895
                        default:
×
896
                                goto parseErr
×
897
                        }
898
                case OP_CONN:
25,604✔
899
                        switch b {
25,604✔
900
                        case 'E', 'e':
25,604✔
901
                                c.state = OP_CONNE
25,604✔
902
                        default:
×
903
                                goto parseErr
×
904
                        }
905
                case OP_CONNE:
25,604✔
906
                        switch b {
25,604✔
907
                        case 'C', 'c':
25,604✔
908
                                c.state = OP_CONNEC
25,604✔
909
                        default:
×
910
                                goto parseErr
×
911
                        }
912
                case OP_CONNEC:
25,604✔
913
                        switch b {
25,604✔
914
                        case 'T', 't':
25,604✔
915
                                c.state = OP_CONNECT
25,604✔
916
                        default:
×
917
                                goto parseErr
×
918
                        }
919
                case OP_CONNECT:
51,208✔
920
                        switch b {
51,208✔
921
                        case ' ', '\t':
25,604✔
922
                                continue
25,604✔
923
                        default:
25,604✔
924
                                c.state = CONNECT_ARG
25,604✔
925
                                c.as = i
25,604✔
926
                        }
927
                case CONNECT_ARG:
4,686,877✔
928
                        switch b {
4,686,877✔
929
                        case '\r':
25,604✔
930
                                c.drop = 1
25,604✔
931
                        case '\n':
25,604✔
932
                                var arg []byte
25,604✔
933
                                if c.argBuf != nil {
25,948✔
934
                                        arg = c.argBuf
344✔
935
                                        c.argBuf = nil
344✔
936
                                } else {
25,604✔
937
                                        arg = buf[c.as : i-c.drop]
25,260✔
938
                                }
25,260✔
939
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
25,604✔
940
                                        return err
×
941
                                }
×
942
                                if trace {
26,018✔
943
                                        c.traceInOp("CONNECT", removeSecretsFromTrace(arg))
414✔
944
                                }
414✔
945
                                if err := c.processConnect(arg); err != nil {
25,785✔
946
                                        return err
181✔
947
                                }
181✔
948
                                c.drop, c.state = 0, OP_START
25,423✔
949
                                // Reset notion on authSet
25,423✔
950
                                c.mu.Lock()
25,423✔
951
                                authSet = c.awaitingAuth()
25,423✔
952
                                c.mu.Unlock()
25,423✔
953
                        default:
4,635,669✔
954
                                if c.argBuf != nil {
4,738,818✔
955
                                        c.argBuf = append(c.argBuf, b)
103,149✔
956
                                }
103,149✔
957
                        }
958
                case OP_M:
5,517,212✔
959
                        switch b {
5,517,212✔
960
                        case 'S', 's':
5,517,212✔
961
                                c.state = OP_MS
5,517,212✔
962
                        default:
×
963
                                goto parseErr
×
964
                        }
965
                case OP_MS:
5,517,212✔
966
                        switch b {
5,517,212✔
967
                        case 'G', 'g':
5,517,212✔
968
                                c.state = OP_MSG
5,517,212✔
969
                        default:
×
970
                                goto parseErr
×
971
                        }
972
                case OP_MSG:
5,517,212✔
973
                        switch b {
5,517,212✔
974
                        case ' ', '\t':
5,517,212✔
975
                                c.state = OP_MSG_SPC
5,517,212✔
976
                        default:
×
977
                                goto parseErr
×
978
                        }
979
                case OP_MSG_SPC:
5,517,212✔
980
                        switch b {
5,517,212✔
981
                        case ' ', '\t':
×
982
                                continue
×
983
                        default:
5,517,212✔
984
                                c.pa.hdr = -1
5,517,212✔
985
                                c.state = MSG_ARG
5,517,212✔
986
                                c.as = i
5,517,212✔
987
                        }
988
                case MSG_ARG:
190,773,842✔
989
                        switch b {
190,773,842✔
990
                        case '\r':
5,517,211✔
991
                                c.drop = 1
5,517,211✔
992
                        case '\n':
5,517,211✔
993
                                var arg []byte
5,517,211✔
994
                                if c.argBuf != nil {
5,676,831✔
995
                                        arg = c.argBuf
159,620✔
996
                                        c.argBuf = nil
159,620✔
997
                                } else {
5,517,211✔
998
                                        arg = buf[c.as : i-c.drop]
5,357,591✔
999
                                }
5,357,591✔
1000
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
5,517,211✔
1001
                                        return err
×
1002
                                }
×
1003
                                var err error
5,517,211✔
1004
                                if c.kind == ROUTER || c.kind == GATEWAY {
11,030,318✔
1005
                                        switch c.op {
5,513,107✔
1006
                                        case 'R', 'r':
5,512,027✔
1007
                                                if trace {
5,516,078✔
1008
                                                        c.traceInOp("RMSG", arg)
4,051✔
1009
                                                }
4,051✔
1010
                                                err = c.processRoutedMsgArgs(arg)
5,512,027✔
1011
                                        case 'L', 'l':
1,080✔
1012
                                                if trace {
1,080✔
1013
                                                        c.traceInOp("LMSG", arg)
×
1014
                                                }
×
1015
                                                lmsg = true
1,080✔
1016
                                                err = c.processRoutedOriginClusterMsgArgs(arg)
1,080✔
1017
                                        }
1018
                                } else if c.kind == LEAF {
8,208✔
1019
                                        if trace {
4,104✔
1020
                                                c.traceInOp("LMSG", arg)
×
1021
                                        }
×
1022
                                        err = c.processLeafMsgArgs(arg)
4,104✔
1023
                                }
1024
                                if err != nil {
5,517,212✔
1025
                                        return err
1✔
1026
                                }
1✔
1027
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
5,517,210✔
1028

5,517,210✔
1029
                                // jump ahead with the index. If this overruns
5,517,210✔
1030
                                // what is left we fall out and process split
5,517,210✔
1031
                                // buffer.
5,517,210✔
1032
                                i = c.as + c.pa.size - LEN_CR_LF
5,517,210✔
1033
                        default:
179,739,420✔
1034
                                if c.argBuf != nil {
181,513,517✔
1035
                                        c.argBuf = append(c.argBuf, b)
1,774,097✔
1036
                                }
1,774,097✔
1037
                        }
1038
                case OP_I:
48,460✔
1039
                        switch b {
48,460✔
1040
                        case 'N', 'n':
48,460✔
1041
                                c.state = OP_IN
48,460✔
1042
                        default:
×
1043
                                goto parseErr
×
1044
                        }
1045
                case OP_IN:
48,460✔
1046
                        switch b {
48,460✔
1047
                        case 'F', 'f':
48,460✔
1048
                                c.state = OP_INF
48,460✔
1049
                        default:
×
1050
                                goto parseErr
×
1051
                        }
1052
                case OP_INF:
48,460✔
1053
                        switch b {
48,460✔
1054
                        case 'O', 'o':
48,460✔
1055
                                c.state = OP_INFO
48,460✔
1056
                        default:
×
1057
                                goto parseErr
×
1058
                        }
1059
                case OP_INFO:
96,920✔
1060
                        switch b {
96,920✔
1061
                        case ' ', '\t':
48,460✔
1062
                                continue
48,460✔
1063
                        default:
48,460✔
1064
                                c.state = INFO_ARG
48,460✔
1065
                                c.as = i
48,460✔
1066
                        }
1067
                case INFO_ARG:
18,554,309✔
1068
                        switch b {
18,554,309✔
1069
                        case '\r':
48,459✔
1070
                                c.drop = 1
48,459✔
1071
                        case '\n':
48,459✔
1072
                                var arg []byte
48,459✔
1073
                                if c.argBuf != nil {
52,795✔
1074
                                        arg = c.argBuf
4,336✔
1075
                                        c.argBuf = nil
4,336✔
1076
                                } else {
48,459✔
1077
                                        arg = buf[c.as : i-c.drop]
44,123✔
1078
                                }
44,123✔
1079
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
48,459✔
1080
                                        return err
×
1081
                                }
×
1082
                                if err := c.processInfo(arg); err != nil {
48,459✔
1083
                                        return err
×
1084
                                }
×
1085
                                c.drop, c.as, c.state = 0, i+1, OP_START
48,459✔
1086
                        default:
18,457,391✔
1087
                                if c.argBuf != nil {
19,002,010✔
1088
                                        c.argBuf = append(c.argBuf, b)
544,619✔
1089
                                }
544,619✔
1090
                        }
1091
                case OP_PLUS:
×
1092
                        switch b {
×
1093
                        case 'O', 'o':
×
1094
                                c.state = OP_PLUS_O
×
1095
                        default:
×
1096
                                goto parseErr
×
1097
                        }
1098
                case OP_PLUS_O:
×
1099
                        switch b {
×
1100
                        case 'K', 'k':
×
1101
                                c.state = OP_PLUS_OK
×
1102
                        default:
×
1103
                                goto parseErr
×
1104
                        }
1105
                case OP_PLUS_OK:
×
1106
                        switch b {
×
1107
                        case '\n':
×
1108
                                c.drop, c.state = 0, OP_START
×
1109
                        }
1110
                case OP_MINUS:
102✔
1111
                        switch b {
102✔
1112
                        case 'E', 'e':
102✔
1113
                                c.state = OP_MINUS_E
102✔
1114
                        default:
×
1115
                                goto parseErr
×
1116
                        }
1117
                case OP_MINUS_E:
102✔
1118
                        switch b {
102✔
1119
                        case 'R', 'r':
102✔
1120
                                c.state = OP_MINUS_ER
102✔
1121
                        default:
×
1122
                                goto parseErr
×
1123
                        }
1124
                case OP_MINUS_ER:
102✔
1125
                        switch b {
102✔
1126
                        case 'R', 'r':
102✔
1127
                                c.state = OP_MINUS_ERR
102✔
1128
                        default:
×
1129
                                goto parseErr
×
1130
                        }
1131
                case OP_MINUS_ERR:
102✔
1132
                        switch b {
102✔
1133
                        case ' ', '\t':
102✔
1134
                                c.state = OP_MINUS_ERR_SPC
102✔
1135
                        default:
×
1136
                                goto parseErr
×
1137
                        }
1138
                case OP_MINUS_ERR_SPC:
102✔
1139
                        switch b {
102✔
1140
                        case ' ', '\t':
×
1141
                                continue
×
1142
                        default:
102✔
1143
                                c.state = MINUS_ERR_ARG
102✔
1144
                                c.as = i
102✔
1145
                        }
1146
                case MINUS_ERR_ARG:
4,103✔
1147
                        switch b {
4,103✔
1148
                        case '\r':
102✔
1149
                                c.drop = 1
102✔
1150
                        case '\n':
102✔
1151
                                var arg []byte
102✔
1152
                                if c.argBuf != nil {
102✔
1153
                                        arg = c.argBuf
×
1154
                                        c.argBuf = nil
×
1155
                                } else {
102✔
1156
                                        arg = buf[c.as : i-c.drop]
102✔
1157
                                }
102✔
1158
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
102✔
1159
                                        return err
×
1160
                                }
×
1161
                                c.processErr(string(arg))
102✔
1162
                                c.drop, c.as, c.state = 0, i+1, OP_START
102✔
1163
                        default:
3,899✔
1164
                                if c.argBuf != nil {
3,899✔
1165
                                        c.argBuf = append(c.argBuf, b)
×
1166
                                }
×
1167
                        }
1168
                default:
×
1169
                        goto parseErr
×
1170
                }
1171
        }
1172

1173
        // Check for split buffer scenarios for any ARG state.
1174
        if c.state == SUB_ARG || c.state == UNSUB_ARG ||
6,087,075✔
1175
                c.state == PUB_ARG || c.state == HPUB_ARG ||
6,087,075✔
1176
                c.state == ASUB_ARG || c.state == AUSUB_ARG ||
6,087,075✔
1177
                c.state == MSG_ARG || c.state == HMSG_ARG ||
6,087,075✔
1178
                c.state == MINUS_ERR_ARG || c.state == CONNECT_ARG || c.state == INFO_ARG {
6,277,449✔
1179

190,374✔
1180
                // Setup a holder buffer to deal with split buffer scenario.
190,374✔
1181
                if c.argBuf == nil {
380,502✔
1182
                        c.argBuf = c.scratch[:0]
190,128✔
1183
                        c.argBuf = append(c.argBuf, buf[c.as:i-c.drop]...)
190,128✔
1184
                }
190,128✔
1185
                // Check for violations of control line length here. Note that this is not
1186
                // exact at all but the performance hit is too great to be precise, and
1187
                // catching here should prevent memory exhaustion attacks.
1188
                if err := c.overMaxControlLineLimit(c.argBuf, mcl); err != nil {
190,375✔
1189
                        return err
1✔
1190
                }
1✔
1191
        }
1192

1193
        // Check for split msg
1194
        if (c.state == MSG_PAYLOAD || c.state == MSG_END_R || c.state == MSG_END_N) && c.msgBuf == nil {
6,715,967✔
1195
                // We need to clone the pubArg if it is still referencing the
628,893✔
1196
                // read buffer and we are not able to process the msg.
628,893✔
1197

628,893✔
1198
                if c.argBuf == nil {
1,257,786✔
1199
                        // Works also for MSG_ARG, when message comes from ROUTE or GATEWAY.
628,893✔
1200
                        if err := c.clonePubArg(lmsg); err != nil {
628,893✔
1201
                                goto parseErr
×
1202
                        }
1203
                }
1204

1205
                // If we will overflow the scratch buffer, just create a
1206
                // new buffer to hold the split message.
1207
                if c.pa.size > cap(c.scratch)-len(c.argBuf) {
655,903✔
1208
                        lrem := len(buf[c.as:])
27,010✔
1209
                        // Consider it a protocol error when the remaining payload
27,010✔
1210
                        // is larger than the reported size for PUB. It can happen
27,010✔
1211
                        // when processing incomplete messages from rogue clients.
27,010✔
1212
                        if lrem > c.pa.size+LEN_CR_LF {
27,010✔
1213
                                goto parseErr
×
1214
                        }
1215
                        c.msgBuf = make([]byte, lrem, c.pa.size+LEN_CR_LF)
27,010✔
1216
                        copy(c.msgBuf, buf[c.as:])
27,010✔
1217
                } else {
601,883✔
1218
                        c.msgBuf = c.scratch[len(c.argBuf):len(c.argBuf)]
601,883✔
1219
                        c.msgBuf = append(c.msgBuf, (buf[c.as:])...)
601,883✔
1220
                }
601,883✔
1221
        }
1222

1223
        return nil
6,087,074✔
1224

6,087,074✔
1225
authErr:
6,087,074✔
1226
        c.authViolation()
12✔
1227
        return ErrAuthentication
12✔
1228

12✔
1229
parseErr:
12✔
1230
        c.sendErr("Unknown Protocol Operation")
6✔
1231
        snip := protoSnippet(i, PROTO_SNIPPET_SIZE, buf)
6✔
1232
        err := fmt.Errorf("%s parser ERROR, state=%d, i=%d: proto='%s...'", c.kindString(), c.state, i, snip)
6✔
1233
        return err
6✔
1234
}
1235

1236
func protoSnippet(start, max int, buf []byte) string {
7✔
1237
        stop := start + max
7✔
1238
        bufSize := len(buf)
7✔
1239
        if start >= bufSize {
7✔
1240
                return `""`
×
1241
        }
×
1242
        if stop > bufSize {
10✔
1243
                stop = bufSize - 1
3✔
1244
        }
3✔
1245
        return fmt.Sprintf("%q", buf[start:stop])
7✔
1246
}
1247

1248
// Check if the length of buffer `arg` is over the max control line limit `mcl`.
1249
// If so, an error is sent to the client and the connection is closed.
1250
// The error ErrMaxControlLine is returned.
1251
func (c *client) overMaxControlLineLimit(arg []byte, mcl int32) error {
14,746,557✔
1252
        if c.kind != CLIENT {
21,728,947✔
1253
                return nil
6,982,390✔
1254
        }
6,982,390✔
1255
        if len(arg) > int(mcl) {
7,764,168✔
1256
                err := NewErrorCtx(ErrMaxControlLine, "State %d, max_control_line %d, Buffer len %d (snip: %s...)",
1✔
1257
                        c.state, int(mcl), len(c.argBuf), protoSnippet(0, MAX_CONTROL_LINE_SNIPPET_SIZE, arg))
1✔
1258
                c.sendErr(err.Error())
1✔
1259
                c.closeConnection(MaxControlLineExceeded)
1✔
1260
                return err
1✔
1261
        }
1✔
1262
        return nil
7,764,166✔
1263
}
1264

1265
// clonePubArg is used when the split buffer scenario has the pubArg in the existing read buffer, but
1266
// we need to hold onto it into the next read.
1267
func (c *client) clonePubArg(lmsg bool) error {
628,893✔
1268
        // Just copy and re-process original arg buffer.
628,893✔
1269
        c.argBuf = c.scratch[:0]
628,893✔
1270
        c.argBuf = append(c.argBuf, c.pa.arg...)
628,893✔
1271

628,893✔
1272
        switch c.kind {
628,893✔
1273
        case ROUTER, GATEWAY:
608,784✔
1274
                if lmsg {
609,047✔
1275
                        return c.processRoutedOriginClusterMsgArgs(c.argBuf)
263✔
1276
                }
263✔
1277
                if c.pa.hdr < 0 {
1,132,560✔
1278
                        return c.processRoutedMsgArgs(c.argBuf)
524,039✔
1279
                } else {
608,521✔
1280
                        return c.processRoutedHeaderMsgArgs(c.argBuf)
84,482✔
1281
                }
84,482✔
1282
        case LEAF:
560✔
1283
                if c.pa.hdr < 0 {
1,116✔
1284
                        return c.processLeafMsgArgs(c.argBuf)
556✔
1285
                } else {
560✔
1286
                        return c.processLeafHeaderMsgArgs(c.argBuf)
4✔
1287
                }
4✔
1288
        default:
19,549✔
1289
                if c.pa.hdr < 0 {
38,543✔
1290
                        return c.processPub(c.argBuf)
18,994✔
1291
                } else {
19,549✔
1292
                        return c.processHeaderPub(c.argBuf, nil)
555✔
1293
                }
555✔
1294
        }
1295
}
1296

1297
func (ps *parseState) getHeader() http.Header {
831✔
1298
        if ps.header == nil {
1,662✔
1299
                if hdr := ps.pa.hdr; hdr > 0 {
902✔
1300
                        reader := bufio.NewReader(bytes.NewReader(ps.msgBuf[0:hdr]))
71✔
1301
                        tp := textproto.NewReader(reader)
71✔
1302
                        tp.ReadLine() // skip over first line, contains version
71✔
1303
                        if mimeHeader, err := tp.ReadMIMEHeader(); err == nil {
142✔
1304
                                ps.header = http.Header(mimeHeader)
71✔
1305
                        }
71✔
1306
                }
1307
        }
1308
        return ps.header
831✔
1309
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc