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

nats-io / nats-server / 20612310899

22 Dec 2025 02:03PM UTC coverage: 84.215% (-0.3%) from 84.522%
20612310899

push

github

web-flow
[FIXED] Deleted raft node's stream is revived (#7668)

Related to https://github.com/nats-io/nats-server/pull/7025

If the Raft node of the stream was deleted while the stream was stalled
on upper-layer catchup, it would wrongfully revive the stream. We now
mark the Raft node as deleted so we can ensure the stream monitor
goroutine quits and doesn't get revived.

Signed-off-by: Maurice van Veen <github@mauricevanveen.com>

73722 of 87540 relevant lines covered (84.22%)

349341.0 hits per line

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

90.96
/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,918,239✔
137
        // Branch out to mqtt clients. c.mqtt is immutable, but should it become
6,918,239✔
138
        // an issue (say data race detection), we could branch outside in readLoop
6,918,239✔
139
        if c.isMqtt() {
6,920,624✔
140
                return c.mqttParse(buf)
2,385✔
141
        }
2,385✔
142
        var i int
6,915,854✔
143
        var b byte
6,915,854✔
144
        var lmsg bool
6,915,854✔
145

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

6,915,854✔
155
        // Move to loop instead of range syntax to allow jumping of i
6,915,854✔
156
        for i = 0; i < len(buf); i++ {
655,342,043✔
157
                b = buf[i]
648,426,189✔
158

648,426,189✔
159
                switch c.state {
648,426,189✔
160
                case OP_START:
18,328,024✔
161
                        c.op = b
18,328,024✔
162
                        if b != 'C' && b != 'c' {
36,613,062✔
163
                                if authSet {
18,285,052✔
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 {
18,285,026✔
190
                                        // Use auth violation since no CONNECT was sent.
×
191
                                        // It could be a parseErr too.
×
192
                                        goto authErr
×
193
                                }
194
                        }
195
                        switch b {
18,328,012✔
196
                        case 'P', 'p':
9,933,058✔
197
                                c.state = OP_P
9,933,058✔
198
                        case 'H', 'h':
692,365✔
199
                                c.state = OP_H
692,365✔
200
                        case 'S', 's':
133,540✔
201
                                c.state = OP_S
133,540✔
202
                        case 'U', 'u':
12,994✔
203
                                c.state = OP_U
12,994✔
204
                        case 'R', 'r':
7,254,102✔
205
                                if c.kind == CLIENT {
7,254,102✔
206
                                        goto parseErr
×
207
                                } else {
7,254,102✔
208
                                        c.state = OP_R
7,254,102✔
209
                                }
7,254,102✔
210
                        case 'L', 'l':
172,949✔
211
                                if c.kind != LEAF && c.kind != ROUTER {
172,949✔
212
                                        goto parseErr
×
213
                                } else {
172,949✔
214
                                        c.state = OP_L
172,949✔
215
                                }
172,949✔
216
                        case 'A', 'a':
32✔
217
                                if c.kind == CLIENT {
32✔
218
                                        goto parseErr
×
219
                                } else {
32✔
220
                                        c.state = OP_A
32✔
221
                                }
32✔
222
                        case 'C', 'c':
42,986✔
223
                                c.state = OP_C
42,986✔
224
                        case 'I', 'i':
85,784✔
225
                                c.state = OP_I
85,784✔
226
                        case '+':
3✔
227
                                c.state = OP_PLUS
3✔
228
                        case '-':
187✔
229
                                c.state = OP_MINUS
187✔
230
                        default:
12✔
231
                                goto parseErr
12✔
232
                        }
233
                case OP_H:
692,365✔
234
                        switch b {
692,365✔
235
                        case 'P', 'p':
321,387✔
236
                                c.state = OP_HP
321,387✔
237
                        case 'M', 'm':
370,978✔
238
                                c.state = OP_HM
370,978✔
239
                        default:
×
240
                                goto parseErr
×
241
                        }
242
                case OP_HP:
321,387✔
243
                        switch b {
321,387✔
244
                        case 'U', 'u':
321,387✔
245
                                c.state = OP_HPU
321,387✔
246
                        default:
×
247
                                goto parseErr
×
248
                        }
249
                case OP_HPU:
321,387✔
250
                        switch b {
321,387✔
251
                        case 'B', 'b':
321,387✔
252
                                c.state = OP_HPUB
321,387✔
253
                        default:
×
254
                                goto parseErr
×
255
                        }
256
                case OP_HPUB:
321,387✔
257
                        switch b {
321,387✔
258
                        case ' ', '\t':
321,387✔
259
                                c.state = OP_HPUB_SPC
321,387✔
260
                        default:
×
261
                                goto parseErr
×
262
                        }
263
                case OP_HPUB_SPC:
321,387✔
264
                        switch b {
321,387✔
265
                        case ' ', '\t':
×
266
                                continue
×
267
                        default:
321,387✔
268
                                c.pa.hdr = 0
321,387✔
269
                                c.state = HPUB_ARG
321,387✔
270
                                c.as = i
321,387✔
271
                        }
272
                case HPUB_ARG:
11,419,115✔
273
                        switch b {
11,419,115✔
274
                        case '\r':
321,387✔
275
                                c.drop = 1
321,387✔
276
                        case '\n':
321,387✔
277
                                var arg []byte
321,387✔
278
                                if c.argBuf != nil {
321,473✔
279
                                        arg = c.argBuf
86✔
280
                                        c.argBuf = nil
86✔
281
                                } else {
321,387✔
282
                                        arg = buf[c.as : i-c.drop]
321,301✔
283
                                }
321,301✔
284
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
321,387✔
285
                                        return err
×
286
                                }
×
287
                                if trace {
369,630✔
288
                                        c.traceInOp("HPUB", arg)
48,243✔
289
                                }
48,243✔
290
                                var remaining []byte
321,387✔
291
                                if i < len(buf) {
642,774✔
292
                                        remaining = buf[i+1:]
321,387✔
293
                                }
321,387✔
294
                                if err := c.processHeaderPub(arg, remaining); err != nil {
321,391✔
295
                                        return err
4✔
296
                                }
4✔
297

298
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
321,383✔
299
                                // If we don't have a saved buffer then jump ahead with
321,383✔
300
                                // the index. If this overruns what is left we fall out
321,383✔
301
                                // and process split buffer.
321,383✔
302
                                if c.msgBuf == nil {
642,766✔
303
                                        i = c.as + c.pa.size - LEN_CR_LF
321,383✔
304
                                }
321,383✔
305
                        default:
10,776,341✔
306
                                if c.argBuf != nil {
10,777,370✔
307
                                        c.argBuf = append(c.argBuf, b)
1,029✔
308
                                }
1,029✔
309
                        }
310
                case OP_HM:
370,978✔
311
                        switch b {
370,978✔
312
                        case 'S', 's':
370,978✔
313
                                c.state = OP_HMS
370,978✔
314
                        default:
×
315
                                goto parseErr
×
316
                        }
317
                case OP_HMS:
370,978✔
318
                        switch b {
370,978✔
319
                        case 'G', 'g':
370,978✔
320
                                c.state = OP_HMSG
370,978✔
321
                        default:
×
322
                                goto parseErr
×
323
                        }
324
                case OP_HMSG:
370,978✔
325
                        switch b {
370,978✔
326
                        case ' ', '\t':
370,978✔
327
                                c.state = OP_HMSG_SPC
370,978✔
328
                        default:
×
329
                                goto parseErr
×
330
                        }
331
                case OP_HMSG_SPC:
370,978✔
332
                        switch b {
370,978✔
333
                        case ' ', '\t':
×
334
                                continue
×
335
                        default:
370,978✔
336
                                c.pa.hdr = 0
370,978✔
337
                                c.state = HMSG_ARG
370,978✔
338
                                c.as = i
370,978✔
339
                        }
340
                case HMSG_ARG:
28,903,392✔
341
                        switch b {
28,903,392✔
342
                        case '\r':
370,978✔
343
                                c.drop = 1
370,978✔
344
                        case '\n':
370,978✔
345
                                var arg []byte
370,978✔
346
                                if c.argBuf != nil {
373,578✔
347
                                        arg = c.argBuf
2,600✔
348
                                        c.argBuf = nil
2,600✔
349
                                } else {
370,978✔
350
                                        arg = buf[c.as : i-c.drop]
368,378✔
351
                                }
368,378✔
352
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
370,978✔
353
                                        return err
×
354
                                }
×
355
                                var err error
370,978✔
356
                                if c.kind == ROUTER || c.kind == GATEWAY {
741,558✔
357
                                        if trace {
372,737✔
358
                                                c.traceInOp("HMSG", arg)
2,157✔
359
                                        }
2,157✔
360
                                        err = c.processRoutedHeaderMsgArgs(arg)
370,580✔
361
                                } else if c.kind == LEAF {
796✔
362
                                        if trace {
549✔
363
                                                c.traceInOp("HMSG", arg)
151✔
364
                                        }
151✔
365
                                        err = c.processLeafHeaderMsgArgs(arg)
398✔
366
                                }
367
                                if err != nil {
370,979✔
368
                                        return err
1✔
369
                                }
1✔
370
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
370,977✔
371

370,977✔
372
                                // jump ahead with the index. If this overruns
370,977✔
373
                                // what is left we fall out and process split
370,977✔
374
                                // buffer.
370,977✔
375
                                i = c.as + c.pa.size - LEN_CR_LF
370,977✔
376
                        default:
28,161,436✔
377
                                if c.argBuf != nil {
28,268,698✔
378
                                        c.argBuf = append(c.argBuf, b)
107,262✔
379
                                }
107,262✔
380
                        }
381
                case OP_P:
9,933,057✔
382
                        switch b {
9,933,057✔
383
                        case 'U', 'u':
9,864,798✔
384
                                c.state = OP_PU
9,864,798✔
385
                        case 'I', 'i':
40,036✔
386
                                c.state = OP_PI
40,036✔
387
                        case 'O', 'o':
28,222✔
388
                                c.state = OP_PO
28,222✔
389
                        default:
1✔
390
                                goto parseErr
1✔
391
                        }
392
                case OP_PU:
9,864,797✔
393
                        switch b {
9,864,797✔
394
                        case 'B', 'b':
9,864,796✔
395
                                c.state = OP_PUB
9,864,796✔
396
                        default:
1✔
397
                                goto parseErr
1✔
398
                        }
399
                case OP_PUB:
9,864,796✔
400
                        switch b {
9,864,796✔
401
                        case ' ', '\t':
9,864,795✔
402
                                c.state = OP_PUB_SPC
9,864,795✔
403
                        default:
1✔
404
                                goto parseErr
1✔
405
                        }
406
                case OP_PUB_SPC:
9,864,795✔
407
                        switch b {
9,864,795✔
408
                        case ' ', '\t':
1✔
409
                                continue
1✔
410
                        default:
9,864,794✔
411
                                c.pa.hdr = -1
9,864,794✔
412
                                c.state = PUB_ARG
9,864,794✔
413
                                c.as = i
9,864,794✔
414
                        }
415
                case PUB_ARG:
134,994,328✔
416
                        switch b {
134,994,328✔
417
                        case '\r':
9,864,786✔
418
                                c.drop = 1
9,864,786✔
419
                        case '\n':
9,864,786✔
420
                                var arg []byte
9,864,786✔
421
                                if c.argBuf != nil {
9,893,509✔
422
                                        arg = c.argBuf
28,723✔
423
                                        c.argBuf = nil
28,723✔
424
                                } else {
9,864,786✔
425
                                        arg = buf[c.as : i-c.drop]
9,836,063✔
426
                                }
9,836,063✔
427
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
9,864,787✔
428
                                        return err
1✔
429
                                }
1✔
430
                                if trace {
10,215,155✔
431
                                        c.traceInOp("PUB", arg)
350,370✔
432
                                }
350,370✔
433
                                if err := c.processPub(arg); err != nil {
9,864,796✔
434
                                        return err
11✔
435
                                }
11✔
436

437
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
9,864,774✔
438
                                // If we don't have a saved buffer then jump ahead with
9,864,774✔
439
                                // the index. If this overruns what is left we fall out
9,864,774✔
440
                                // and process split buffer.
9,864,774✔
441
                                if c.msgBuf == nil {
19,729,548✔
442
                                        i = c.as + c.pa.size - LEN_CR_LF
9,864,774✔
443
                                }
9,864,774✔
444
                        default:
115,264,756✔
445
                                if c.argBuf != nil {
115,798,548✔
446
                                        c.argBuf = append(c.argBuf, b)
533,792✔
447
                                }
533,792✔
448
                        }
449
                case MSG_PAYLOAD:
17,078,661✔
450
                        if c.msgBuf != nil {
18,023,315✔
451
                                // copy as much as we can to the buffer and skip ahead.
944,654✔
452
                                toCopy := c.pa.size - len(c.msgBuf)
944,654✔
453
                                avail := len(buf) - i
944,654✔
454
                                if avail < toCopy {
1,255,296✔
455
                                        toCopy = avail
310,642✔
456
                                }
310,642✔
457
                                if toCopy > 0 {
1,889,308✔
458
                                        start := len(c.msgBuf)
944,654✔
459
                                        // This is needed for copy to work.
944,654✔
460
                                        c.msgBuf = c.msgBuf[:start+toCopy]
944,654✔
461
                                        copy(c.msgBuf[start:], buf[i:i+toCopy])
944,654✔
462
                                        // Update our index
944,654✔
463
                                        i = (i + toCopy) - 1
944,654✔
464
                                } else {
944,654✔
465
                                        // Fall back to append if needed.
×
466
                                        c.msgBuf = append(c.msgBuf, b)
×
467
                                }
×
468
                                if len(c.msgBuf) >= c.pa.size {
1,578,666✔
469
                                        c.state = MSG_END_R
634,012✔
470
                                }
634,012✔
471
                        } else if i-c.as+1 >= c.pa.size {
32,268,014✔
472
                                c.state = MSG_END_R
16,134,007✔
473
                        }
16,134,007✔
474
                case MSG_END_R:
16,768,019✔
475
                        if b != '\r' {
16,768,022✔
476
                                goto parseErr
3✔
477
                        }
478
                        if c.msgBuf != nil {
17,417,083✔
479
                                c.msgBuf = append(c.msgBuf, b)
649,067✔
480
                        }
649,067✔
481
                        c.state = MSG_END_N
16,768,016✔
482
                case MSG_END_N:
16,768,003✔
483
                        if b != '\n' {
16,768,004✔
484
                                goto parseErr
1✔
485
                        }
486
                        if c.msgBuf != nil {
17,418,920✔
487
                                c.msgBuf = append(c.msgBuf, b)
650,918✔
488
                        } else {
16,768,002✔
489
                                c.msgBuf = buf[c.as : i+1]
16,117,084✔
490
                        }
16,117,084✔
491

492
                        var mt *msgTrace
16,768,002✔
493
                        if c.pa.hdr > 0 {
17,460,804✔
494
                                mt = c.initMsgTrace()
692,802✔
495
                        }
692,802✔
496
                        // Check for mappings.
497
                        if (c.kind == CLIENT || c.kind == LEAF) && c.in.flags.isSet(hasMappings) {
16,800,807✔
498
                                changed := c.selectMappedSubject()
32,805✔
499
                                if changed {
45,394✔
500
                                        if trace {
12,664✔
501
                                                c.traceInOp("MAPPING", []byte(fmt.Sprintf("%s -> %s", c.pa.mapped, c.pa.subject)))
75✔
502
                                        }
75✔
503
                                        // c.pa.subject is the subject the original is now mapped to.
504
                                        mt.addSubjectMappingEvent(c.pa.subject)
12,589✔
505
                                }
506
                        }
507
                        if trace {
17,175,865✔
508
                                c.traceMsg(c.msgBuf)
407,863✔
509
                        }
407,863✔
510

511
                        c.processInboundMsg(c.msgBuf)
16,768,002✔
512

16,768,002✔
513
                        mt.sendEvent()
16,768,002✔
514
                        c.argBuf, c.msgBuf, c.header = nil, nil, nil
16,768,002✔
515
                        c.drop, c.as, c.state = 0, i+1, OP_START
16,768,002✔
516
                        // Drop all pub args
16,768,002✔
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
16,768,002✔
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
16,768,002✔
519
                        c.pa.trace = nil
16,768,002✔
520
                        c.pa.delivered = false
16,768,002✔
521
                        lmsg = false
16,768,002✔
522
                case OP_A:
32✔
523
                        switch b {
32✔
524
                        case '+':
4✔
525
                                c.state = OP_ASUB
4✔
526
                        case '-', 'u':
28✔
527
                                c.state = OP_AUSUB
28✔
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:
28✔
574
                        switch b {
28✔
575
                        case ' ', '\t':
28✔
576
                                c.state = OP_AUSUB_SPC
28✔
577
                        default:
×
578
                                goto parseErr
×
579
                        }
580
                case OP_AUSUB_SPC:
28✔
581
                        switch b {
28✔
582
                        case ' ', '\t':
×
583
                                continue
×
584
                        default:
28✔
585
                                c.state = AUSUB_ARG
28✔
586
                                c.as = i
28✔
587
                        }
588
                case AUSUB_ARG:
142✔
589
                        switch b {
142✔
590
                        case '\r':
28✔
591
                                c.drop = 1
28✔
592
                        case '\n':
28✔
593
                                var arg []byte
28✔
594
                                if c.argBuf != nil {
28✔
595
                                        arg = c.argBuf
×
596
                                        c.argBuf = nil
×
597
                                } else {
28✔
598
                                        arg = buf[c.as : i-c.drop]
28✔
599
                                }
28✔
600
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
28✔
601
                                        return err
×
602
                                }
×
603
                                if trace {
45✔
604
                                        c.traceInOp("A-", arg)
17✔
605
                                }
17✔
606
                                c.processAccountUnsub(arg)
28✔
607
                                c.drop, c.as, c.state = 0, i+1, OP_START
28✔
608
                        default:
86✔
609
                                if c.argBuf != nil {
86✔
610
                                        c.argBuf = append(c.argBuf, b)
×
611
                                }
×
612
                        }
613
                case OP_S:
133,540✔
614
                        switch b {
133,540✔
615
                        case 'U', 'u':
133,539✔
616
                                c.state = OP_SU
133,539✔
617
                        default:
1✔
618
                                goto parseErr
1✔
619
                        }
620
                case OP_SU:
133,539✔
621
                        switch b {
133,539✔
622
                        case 'B', 'b':
133,538✔
623
                                c.state = OP_SUB
133,538✔
624
                        default:
1✔
625
                                goto parseErr
1✔
626
                        }
627
                case OP_SUB:
1,141,712✔
628
                        switch b {
1,141,712✔
629
                        case ' ', '\t':
1,141,710✔
630
                                c.state = OP_SUB_SPC
1,141,710✔
631
                        default:
2✔
632
                                goto parseErr
2✔
633
                        }
634
                case OP_SUB_SPC:
1,141,710✔
635
                        switch b {
1,141,710✔
636
                        case ' ', '\t':
1✔
637
                                continue
1✔
638
                        default:
1,141,709✔
639
                                c.state = SUB_ARG
1,141,709✔
640
                                c.as = i
1,141,709✔
641
                        }
642
                case SUB_ARG:
42,559,331✔
643
                        switch b {
42,559,331✔
644
                        case '\r':
1,141,691✔
645
                                c.drop = 1
1,141,691✔
646
                        case '\n':
1,141,691✔
647
                                var arg []byte
1,141,691✔
648
                                if c.argBuf != nil {
1,168,240✔
649
                                        arg = c.argBuf
26,549✔
650
                                        c.argBuf = nil
26,549✔
651
                                } else {
1,141,691✔
652
                                        arg = buf[c.as : i-c.drop]
1,115,142✔
653
                                }
1,115,142✔
654
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
1,141,691✔
655
                                        return err
×
656
                                }
×
657
                                var err error
1,141,691✔
658

1,141,691✔
659
                                switch c.kind {
1,141,691✔
660
                                case CLIENT:
133,534✔
661
                                        if trace {
144,064✔
662
                                                c.traceInOp("SUB", arg)
10,530✔
663
                                        }
10,530✔
664
                                        err = c.parseSub(arg, false)
133,534✔
665
                                case ROUTER:
816,047✔
666
                                        switch c.op {
816,047✔
667
                                        case 'R', 'r':
804,258✔
668
                                                if trace {
807,124✔
669
                                                        c.traceInOp("RS+", arg)
2,866✔
670
                                                }
2,866✔
671
                                                err = c.processRemoteSub(arg, false)
804,258✔
672
                                        case 'L', 'l':
11,789✔
673
                                                if trace {
11,983✔
674
                                                        c.traceInOp("LS+", arg)
194✔
675
                                                }
194✔
676
                                                err = c.processRemoteSub(arg, true)
11,789✔
677
                                        }
678
                                case GATEWAY:
159,619✔
679
                                        if trace {
160,034✔
680
                                                c.traceInOp("RS+", arg)
415✔
681
                                        }
415✔
682
                                        err = c.processGatewayRSub(arg)
159,619✔
683
                                case LEAF:
32,491✔
684
                                        if trace {
32,612✔
685
                                                c.traceInOp("LS+", arg)
121✔
686
                                        }
121✔
687
                                        err = c.processLeafSub(arg)
32,491✔
688
                                }
689
                                if err != nil {
1,141,694✔
690
                                        return err
3✔
691
                                }
3✔
692
                                c.drop, c.as, c.state = 0, i+1, OP_START
1,141,688✔
693
                        default:
40,275,949✔
694
                                if c.argBuf != nil {
40,927,671✔
695
                                        c.argBuf = append(c.argBuf, b)
651,722✔
696
                                }
651,722✔
697
                        }
698
                case OP_L:
172,949✔
699
                        switch b {
172,949✔
700
                        case 'S', 's':
58,644✔
701
                                c.state = OP_LS
58,644✔
702
                        case 'M', 'm':
114,305✔
703
                                c.state = OP_M
114,305✔
704
                        default:
×
705
                                goto parseErr
×
706
                        }
707
                case OP_LS:
58,644✔
708
                        switch b {
58,644✔
709
                        case '+':
44,280✔
710
                                c.state = OP_SUB
44,280✔
711
                        case '-':
14,364✔
712
                                c.state = OP_UNSUB
14,364✔
713
                        default:
×
714
                                goto parseErr
×
715
                        }
716
                case OP_R:
7,254,102✔
717
                        switch b {
7,254,102✔
718
                        case 'S', 's':
1,157,489✔
719
                                c.state = OP_RS
1,157,489✔
720
                        case 'M', 'm':
6,096,613✔
721
                                c.state = OP_M
6,096,613✔
722
                        default:
×
723
                                goto parseErr
×
724
                        }
725
                case OP_RS:
1,157,489✔
726
                        switch b {
1,157,489✔
727
                        case '+':
963,894✔
728
                                c.state = OP_SUB
963,894✔
729
                        case '-':
193,595✔
730
                                c.state = OP_UNSUB
193,595✔
731
                        default:
×
732
                                goto parseErr
×
733
                        }
734
                case OP_U:
12,994✔
735
                        switch b {
12,994✔
736
                        case 'N', 'n':
12,993✔
737
                                c.state = OP_UN
12,993✔
738
                        default:
1✔
739
                                goto parseErr
1✔
740
                        }
741
                case OP_UN:
12,993✔
742
                        switch b {
12,993✔
743
                        case 'S', 's':
12,992✔
744
                                c.state = OP_UNS
12,992✔
745
                        default:
1✔
746
                                goto parseErr
1✔
747
                        }
748
                case OP_UNS:
12,992✔
749
                        switch b {
12,992✔
750
                        case 'U', 'u':
12,991✔
751
                                c.state = OP_UNSU
12,991✔
752
                        default:
1✔
753
                                goto parseErr
1✔
754
                        }
755
                case OP_UNSU:
12,991✔
756
                        switch b {
12,991✔
757
                        case 'B', 'b':
12,990✔
758
                                c.state = OP_UNSUB
12,990✔
759
                        default:
1✔
760
                                goto parseErr
1✔
761
                        }
762
                case OP_UNSUB:
220,949✔
763
                        switch b {
220,949✔
764
                        case ' ', '\t':
220,943✔
765
                                c.state = OP_UNSUB_SPC
220,943✔
766
                        default:
6✔
767
                                goto parseErr
6✔
768
                        }
769
                case OP_UNSUB_SPC:
220,960✔
770
                        switch b {
220,960✔
771
                        case ' ', '\t':
17✔
772
                                continue
17✔
773
                        default:
220,943✔
774
                                c.state = UNSUB_ARG
220,943✔
775
                                c.as = i
220,943✔
776
                        }
777
                case UNSUB_ARG:
6,184,479✔
778
                        switch b {
6,184,479✔
779
                        case '\r':
220,938✔
780
                                c.drop = 1
220,938✔
781
                        case '\n':
220,940✔
782
                                var arg []byte
220,940✔
783
                                if c.argBuf != nil {
227,574✔
784
                                        arg = c.argBuf
6,634✔
785
                                        c.argBuf = nil
6,634✔
786
                                } else {
220,940✔
787
                                        arg = buf[c.as : i-c.drop]
214,306✔
788
                                }
214,306✔
789
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
220,940✔
790
                                        return err
×
791
                                }
×
792
                                var err error
220,940✔
793

220,940✔
794
                                switch c.kind {
220,940✔
795
                                case CLIENT:
12,984✔
796
                                        if trace {
23,265✔
797
                                                c.traceInOp("UNSUB", arg)
10,281✔
798
                                        }
10,281✔
799
                                        err = c.processUnsub(arg)
12,984✔
800
                                case ROUTER:
163,816✔
801
                                        if trace && c.srv != nil {
164,174✔
802
                                                switch c.op {
358✔
803
                                                case 'R', 'r':
164✔
804
                                                        c.traceInOp("RS-", arg)
164✔
805
                                                case 'L', 'l':
194✔
806
                                                        c.traceInOp("LS-", arg)
194✔
807
                                                }
808
                                        }
809
                                        leafUnsub := c.op == 'L' || c.op == 'l'
163,816✔
810
                                        err = c.processRemoteUnsub(arg, leafUnsub)
163,816✔
811
                                case GATEWAY:
40,760✔
812
                                        if trace {
41,951✔
813
                                                c.traceInOp("RS-", arg)
1,191✔
814
                                        }
1,191✔
815
                                        err = c.processGatewayRUnsub(arg)
40,760✔
816
                                case LEAF:
3,380✔
817
                                        if trace {
3,380✔
818
                                                c.traceInOp("LS-", arg)
×
819
                                        }
×
820
                                        err = c.processLeafUnsub(arg)
3,380✔
821
                                }
822
                                if err != nil {
220,942✔
823
                                        return err
2✔
824
                                }
2✔
825
                                c.drop, c.as, c.state = 0, i+1, OP_START
220,938✔
826
                        default:
5,742,601✔
827
                                if c.argBuf != nil {
5,867,223✔
828
                                        c.argBuf = append(c.argBuf, b)
124,622✔
829
                                }
124,622✔
830
                        }
831
                case OP_PI:
40,036✔
832
                        switch b {
40,036✔
833
                        case 'N', 'n':
40,035✔
834
                                c.state = OP_PIN
40,035✔
835
                        default:
1✔
836
                                goto parseErr
1✔
837
                        }
838
                case OP_PIN:
40,035✔
839
                        switch b {
40,035✔
840
                        case 'G', 'g':
40,034✔
841
                                c.state = OP_PING
40,034✔
842
                        default:
1✔
843
                                goto parseErr
1✔
844
                        }
845
                case OP_PING:
80,073✔
846
                        switch b {
80,073✔
847
                        case '\n':
40,033✔
848
                                if trace {
40,668✔
849
                                        c.traceInOp("PING", nil)
635✔
850
                                }
635✔
851
                                c.processPing()
40,033✔
852
                                c.drop, c.state = 0, OP_START
40,033✔
853
                        }
854
                case OP_PO:
28,222✔
855
                        switch b {
28,222✔
856
                        case 'N', 'n':
28,221✔
857
                                c.state = OP_PON
28,221✔
858
                        default:
1✔
859
                                goto parseErr
1✔
860
                        }
861
                case OP_PON:
28,221✔
862
                        switch b {
28,221✔
863
                        case 'G', 'g':
28,220✔
864
                                c.state = OP_PONG
28,220✔
865
                        default:
1✔
866
                                goto parseErr
1✔
867
                        }
868
                case OP_PONG:
56,445✔
869
                        switch b {
56,445✔
870
                        case '\n':
28,219✔
871
                                if trace {
28,269✔
872
                                        c.traceInOp("PONG", nil)
50✔
873
                                }
50✔
874
                                c.processPong()
28,219✔
875
                                c.drop, c.state = 0, OP_START
28,219✔
876
                        }
877
                case OP_C:
42,986✔
878
                        switch b {
42,986✔
879
                        case 'O', 'o':
42,985✔
880
                                c.state = OP_CO
42,985✔
881
                        default:
1✔
882
                                goto parseErr
1✔
883
                        }
884
                case OP_CO:
42,985✔
885
                        switch b {
42,985✔
886
                        case 'N', 'n':
42,984✔
887
                                c.state = OP_CON
42,984✔
888
                        default:
1✔
889
                                goto parseErr
1✔
890
                        }
891
                case OP_CON:
42,984✔
892
                        switch b {
42,984✔
893
                        case 'N', 'n':
42,983✔
894
                                c.state = OP_CONN
42,983✔
895
                        default:
1✔
896
                                goto parseErr
1✔
897
                        }
898
                case OP_CONN:
42,983✔
899
                        switch b {
42,983✔
900
                        case 'E', 'e':
42,982✔
901
                                c.state = OP_CONNE
42,982✔
902
                        default:
1✔
903
                                goto parseErr
1✔
904
                        }
905
                case OP_CONNE:
42,982✔
906
                        switch b {
42,982✔
907
                        case 'C', 'c':
42,981✔
908
                                c.state = OP_CONNEC
42,981✔
909
                        default:
1✔
910
                                goto parseErr
1✔
911
                        }
912
                case OP_CONNEC:
42,981✔
913
                        switch b {
42,981✔
914
                        case 'T', 't':
42,979✔
915
                                c.state = OP_CONNECT
42,979✔
916
                        default:
2✔
917
                                goto parseErr
2✔
918
                        }
919
                case OP_CONNECT:
85,958✔
920
                        switch b {
85,958✔
921
                        case ' ', '\t':
42,979✔
922
                                continue
42,979✔
923
                        default:
42,979✔
924
                                c.state = CONNECT_ARG
42,979✔
925
                                c.as = i
42,979✔
926
                        }
927
                case CONNECT_ARG:
9,123,594✔
928
                        switch b {
9,123,594✔
929
                        case '\r':
42,978✔
930
                                c.drop = 1
42,978✔
931
                        case '\n':
42,979✔
932
                                var arg []byte
42,979✔
933
                                if c.argBuf != nil {
45,400✔
934
                                        arg = c.argBuf
2,421✔
935
                                        c.argBuf = nil
2,421✔
936
                                } else {
42,979✔
937
                                        arg = buf[c.as : i-c.drop]
40,558✔
938
                                }
40,558✔
939
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
42,979✔
940
                                        return err
×
941
                                }
×
942
                                if trace {
43,665✔
943
                                        c.traceInOp("CONNECT", removeSecretsFromTrace(arg))
686✔
944
                                }
686✔
945
                                if err := c.processConnect(arg); err != nil {
43,303✔
946
                                        return err
324✔
947
                                }
324✔
948
                                c.drop, c.state = 0, OP_START
42,655✔
949
                                // Reset notion on authSet
42,655✔
950
                                c.mu.Lock()
42,655✔
951
                                authSet = c.awaitingAuth()
42,655✔
952
                                c.mu.Unlock()
42,655✔
953
                        default:
9,037,637✔
954
                                if c.argBuf != nil {
9,792,926✔
955
                                        c.argBuf = append(c.argBuf, b)
755,289✔
956
                                }
755,289✔
957
                        }
958
                case OP_M:
6,210,918✔
959
                        switch b {
6,210,918✔
960
                        case 'S', 's':
6,210,918✔
961
                                c.state = OP_MS
6,210,918✔
962
                        default:
×
963
                                goto parseErr
×
964
                        }
965
                case OP_MS:
6,210,918✔
966
                        switch b {
6,210,918✔
967
                        case 'G', 'g':
6,210,918✔
968
                                c.state = OP_MSG
6,210,918✔
969
                        default:
×
970
                                goto parseErr
×
971
                        }
972
                case OP_MSG:
6,210,918✔
973
                        switch b {
6,210,918✔
974
                        case ' ', '\t':
6,210,918✔
975
                                c.state = OP_MSG_SPC
6,210,918✔
976
                        default:
×
977
                                goto parseErr
×
978
                        }
979
                case OP_MSG_SPC:
6,210,918✔
980
                        switch b {
6,210,918✔
981
                        case ' ', '\t':
×
982
                                continue
×
983
                        default:
6,210,918✔
984
                                c.pa.hdr = -1
6,210,918✔
985
                                c.state = MSG_ARG
6,210,918✔
986
                                c.as = i
6,210,918✔
987
                        }
988
                case MSG_ARG:
233,286,026✔
989
                        switch b {
233,286,026✔
990
                        case '\r':
6,210,917✔
991
                                c.drop = 1
6,210,917✔
992
                        case '\n':
6,210,917✔
993
                                var arg []byte
6,210,917✔
994
                                if c.argBuf != nil {
6,349,089✔
995
                                        arg = c.argBuf
138,172✔
996
                                        c.argBuf = nil
138,172✔
997
                                } else {
6,210,917✔
998
                                        arg = buf[c.as : i-c.drop]
6,072,745✔
999
                                }
6,072,745✔
1000
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
6,210,917✔
1001
                                        return err
×
1002
                                }
×
1003
                                var err error
6,210,917✔
1004
                                if c.kind == ROUTER || c.kind == GATEWAY {
12,315,110✔
1005
                                        switch c.op {
6,104,193✔
1006
                                        case 'R', 'r':
6,096,612✔
1007
                                                if trace {
6,103,277✔
1008
                                                        c.traceInOp("RMSG", arg)
6,665✔
1009
                                                }
6,665✔
1010
                                                err = c.processRoutedMsgArgs(arg)
6,096,612✔
1011
                                        case 'L', 'l':
7,581✔
1012
                                                if trace {
7,851✔
1013
                                                        c.traceInOp("LMSG", arg)
270✔
1014
                                                }
270✔
1015
                                                lmsg = true
7,581✔
1016
                                                err = c.processRoutedOriginClusterMsgArgs(arg)
7,581✔
1017
                                        }
1018
                                } else if c.kind == LEAF {
213,448✔
1019
                                        if trace {
106,740✔
1020
                                                c.traceInOp("LMSG", arg)
16✔
1021
                                        }
16✔
1022
                                        err = c.processLeafMsgArgs(arg)
106,724✔
1023
                                }
1024
                                if err != nil {
6,210,918✔
1025
                                        return err
1✔
1026
                                }
1✔
1027
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
6,210,916✔
1028

6,210,916✔
1029
                                // jump ahead with the index. If this overruns
6,210,916✔
1030
                                // what is left we fall out and process split
6,210,916✔
1031
                                // buffer.
6,210,916✔
1032
                                i = c.as + c.pa.size - LEN_CR_LF
6,210,916✔
1033
                        default:
220,864,192✔
1034
                                if c.argBuf != nil {
222,517,149✔
1035
                                        c.argBuf = append(c.argBuf, b)
1,652,957✔
1036
                                }
1,652,957✔
1037
                        }
1038
                case OP_I:
85,784✔
1039
                        switch b {
85,784✔
1040
                        case 'N', 'n':
85,783✔
1041
                                c.state = OP_IN
85,783✔
1042
                        default:
1✔
1043
                                goto parseErr
1✔
1044
                        }
1045
                case OP_IN:
85,783✔
1046
                        switch b {
85,783✔
1047
                        case 'F', 'f':
85,782✔
1048
                                c.state = OP_INF
85,782✔
1049
                        default:
1✔
1050
                                goto parseErr
1✔
1051
                        }
1052
                case OP_INF:
85,782✔
1053
                        switch b {
85,782✔
1054
                        case 'O', 'o':
85,781✔
1055
                                c.state = OP_INFO
85,781✔
1056
                        default:
1✔
1057
                                goto parseErr
1✔
1058
                        }
1059
                case OP_INFO:
171,563✔
1060
                        switch b {
171,563✔
1061
                        case ' ', '\t':
85,782✔
1062
                                continue
85,782✔
1063
                        default:
85,781✔
1064
                                c.state = INFO_ARG
85,781✔
1065
                                c.as = i
85,781✔
1066
                        }
1067
                case INFO_ARG:
32,439,986✔
1068
                        switch b {
32,439,986✔
1069
                        case '\r':
85,778✔
1070
                                c.drop = 1
85,778✔
1071
                        case '\n':
85,779✔
1072
                                var arg []byte
85,779✔
1073
                                if c.argBuf != nil {
94,277✔
1074
                                        arg = c.argBuf
8,498✔
1075
                                        c.argBuf = nil
8,498✔
1076
                                } else {
85,779✔
1077
                                        arg = buf[c.as : i-c.drop]
77,281✔
1078
                                }
77,281✔
1079
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
85,779✔
1080
                                        return err
×
1081
                                }
×
1082
                                if err := c.processInfo(arg); err != nil {
85,780✔
1083
                                        return err
1✔
1084
                                }
1✔
1085
                                c.drop, c.as, c.state = 0, i+1, OP_START
85,778✔
1086
                        default:
32,268,429✔
1087
                                if c.argBuf != nil {
33,218,506✔
1088
                                        c.argBuf = append(c.argBuf, b)
950,077✔
1089
                                }
950,077✔
1090
                        }
1091
                case OP_PLUS:
3✔
1092
                        switch b {
3✔
1093
                        case 'O', 'o':
2✔
1094
                                c.state = OP_PLUS_O
2✔
1095
                        default:
1✔
1096
                                goto parseErr
1✔
1097
                        }
1098
                case OP_PLUS_O:
2✔
1099
                        switch b {
2✔
1100
                        case 'K', 'k':
1✔
1101
                                c.state = OP_PLUS_OK
1✔
1102
                        default:
1✔
1103
                                goto parseErr
1✔
1104
                        }
1105
                case OP_PLUS_OK:
2✔
1106
                        switch b {
2✔
1107
                        case '\n':
1✔
1108
                                c.drop, c.state = 0, OP_START
1✔
1109
                        }
1110
                case OP_MINUS:
187✔
1111
                        switch b {
187✔
1112
                        case 'E', 'e':
186✔
1113
                                c.state = OP_MINUS_E
186✔
1114
                        default:
1✔
1115
                                goto parseErr
1✔
1116
                        }
1117
                case OP_MINUS_E:
186✔
1118
                        switch b {
186✔
1119
                        case 'R', 'r':
185✔
1120
                                c.state = OP_MINUS_ER
185✔
1121
                        default:
1✔
1122
                                goto parseErr
1✔
1123
                        }
1124
                case OP_MINUS_ER:
185✔
1125
                        switch b {
185✔
1126
                        case 'R', 'r':
184✔
1127
                                c.state = OP_MINUS_ERR
184✔
1128
                        default:
1✔
1129
                                goto parseErr
1✔
1130
                        }
1131
                case OP_MINUS_ERR:
184✔
1132
                        switch b {
184✔
1133
                        case ' ', '\t':
183✔
1134
                                c.state = OP_MINUS_ERR_SPC
183✔
1135
                        default:
1✔
1136
                                goto parseErr
1✔
1137
                        }
1138
                case OP_MINUS_ERR_SPC:
183✔
1139
                        switch b {
183✔
1140
                        case ' ', '\t':
×
1141
                                continue
×
1142
                        default:
183✔
1143
                                c.state = MINUS_ERR_ARG
183✔
1144
                                c.as = i
183✔
1145
                        }
1146
                case MINUS_ERR_ARG:
5,738✔
1147
                        switch b {
5,738✔
1148
                        case '\r':
183✔
1149
                                c.drop = 1
183✔
1150
                        case '\n':
183✔
1151
                                var arg []byte
183✔
1152
                                if c.argBuf != nil {
184✔
1153
                                        arg = c.argBuf
1✔
1154
                                        c.argBuf = nil
1✔
1155
                                } else {
183✔
1156
                                        arg = buf[c.as : i-c.drop]
182✔
1157
                                }
182✔
1158
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
183✔
1159
                                        return err
×
1160
                                }
×
1161
                                c.processErr(string(arg))
183✔
1162
                                c.drop, c.as, c.state = 0, i+1, OP_START
183✔
1163
                        default:
5,372✔
1164
                                if c.argBuf != nil {
5,374✔
1165
                                        c.argBuf = append(c.argBuf, b)
2✔
1166
                                }
2✔
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,915,441✔
1175
                c.state == PUB_ARG || c.state == HPUB_ARG ||
6,915,441✔
1176
                c.state == ASUB_ARG || c.state == AUSUB_ARG ||
6,915,441✔
1177
                c.state == MSG_ARG || c.state == HMSG_ARG ||
6,915,441✔
1178
                c.state == MINUS_ERR_ARG || c.state == CONNECT_ARG || c.state == INFO_ARG {
7,129,472✔
1179

214,031✔
1180
                // Setup a holder buffer to deal with split buffer scenario.
214,031✔
1181
                if c.argBuf == nil {
427,747✔
1182
                        c.argBuf = c.scratch[:0]
213,716✔
1183
                        c.argBuf = append(c.argBuf, buf[c.as:i-c.drop]...)
213,716✔
1184
                }
213,716✔
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 {
214,033✔
1189
                        return err
2✔
1190
                }
2✔
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 {
7,566,401✔
1195
                // We need to clone the pubArg if it is still referencing the
650,962✔
1196
                // read buffer and we are not able to process the msg.
650,962✔
1197

650,962✔
1198
                if c.argBuf == nil {
1,301,924✔
1199
                        // Works also for MSG_ARG, when message comes from ROUTE or GATEWAY.
650,962✔
1200
                        if err := c.clonePubArg(lmsg); err != nil {
650,962✔
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) {
685,961✔
1208
                        lrem := len(buf[c.as:])
34,999✔
1209
                        // Consider it a protocol error when the remaining payload
34,999✔
1210
                        // is larger than the reported size for PUB. It can happen
34,999✔
1211
                        // when processing incomplete messages from rogue clients.
34,999✔
1212
                        if lrem > c.pa.size+LEN_CR_LF {
34,999✔
1213
                                goto parseErr
×
1214
                        }
1215
                        c.msgBuf = make([]byte, lrem, c.pa.size+LEN_CR_LF)
34,999✔
1216
                        copy(c.msgBuf, buf[c.as:])
34,999✔
1217
                } else {
615,963✔
1218
                        c.msgBuf = c.scratch[len(c.argBuf):len(c.argBuf)]
615,963✔
1219
                        c.msgBuf = append(c.msgBuf, (buf[c.as:])...)
615,963✔
1220
                }
615,963✔
1221
        }
1222

1223
        return nil
6,915,439✔
1224

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

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

1236
func protoSnippet(start, max int, buf []byte) string {
111✔
1237
        stop := start + max
111✔
1238
        bufSize := len(buf)
111✔
1239
        if start >= bufSize {
114✔
1240
                return `""`
3✔
1241
        }
3✔
1242
        if stop > bufSize {
191✔
1243
                stop = bufSize - 1
83✔
1244
        }
83✔
1245
        return fmt.Sprintf("%q", buf[start:stop])
108✔
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 {
18,473,703✔
1252
        if c.kind != CLIENT {
26,573,218✔
1253
                return nil
8,099,515✔
1254
        }
8,099,515✔
1255
        if len(arg) > int(mcl) {
10,374,191✔
1256
                err := NewErrorCtx(ErrMaxControlLine, "State %d, max_control_line %d, Buffer len %d (snip: %s...)",
3✔
1257
                        c.state, int(mcl), len(c.argBuf), protoSnippet(0, MAX_CONTROL_LINE_SNIPPET_SIZE, arg))
3✔
1258
                c.sendErr(err.Error())
3✔
1259
                c.closeConnection(MaxControlLineExceeded)
3✔
1260
                return err
3✔
1261
        }
3✔
1262
        return nil
10,374,185✔
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 {
650,962✔
1268
        // Just copy and re-process original arg buffer.
650,962✔
1269
        c.argBuf = c.scratch[:0]
650,962✔
1270
        c.argBuf = append(c.argBuf, c.pa.arg...)
650,962✔
1271

650,962✔
1272
        switch c.kind {
650,962✔
1273
        case ROUTER, GATEWAY:
610,640✔
1274
                if lmsg {
611,380✔
1275
                        return c.processRoutedOriginClusterMsgArgs(c.argBuf)
740✔
1276
                }
740✔
1277
                if c.pa.hdr < 0 {
1,146,225✔
1278
                        return c.processRoutedMsgArgs(c.argBuf)
536,325✔
1279
                } else {
609,900✔
1280
                        return c.processRoutedHeaderMsgArgs(c.argBuf)
73,575✔
1281
                }
73,575✔
1282
        case LEAF:
2,056✔
1283
                if c.pa.hdr < 0 {
4,000✔
1284
                        return c.processLeafMsgArgs(c.argBuf)
1,944✔
1285
                } else {
2,056✔
1286
                        return c.processLeafHeaderMsgArgs(c.argBuf)
112✔
1287
                }
112✔
1288
        default:
38,266✔
1289
                if c.pa.hdr < 0 {
75,720✔
1290
                        return c.processPub(c.argBuf)
37,454✔
1291
                } else {
38,266✔
1292
                        return c.processHeaderPub(c.argBuf, nil)
812✔
1293
                }
812✔
1294
        }
1295
}
1296

1297
func (ps *parseState) getHeader() http.Header {
858✔
1298
        if ps.header == nil {
1,716✔
1299
                if hdr := ps.pa.hdr; hdr > 0 {
931✔
1300
                        reader := bufio.NewReader(bytes.NewReader(ps.msgBuf[0:hdr]))
73✔
1301
                        tp := textproto.NewReader(reader)
73✔
1302
                        tp.ReadLine() // skip over first line, contains version
73✔
1303
                        if mimeHeader, err := tp.ReadMIMEHeader(); err == nil {
146✔
1304
                                ps.header = http.Header(mimeHeader)
73✔
1305
                        }
73✔
1306
                }
1307
        }
1308
        return ps.header
858✔
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