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

nats-io / nats-server / 24818579558

22 Apr 2026 03:35PM UTC coverage: 81.122% (-1.8%) from 82.965%
24818579558

push

github

web-flow
(2.14) [FIXED] Remove Nats-Scheduler header from sourced message (#8066)

Fixes an issue with `Nats-Schedule-Source` that allows to source the
content of a delayed message that contains the `Nats-Scheduler` header.
The latter would then make the triggering schedule become stuck.

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

75127 of 92610 relevant lines covered (81.12%)

520436.65 hits per line

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

82.75
/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
        argsa   [MAX_HMSG_ARGS + 1][]byte // pre-allocated args array to avoid per-call heap escape
36
}
37

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

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

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

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

13,052,848✔
156
        // Move to loop instead of range syntax to allow jumping of i
13,052,848✔
157
        for i = 0; i < len(buf); i++ {
938,699,600✔
158
                b = buf[i]
925,646,752✔
159

925,646,752✔
160
                switch c.state {
925,646,752✔
161
                case OP_START:
21,557,851✔
162
                        c.op = b
21,557,851✔
163
                        if b != 'C' && b != 'c' {
43,071,941✔
164
                                if authSet {
21,514,693✔
165
                                        if s == nil {
603✔
166
                                                goto authErr
×
167
                                        }
168
                                        var ok bool
603✔
169
                                        switch c.kind {
603✔
170
                                        case CLIENT:
3✔
171
                                                // Check here for NoAuthUser. If this is set allow non CONNECT protos as our first.
3✔
172
                                                // E.g. telnet proto demos.
3✔
173
                                                opts := s.getOpts()
3✔
174
                                                noAuthUser := opts.NoAuthUser
3✔
175
                                                if c.ws != nil {
3✔
176
                                                        if noAuthUserWS := opts.Websocket.NoAuthUser; noAuthUserWS != _EMPTY_ {
×
177
                                                                noAuthUser = noAuthUserWS
×
178
                                                        }
×
179
                                                }
180
                                                if noAuthUser != _EMPTY_ {
5✔
181
                                                        s.mu.Lock()
2✔
182
                                                        user, exists := s.users[noAuthUser]
2✔
183
                                                        s.mu.Unlock()
2✔
184
                                                        if exists {
4✔
185
                                                                c.RegisterUser(user)
2✔
186
                                                                c.mu.Lock()
2✔
187
                                                                c.clearAuthTimer()
2✔
188
                                                                c.flags.set(connectReceived)
2✔
189
                                                                c.mu.Unlock()
2✔
190
                                                                authSet, ok = false, true
2✔
191
                                                        }
2✔
192
                                                }
193
                                        case LEAF:
582✔
194
                                                // Compressed inbound leaf-node negotiation may require INFO
582✔
195
                                                // before CONNECT. Without compression, leaf connections must
582✔
196
                                                // still start with CONNECT.
582✔
197
                                                ok = (b == 'I' || b == 'i') && needsCompression(s.getOpts().LeafNode.Compression.Mode)
582✔
198
                                        }
199
                                        if !ok {
632✔
200
                                                goto authErr
29✔
201
                                        }
202
                                }
203
                        }
204
                        switch b {
21,557,822✔
205
                        case 'P', 'p':
6,612,686✔
206
                                c.state = OP_P
6,612,686✔
207
                        case 'H', 'h':
1,272,654✔
208
                                c.state = OP_H
1,272,654✔
209
                        case 'S', 's':
137,432✔
210
                                c.state = OP_S
137,432✔
211
                        case 'U', 'u':
16,768✔
212
                                c.state = OP_U
16,768✔
213
                        case 'R', 'r':
13,225,564✔
214
                                if c.kind == CLIENT {
13,225,564✔
215
                                        goto parseErr
×
216
                                } else {
13,225,564✔
217
                                        c.state = OP_R
13,225,564✔
218
                                }
13,225,564✔
219
                        case 'L', 'l':
162,651✔
220
                                if c.kind != LEAF && c.kind != ROUTER {
162,651✔
221
                                        goto parseErr
×
222
                                } else {
162,651✔
223
                                        c.state = OP_L
162,651✔
224
                                }
162,651✔
225
                        case 'A', 'a':
32✔
226
                                if c.kind == CLIENT {
32✔
227
                                        goto parseErr
×
228
                                } else {
32✔
229
                                        c.state = OP_A
32✔
230
                                }
32✔
231
                        case 'C', 'c':
43,761✔
232
                                c.state = OP_C
43,761✔
233
                        case 'I', 'i':
86,132✔
234
                                c.state = OP_I
86,132✔
235
                        case '+':
×
236
                                c.state = OP_PLUS
×
237
                        case '-':
141✔
238
                                c.state = OP_MINUS
141✔
239
                        default:
1✔
240
                                goto parseErr
1✔
241
                        }
242
                case OP_H:
1,272,654✔
243
                        switch b {
1,272,654✔
244
                        case 'P', 'p':
636,144✔
245
                                c.state = OP_HP
636,144✔
246
                        case 'M', 'm':
636,510✔
247
                                c.state = OP_HM
636,510✔
248
                        default:
×
249
                                goto parseErr
×
250
                        }
251
                case OP_HP:
636,144✔
252
                        switch b {
636,144✔
253
                        case 'U', 'u':
636,144✔
254
                                c.state = OP_HPU
636,144✔
255
                        default:
×
256
                                goto parseErr
×
257
                        }
258
                case OP_HPU:
636,144✔
259
                        switch b {
636,144✔
260
                        case 'B', 'b':
636,144✔
261
                                c.state = OP_HPUB
636,144✔
262
                        default:
×
263
                                goto parseErr
×
264
                        }
265
                case OP_HPUB:
636,144✔
266
                        switch b {
636,144✔
267
                        case ' ', '\t':
636,144✔
268
                                c.state = OP_HPUB_SPC
636,144✔
269
                        default:
×
270
                                goto parseErr
×
271
                        }
272
                case OP_HPUB_SPC:
636,144✔
273
                        switch b {
636,144✔
274
                        case ' ', '\t':
×
275
                                continue
×
276
                        default:
636,144✔
277
                                c.pa.hdr = 0
636,144✔
278
                                c.state = HPUB_ARG
636,144✔
279
                                c.as = i
636,144✔
280
                        }
281
                case HPUB_ARG:
22,910,805✔
282
                        switch b {
22,910,805✔
283
                        case '\r':
636,142✔
284
                                c.drop = 1
636,142✔
285
                        case '\n':
636,142✔
286
                                var arg []byte
636,142✔
287
                                if c.argBuf != nil {
636,218✔
288
                                        arg = c.argBuf
76✔
289
                                        c.argBuf = nil
76✔
290
                                } else {
636,142✔
291
                                        arg = buf[c.as : i-c.drop]
636,066✔
292
                                }
636,066✔
293
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
636,142✔
294
                                        return err
×
295
                                }
×
296
                                if trace {
704,688✔
297
                                        c.traceInOp("HPUB", arg)
68,546✔
298
                                }
68,546✔
299
                                var remaining []byte
636,142✔
300
                                if i < len(buf) {
1,272,284✔
301
                                        remaining = buf[i+1:]
636,142✔
302
                                }
636,142✔
303
                                if err := c.processHeaderPub(arg, remaining); err != nil {
636,144✔
304
                                        return err
2✔
305
                                }
2✔
306

307
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
636,140✔
308
                                // If we don't have a saved buffer then jump ahead with
636,140✔
309
                                // the index. If this overruns what is left we fall out
636,140✔
310
                                // and process split buffer.
636,140✔
311
                                if c.msgBuf == nil {
1,272,280✔
312
                                        i = c.as + c.pa.size - LEN_CR_LF
636,140✔
313
                                }
636,140✔
314
                        default:
21,638,521✔
315
                                if c.argBuf != nil {
21,639,377✔
316
                                        c.argBuf = append(c.argBuf, b)
856✔
317
                                }
856✔
318
                        }
319
                case OP_HM:
636,510✔
320
                        switch b {
636,510✔
321
                        case 'S', 's':
636,510✔
322
                                c.state = OP_HMS
636,510✔
323
                        default:
×
324
                                goto parseErr
×
325
                        }
326
                case OP_HMS:
636,510✔
327
                        switch b {
636,510✔
328
                        case 'G', 'g':
636,510✔
329
                                c.state = OP_HMSG
636,510✔
330
                        default:
×
331
                                goto parseErr
×
332
                        }
333
                case OP_HMSG:
636,510✔
334
                        switch b {
636,510✔
335
                        case ' ', '\t':
636,510✔
336
                                c.state = OP_HMSG_SPC
636,510✔
337
                        default:
×
338
                                goto parseErr
×
339
                        }
340
                case OP_HMSG_SPC:
636,510✔
341
                        switch b {
636,510✔
342
                        case ' ', '\t':
×
343
                                continue
×
344
                        default:
636,510✔
345
                                c.pa.hdr = 0
636,510✔
346
                                c.state = HMSG_ARG
636,510✔
347
                                c.as = i
636,510✔
348
                        }
349
                case HMSG_ARG:
51,651,639✔
350
                        switch b {
51,651,639✔
351
                        case '\r':
636,510✔
352
                                c.drop = 1
636,510✔
353
                        case '\n':
636,510✔
354
                                var arg []byte
636,510✔
355
                                if c.argBuf != nil {
643,838✔
356
                                        arg = c.argBuf
7,328✔
357
                                        c.argBuf = nil
7,328✔
358
                                } else {
636,510✔
359
                                        arg = buf[c.as : i-c.drop]
629,182✔
360
                                }
629,182✔
361
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
636,510✔
362
                                        return err
×
363
                                }
×
364
                                var err error
636,510✔
365
                                if c.kind == ROUTER || c.kind == GATEWAY {
1,272,619✔
366
                                        if trace {
638,255✔
367
                                                c.traceInOp("HMSG", arg)
2,146✔
368
                                        }
2,146✔
369
                                        err = c.processRoutedHeaderMsgArgs(arg)
636,109✔
370
                                } else if c.kind == LEAF {
802✔
371
                                        if trace {
553✔
372
                                                c.traceInOp("HMSG", arg)
152✔
373
                                        }
152✔
374
                                        err = c.processLeafHeaderMsgArgs(arg)
401✔
375
                                }
376
                                if err != nil {
636,510✔
377
                                        return err
×
378
                                }
×
379
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
636,510✔
380

636,510✔
381
                                // jump ahead with the index. If this overruns
636,510✔
382
                                // what is left we fall out and process split
636,510✔
383
                                // buffer.
636,510✔
384
                                i = c.as + c.pa.size - LEN_CR_LF
636,510✔
385
                        default:
50,378,619✔
386
                                if c.argBuf != nil {
50,771,802✔
387
                                        c.argBuf = append(c.argBuf, b)
393,183✔
388
                                }
393,183✔
389
                        }
390
                case OP_P:
6,612,685✔
391
                        switch b {
6,612,685✔
392
                        case 'U', 'u':
6,541,868✔
393
                                c.state = OP_PU
6,541,868✔
394
                        case 'I', 'i':
41,365✔
395
                                c.state = OP_PI
41,365✔
396
                        case 'O', 'o':
29,452✔
397
                                c.state = OP_PO
29,452✔
398
                        default:
×
399
                                goto parseErr
×
400
                        }
401
                case OP_PU:
6,541,868✔
402
                        switch b {
6,541,868✔
403
                        case 'B', 'b':
6,541,868✔
404
                                c.state = OP_PUB
6,541,868✔
405
                        default:
×
406
                                goto parseErr
×
407
                        }
408
                case OP_PUB:
6,541,868✔
409
                        switch b {
6,541,868✔
410
                        case ' ', '\t':
6,541,867✔
411
                                c.state = OP_PUB_SPC
6,541,867✔
412
                        default:
1✔
413
                                goto parseErr
1✔
414
                        }
415
                case OP_PUB_SPC:
6,541,867✔
416
                        switch b {
6,541,867✔
417
                        case ' ', '\t':
×
418
                                continue
×
419
                        default:
6,541,867✔
420
                                c.pa.hdr = -1
6,541,867✔
421
                                c.state = PUB_ARG
6,541,867✔
422
                                c.as = i
6,541,867✔
423
                        }
424
                case PUB_ARG:
144,454,205✔
425
                        switch b {
144,454,205✔
426
                        case '\r':
6,541,859✔
427
                                c.drop = 1
6,541,859✔
428
                        case '\n':
6,541,859✔
429
                                var arg []byte
6,541,859✔
430
                                if c.argBuf != nil {
6,566,794✔
431
                                        arg = c.argBuf
24,935✔
432
                                        c.argBuf = nil
24,935✔
433
                                } else {
6,541,859✔
434
                                        arg = buf[c.as : i-c.drop]
6,516,924✔
435
                                }
6,516,924✔
436
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
6,541,859✔
437
                                        return err
×
438
                                }
×
439
                                if trace {
6,853,784✔
440
                                        c.traceInOp("PUB", arg)
311,925✔
441
                                }
311,925✔
442
                                if err := c.processPub(arg); err != nil {
6,541,865✔
443
                                        return err
6✔
444
                                }
6✔
445

446
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
6,541,853✔
447
                                // If we don't have a saved buffer then jump ahead with
6,541,853✔
448
                                // the index. If this overruns what is left we fall out
6,541,853✔
449
                                // and process split buffer.
6,541,853✔
450
                                if c.msgBuf == nil {
13,083,706✔
451
                                        i = c.as + c.pa.size - LEN_CR_LF
6,541,853✔
452
                                }
6,541,853✔
453
                        default:
131,370,487✔
454
                                if c.argBuf != nil {
131,851,191✔
455
                                        c.argBuf = append(c.argBuf, b)
480,704✔
456
                                }
480,704✔
457
                        }
458
                case MSG_PAYLOAD:
20,487,588✔
459
                        if c.msgBuf != nil {
22,301,030✔
460
                                // copy as much as we can to the buffer and skip ahead.
1,813,442✔
461
                                toCopy := c.pa.size - len(c.msgBuf)
1,813,442✔
462
                                avail := len(buf) - i
1,813,442✔
463
                                if avail < toCopy {
2,360,497✔
464
                                        toCopy = avail
547,055✔
465
                                }
547,055✔
466
                                if toCopy > 0 {
3,626,884✔
467
                                        start := len(c.msgBuf)
1,813,442✔
468
                                        // This is needed for copy to work.
1,813,442✔
469
                                        c.msgBuf = c.msgBuf[:start+toCopy]
1,813,442✔
470
                                        copy(c.msgBuf[start:], buf[i:i+toCopy])
1,813,442✔
471
                                        // Update our index
1,813,442✔
472
                                        i = (i + toCopy) - 1
1,813,442✔
473
                                } else {
1,813,442✔
474
                                        // Fall back to append if needed.
×
475
                                        c.msgBuf = append(c.msgBuf, b)
×
476
                                }
×
477
                                if len(c.msgBuf) >= c.pa.size {
3,079,829✔
478
                                        c.state = MSG_END_R
1,266,387✔
479
                                }
1,266,387✔
480
                        } else if i-c.as+1 >= c.pa.size {
37,348,292✔
481
                                c.state = MSG_END_R
18,674,146✔
482
                        }
18,674,146✔
483
                case MSG_END_R:
19,940,533✔
484
                        if b != '\r' {
19,940,534✔
485
                                goto parseErr
1✔
486
                        }
487
                        if c.msgBuf != nil {
21,221,249✔
488
                                c.msgBuf = append(c.msgBuf, b)
1,280,717✔
489
                        }
1,280,717✔
490
                        c.state = MSG_END_N
19,940,532✔
491
                case MSG_END_N:
19,940,531✔
492
                        if b != '\n' {
19,940,531✔
493
                                goto parseErr
×
494
                        }
495
                        if c.msgBuf != nil {
21,222,996✔
496
                                c.msgBuf = append(c.msgBuf, b)
1,282,465✔
497
                        } else {
19,940,531✔
498
                                c.msgBuf = buf[c.as : i+1]
18,658,066✔
499
                        }
18,658,066✔
500

501
                        var mt *msgTrace
19,940,531✔
502
                        if c.pa.hdr > 0 {
21,213,630✔
503
                                mt = c.initMsgTrace()
1,273,099✔
504
                        }
1,273,099✔
505
                        // Check for mappings.
506
                        if (c.kind == CLIENT || c.kind == LEAF) && c.in.flags.isSet(hasMappings) {
19,973,266✔
507
                                changed := c.selectMappedSubject()
32,735✔
508
                                if changed {
45,301✔
509
                                        if trace {
12,642✔
510
                                                c.traceInOp("MAPPING", []byte(fmt.Sprintf("%s -> %s", c.pa.mapped, c.pa.subject)))
76✔
511
                                        }
76✔
512
                                        // c.pa.subject is the subject the original is now mapped to.
513
                                        mt.addSubjectMappingEvent(c.pa.subject)
12,566✔
514
                                }
515
                        }
516
                        if trace {
20,330,230✔
517
                                c.traceMsg(c.msgBuf)
389,699✔
518
                        }
389,699✔
519

520
                        c.processInboundMsg(c.msgBuf)
19,940,531✔
521

19,940,531✔
522
                        mt.sendEvent()
19,940,531✔
523
                        c.argBuf, c.msgBuf, c.header = nil, nil, nil
19,940,531✔
524
                        c.drop, c.as, c.state = 0, i+1, OP_START
19,940,531✔
525
                        // Drop all pub args
19,940,531✔
526
                        c.pa.arg, c.pa.pacache, c.pa.origin, c.pa.account, c.pa.subject, c.pa.mapped = nil, nil, nil, nil, nil, nil
19,940,531✔
527
                        c.pa.reply, c.pa.hdr, c.pa.size, c.pa.szb, c.pa.hdb, c.pa.queues = nil, -1, 0, nil, nil, nil
19,940,531✔
528
                        c.pa.trace = nil
19,940,531✔
529
                        c.pa.delivered = false
19,940,531✔
530
                        lmsg = false
19,940,531✔
531
                case OP_A:
32✔
532
                        switch b {
32✔
533
                        case '+':
4✔
534
                                c.state = OP_ASUB
4✔
535
                        case '-', 'u':
28✔
536
                                c.state = OP_AUSUB
28✔
537
                        default:
×
538
                                goto parseErr
×
539
                        }
540
                case OP_ASUB:
4✔
541
                        switch b {
4✔
542
                        case ' ', '\t':
4✔
543
                                c.state = OP_ASUB_SPC
4✔
544
                        default:
×
545
                                goto parseErr
×
546
                        }
547
                case OP_ASUB_SPC:
4✔
548
                        switch b {
4✔
549
                        case ' ', '\t':
×
550
                                continue
×
551
                        default:
4✔
552
                                c.state = ASUB_ARG
4✔
553
                                c.as = i
4✔
554
                        }
555
                case ASUB_ARG:
14✔
556
                        switch b {
14✔
557
                        case '\r':
4✔
558
                                c.drop = 1
4✔
559
                        case '\n':
4✔
560
                                var arg []byte
4✔
561
                                if c.argBuf != nil {
4✔
562
                                        arg = c.argBuf
×
563
                                        c.argBuf = nil
×
564
                                } else {
4✔
565
                                        arg = buf[c.as : i-c.drop]
4✔
566
                                }
4✔
567
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
4✔
568
                                        return err
×
569
                                }
×
570
                                if trace {
4✔
571
                                        c.traceInOp("A+", arg)
×
572
                                }
×
573
                                if err := c.processAccountSub(arg); err != nil {
4✔
574
                                        return err
×
575
                                }
×
576
                                c.drop, c.as, c.state = 0, i+1, OP_START
4✔
577
                        default:
6✔
578
                                if c.argBuf != nil {
6✔
579
                                        c.argBuf = append(c.argBuf, b)
×
580
                                }
×
581
                        }
582
                case OP_AUSUB:
28✔
583
                        switch b {
28✔
584
                        case ' ', '\t':
28✔
585
                                c.state = OP_AUSUB_SPC
28✔
586
                        default:
×
587
                                goto parseErr
×
588
                        }
589
                case OP_AUSUB_SPC:
28✔
590
                        switch b {
28✔
591
                        case ' ', '\t':
×
592
                                continue
×
593
                        default:
28✔
594
                                c.state = AUSUB_ARG
28✔
595
                                c.as = i
28✔
596
                        }
597
                case AUSUB_ARG:
142✔
598
                        switch b {
142✔
599
                        case '\r':
28✔
600
                                c.drop = 1
28✔
601
                        case '\n':
28✔
602
                                var arg []byte
28✔
603
                                if c.argBuf != nil {
28✔
604
                                        arg = c.argBuf
×
605
                                        c.argBuf = nil
×
606
                                } else {
28✔
607
                                        arg = buf[c.as : i-c.drop]
28✔
608
                                }
28✔
609
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
28✔
610
                                        return err
×
611
                                }
×
612
                                if trace {
45✔
613
                                        c.traceInOp("A-", arg)
17✔
614
                                }
17✔
615
                                c.processAccountUnsub(arg)
28✔
616
                                c.drop, c.as, c.state = 0, i+1, OP_START
28✔
617
                        default:
86✔
618
                                if c.argBuf != nil {
86✔
619
                                        c.argBuf = append(c.argBuf, b)
×
620
                                }
×
621
                        }
622
                case OP_S:
137,432✔
623
                        switch b {
137,432✔
624
                        case 'U', 'u':
137,432✔
625
                                c.state = OP_SU
137,432✔
626
                        default:
×
627
                                goto parseErr
×
628
                        }
629
                case OP_SU:
137,432✔
630
                        switch b {
137,432✔
631
                        case 'B', 'b':
137,432✔
632
                                c.state = OP_SUB
137,432✔
633
                        default:
×
634
                                goto parseErr
×
635
                        }
636
                case OP_SUB:
1,173,742✔
637
                        switch b {
1,173,742✔
638
                        case ' ', '\t':
1,173,741✔
639
                                c.state = OP_SUB_SPC
1,173,741✔
640
                        default:
1✔
641
                                goto parseErr
1✔
642
                        }
643
                case OP_SUB_SPC:
1,173,739✔
644
                        switch b {
1,173,739✔
645
                        case ' ', '\t':
×
646
                                continue
×
647
                        default:
1,173,739✔
648
                                c.state = SUB_ARG
1,173,739✔
649
                                c.as = i
1,173,739✔
650
                        }
651
                case SUB_ARG:
43,904,229✔
652
                        switch b {
43,904,229✔
653
                        case '\r':
1,173,712✔
654
                                c.drop = 1
1,173,712✔
655
                        case '\n':
1,173,712✔
656
                                var arg []byte
1,173,712✔
657
                                if c.argBuf != nil {
1,201,943✔
658
                                        arg = c.argBuf
28,231✔
659
                                        c.argBuf = nil
28,231✔
660
                                } else {
1,173,712✔
661
                                        arg = buf[c.as : i-c.drop]
1,145,481✔
662
                                }
1,145,481✔
663
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
1,173,712✔
664
                                        return err
×
665
                                }
×
666
                                var err error
1,173,712✔
667

1,173,712✔
668
                                switch c.kind {
1,173,712✔
669
                                case CLIENT:
137,430✔
670
                                        if trace {
151,708✔
671
                                                c.traceInOp("SUB", arg)
14,278✔
672
                                        }
14,278✔
673
                                        err = c.parseSub(arg, false)
137,430✔
674
                                case ROUTER:
842,321✔
675
                                        switch c.op {
842,321✔
676
                                        case 'R', 'r':
830,544✔
677
                                                if trace {
833,506✔
678
                                                        c.traceInOp("RS+", arg)
2,962✔
679
                                                }
2,962✔
680
                                                err = c.processRemoteSub(arg, false)
830,544✔
681
                                        case 'L', 'l':
11,777✔
682
                                                if trace {
11,971✔
683
                                                        c.traceInOp("LS+", arg)
194✔
684
                                                }
194✔
685
                                                err = c.processRemoteSub(arg, true)
11,777✔
686
                                        }
687
                                case GATEWAY:
161,354✔
688
                                        if trace {
161,549✔
689
                                                c.traceInOp("RS+", arg)
195✔
690
                                        }
195✔
691
                                        err = c.processGatewayRSub(arg)
161,354✔
692
                                case LEAF:
32,607✔
693
                                        if trace {
32,728✔
694
                                                c.traceInOp("LS+", arg)
121✔
695
                                        }
121✔
696
                                        err = c.processLeafSub(arg)
32,607✔
697
                                }
698
                                if err != nil {
1,173,712✔
699
                                        return err
×
700
                                }
×
701
                                c.drop, c.as, c.state = 0, i+1, OP_START
1,173,712✔
702
                        default:
41,556,805✔
703
                                if c.argBuf != nil {
42,263,648✔
704
                                        c.argBuf = append(c.argBuf, b)
706,843✔
705
                                }
706,843✔
706
                        }
707
                case OP_L:
162,651✔
708
                        switch b {
162,651✔
709
                        case 'S', 's':
58,898✔
710
                                c.state = OP_LS
58,898✔
711
                        case 'M', 'm':
103,753✔
712
                                c.state = OP_M
103,753✔
713
                        default:
×
714
                                goto parseErr
×
715
                        }
716
                case OP_LS:
58,898✔
717
                        switch b {
58,898✔
718
                        case '+':
44,384✔
719
                                c.state = OP_SUB
44,384✔
720
                        case '-':
14,514✔
721
                                c.state = OP_UNSUB
14,514✔
722
                        default:
×
723
                                goto parseErr
×
724
                        }
725
                case OP_R:
13,225,564✔
726
                        switch b {
13,225,564✔
727
                        case 'S', 's':
1,203,264✔
728
                                c.state = OP_RS
1,203,264✔
729
                        case 'M', 'm':
12,022,300✔
730
                                c.state = OP_M
12,022,300✔
731
                        default:
×
732
                                goto parseErr
×
733
                        }
734
                case OP_RS:
1,203,264✔
735
                        switch b {
1,203,264✔
736
                        case '+':
991,927✔
737
                                c.state = OP_SUB
991,927✔
738
                        case '-':
211,337✔
739
                                c.state = OP_UNSUB
211,337✔
740
                        default:
×
741
                                goto parseErr
×
742
                        }
743
                case OP_U:
16,768✔
744
                        switch b {
16,768✔
745
                        case 'N', 'n':
16,768✔
746
                                c.state = OP_UN
16,768✔
747
                        default:
×
748
                                goto parseErr
×
749
                        }
750
                case OP_UN:
16,768✔
751
                        switch b {
16,768✔
752
                        case 'S', 's':
16,768✔
753
                                c.state = OP_UNS
16,768✔
754
                        default:
×
755
                                goto parseErr
×
756
                        }
757
                case OP_UNS:
16,768✔
758
                        switch b {
16,768✔
759
                        case 'U', 'u':
16,768✔
760
                                c.state = OP_UNSU
16,768✔
761
                        default:
×
762
                                goto parseErr
×
763
                        }
764
                case OP_UNSU:
16,768✔
765
                        switch b {
16,768✔
766
                        case 'B', 'b':
16,768✔
767
                                c.state = OP_UNSUB
16,768✔
768
                        default:
×
769
                                goto parseErr
×
770
                        }
771
                case OP_UNSUB:
242,619✔
772
                        switch b {
242,619✔
773
                        case ' ', '\t':
242,619✔
774
                                c.state = OP_UNSUB_SPC
242,619✔
775
                        default:
×
776
                                goto parseErr
×
777
                        }
778
                case OP_UNSUB_SPC:
242,619✔
779
                        switch b {
242,619✔
780
                        case ' ', '\t':
×
781
                                continue
×
782
                        default:
242,619✔
783
                                c.state = UNSUB_ARG
242,619✔
784
                                c.as = i
242,619✔
785
                        }
786
                case UNSUB_ARG:
7,149,883✔
787
                        switch b {
7,149,883✔
788
                        case '\r':
242,614✔
789
                                c.drop = 1
242,614✔
790
                        case '\n':
242,613✔
791
                                var arg []byte
242,613✔
792
                                if c.argBuf != nil {
249,758✔
793
                                        arg = c.argBuf
7,145✔
794
                                        c.argBuf = nil
7,145✔
795
                                } else {
242,613✔
796
                                        arg = buf[c.as : i-c.drop]
235,468✔
797
                                }
235,468✔
798
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
242,613✔
799
                                        return err
×
800
                                }
×
801
                                var err error
242,613✔
802

242,613✔
803
                                switch c.kind {
242,613✔
804
                                case CLIENT:
16,768✔
805
                                        if trace {
30,806✔
806
                                                c.traceInOp("UNSUB", arg)
14,038✔
807
                                        }
14,038✔
808
                                        err = c.processUnsub(arg)
16,768✔
809
                                case ROUTER:
177,196✔
810
                                        if trace && c.srv != nil {
177,611✔
811
                                                switch c.op {
415✔
812
                                                case 'R', 'r':
221✔
813
                                                        c.traceInOp("RS-", arg)
221✔
814
                                                case 'L', 'l':
194✔
815
                                                        c.traceInOp("LS-", arg)
194✔
816
                                                }
817
                                        }
818
                                        leafUnsub := c.op == 'L' || c.op == 'l'
177,196✔
819
                                        err = c.processRemoteUnsub(arg, leafUnsub)
177,196✔
820
                                case GATEWAY:
45,261✔
821
                                        if trace {
46,438✔
822
                                                c.traceInOp("RS-", arg)
1,177✔
823
                                        }
1,177✔
824
                                        err = c.processGatewayRUnsub(arg)
45,261✔
825
                                case LEAF:
3,388✔
826
                                        if trace {
3,392✔
827
                                                c.traceInOp("LS-", arg)
4✔
828
                                        }
4✔
829
                                        err = c.processLeafUnsub(arg)
3,388✔
830
                                }
831
                                if err != nil {
242,613✔
832
                                        return err
×
833
                                }
×
834
                                c.drop, c.as, c.state = 0, i+1, OP_START
242,613✔
835
                        default:
6,664,656✔
836
                                if c.argBuf != nil {
6,827,052✔
837
                                        c.argBuf = append(c.argBuf, b)
162,396✔
838
                                }
162,396✔
839
                        }
840
                case OP_PI:
41,365✔
841
                        switch b {
41,365✔
842
                        case 'N', 'n':
41,365✔
843
                                c.state = OP_PIN
41,365✔
844
                        default:
×
845
                                goto parseErr
×
846
                        }
847
                case OP_PIN:
41,365✔
848
                        switch b {
41,365✔
849
                        case 'G', 'g':
41,365✔
850
                                c.state = OP_PING
41,365✔
851
                        default:
×
852
                                goto parseErr
×
853
                        }
854
                case OP_PING:
82,730✔
855
                        switch b {
82,730✔
856
                        case '\n':
41,365✔
857
                                if trace {
42,034✔
858
                                        c.traceInOp("PING", nil)
669✔
859
                                }
669✔
860
                                c.processPing()
41,365✔
861
                                c.drop, c.state = 0, OP_START
41,365✔
862
                        }
863
                case OP_PO:
29,452✔
864
                        switch b {
29,452✔
865
                        case 'N', 'n':
29,452✔
866
                                c.state = OP_PON
29,452✔
867
                        default:
×
868
                                goto parseErr
×
869
                        }
870
                case OP_PON:
29,452✔
871
                        switch b {
29,452✔
872
                        case 'G', 'g':
29,452✔
873
                                c.state = OP_PONG
29,452✔
874
                        default:
×
875
                                goto parseErr
×
876
                        }
877
                case OP_PONG:
58,904✔
878
                        switch b {
58,904✔
879
                        case '\n':
29,452✔
880
                                if trace {
29,547✔
881
                                        c.traceInOp("PONG", nil)
95✔
882
                                }
95✔
883
                                c.processPong()
29,452✔
884
                                c.drop, c.state = 0, OP_START
29,452✔
885
                        }
886
                case OP_C:
43,761✔
887
                        switch b {
43,761✔
888
                        case 'O', 'o':
43,761✔
889
                                c.state = OP_CO
43,761✔
890
                        default:
×
891
                                goto parseErr
×
892
                        }
893
                case OP_CO:
43,761✔
894
                        switch b {
43,761✔
895
                        case 'N', 'n':
43,761✔
896
                                c.state = OP_CON
43,761✔
897
                        default:
×
898
                                goto parseErr
×
899
                        }
900
                case OP_CON:
43,761✔
901
                        switch b {
43,761✔
902
                        case 'N', 'n':
43,761✔
903
                                c.state = OP_CONN
43,761✔
904
                        default:
×
905
                                goto parseErr
×
906
                        }
907
                case OP_CONN:
43,761✔
908
                        switch b {
43,761✔
909
                        case 'E', 'e':
43,761✔
910
                                c.state = OP_CONNE
43,761✔
911
                        default:
×
912
                                goto parseErr
×
913
                        }
914
                case OP_CONNE:
43,761✔
915
                        switch b {
43,761✔
916
                        case 'C', 'c':
43,761✔
917
                                c.state = OP_CONNEC
43,761✔
918
                        default:
×
919
                                goto parseErr
×
920
                        }
921
                case OP_CONNEC:
43,761✔
922
                        switch b {
43,761✔
923
                        case 'T', 't':
43,761✔
924
                                c.state = OP_CONNECT
43,761✔
925
                        default:
×
926
                                goto parseErr
×
927
                        }
928
                case OP_CONNECT:
87,522✔
929
                        switch b {
87,522✔
930
                        case ' ', '\t':
43,761✔
931
                                continue
43,761✔
932
                        default:
43,761✔
933
                                c.state = CONNECT_ARG
43,761✔
934
                                c.as = i
43,761✔
935
                        }
936
                case CONNECT_ARG:
9,251,532✔
937
                        switch b {
9,251,532✔
938
                        case '\r':
43,761✔
939
                                c.drop = 1
43,761✔
940
                        case '\n':
43,761✔
941
                                var arg []byte
43,761✔
942
                                if c.argBuf != nil {
46,168✔
943
                                        arg = c.argBuf
2,407✔
944
                                        c.argBuf = nil
2,407✔
945
                                } else {
43,761✔
946
                                        arg = buf[c.as : i-c.drop]
41,354✔
947
                                }
41,354✔
948
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
43,761✔
949
                                        return err
×
950
                                }
×
951
                                if trace {
44,436✔
952
                                        c.traceInOp("CONNECT", removeSecretsFromTrace(arg))
675✔
953
                                }
675✔
954
                                if err := c.processConnect(arg); err != nil {
44,042✔
955
                                        return err
281✔
956
                                }
281✔
957
                                c.drop, c.state = 0, OP_START
43,480✔
958
                                // Reset notion on authSet
43,480✔
959
                                c.mu.Lock()
43,480✔
960
                                authSet = c.awaitingAuth()
43,480✔
961
                                c.mu.Unlock()
43,480✔
962
                        default:
9,164,010✔
963
                                if c.argBuf != nil {
9,915,687✔
964
                                        c.argBuf = append(c.argBuf, b)
751,677✔
965
                                }
751,677✔
966
                        }
967
                case OP_M:
12,126,053✔
968
                        switch b {
12,126,053✔
969
                        case 'S', 's':
12,126,053✔
970
                                c.state = OP_MS
12,126,053✔
971
                        default:
×
972
                                goto parseErr
×
973
                        }
974
                case OP_MS:
12,126,053✔
975
                        switch b {
12,126,053✔
976
                        case 'G', 'g':
12,126,053✔
977
                                c.state = OP_MSG
12,126,053✔
978
                        default:
×
979
                                goto parseErr
×
980
                        }
981
                case OP_MSG:
12,126,053✔
982
                        switch b {
12,126,053✔
983
                        case ' ', '\t':
12,126,053✔
984
                                c.state = OP_MSG_SPC
12,126,053✔
985
                        default:
×
986
                                goto parseErr
×
987
                        }
988
                case OP_MSG_SPC:
12,126,053✔
989
                        switch b {
12,126,053✔
990
                        case ' ', '\t':
×
991
                                continue
×
992
                        default:
12,126,053✔
993
                                c.pa.hdr = -1
12,126,053✔
994
                                c.state = MSG_ARG
12,126,053✔
995
                                c.as = i
12,126,053✔
996
                        }
997
                case MSG_ARG:
431,656,954✔
998
                        switch b {
431,656,954✔
999
                        case '\r':
12,126,052✔
1000
                                c.drop = 1
12,126,052✔
1001
                        case '\n':
12,126,052✔
1002
                                var arg []byte
12,126,052✔
1003
                                if c.argBuf != nil {
12,547,021✔
1004
                                        arg = c.argBuf
420,969✔
1005
                                        c.argBuf = nil
420,969✔
1006
                                } else {
12,126,052✔
1007
                                        arg = buf[c.as : i-c.drop]
11,705,083✔
1008
                                }
11,705,083✔
1009
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
12,126,052✔
1010
                                        return err
×
1011
                                }
×
1012
                                var err error
12,126,052✔
1013
                                if c.kind == ROUTER || c.kind == GATEWAY {
24,155,247✔
1014
                                        switch c.op {
12,029,195✔
1015
                                        case 'R', 'r':
12,022,300✔
1016
                                                if trace {
12,028,950✔
1017
                                                        c.traceInOp("RMSG", arg)
6,650✔
1018
                                                }
6,650✔
1019
                                                err = c.processRoutedMsgArgs(arg)
12,022,300✔
1020
                                        case 'L', 'l':
6,895✔
1021
                                                if trace {
7,159✔
1022
                                                        c.traceInOp("LMSG", arg)
264✔
1023
                                                }
264✔
1024
                                                lmsg = true
6,895✔
1025
                                                err = c.processRoutedOriginClusterMsgArgs(arg)
6,895✔
1026
                                        }
1027
                                } else if c.kind == LEAF {
193,714✔
1028
                                        if trace {
96,876✔
1029
                                                c.traceInOp("LMSG", arg)
19✔
1030
                                        }
19✔
1031
                                        err = c.processLeafMsgArgs(arg)
96,857✔
1032
                                }
1033
                                if err != nil {
12,126,053✔
1034
                                        return err
1✔
1035
                                }
1✔
1036
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
12,126,051✔
1037

12,126,051✔
1038
                                // jump ahead with the index. If this overruns
12,126,051✔
1039
                                // what is left we fall out and process split
12,126,051✔
1040
                                // buffer.
12,126,051✔
1041
                                i = c.as + c.pa.size - LEN_CR_LF
12,126,051✔
1042
                        default:
407,404,850✔
1043
                                if c.argBuf != nil {
411,878,099✔
1044
                                        c.argBuf = append(c.argBuf, b)
4,473,249✔
1045
                                }
4,473,249✔
1046
                        }
1047
                case OP_I:
86,132✔
1048
                        switch b {
86,132✔
1049
                        case 'N', 'n':
86,132✔
1050
                                c.state = OP_IN
86,132✔
1051
                        default:
×
1052
                                goto parseErr
×
1053
                        }
1054
                case OP_IN:
86,132✔
1055
                        switch b {
86,132✔
1056
                        case 'F', 'f':
86,132✔
1057
                                c.state = OP_INF
86,132✔
1058
                        default:
×
1059
                                goto parseErr
×
1060
                        }
1061
                case OP_INF:
86,132✔
1062
                        switch b {
86,132✔
1063
                        case 'O', 'o':
86,132✔
1064
                                c.state = OP_INFO
86,132✔
1065
                        default:
×
1066
                                goto parseErr
×
1067
                        }
1068
                case OP_INFO:
172,264✔
1069
                        switch b {
172,264✔
1070
                        case ' ', '\t':
86,132✔
1071
                                continue
86,132✔
1072
                        default:
86,132✔
1073
                                c.state = INFO_ARG
86,132✔
1074
                                c.as = i
86,132✔
1075
                        }
1076
                case INFO_ARG:
32,740,546✔
1077
                        switch b {
32,740,546✔
1078
                        case '\r':
86,127✔
1079
                                c.drop = 1
86,127✔
1080
                        case '\n':
86,127✔
1081
                                var arg []byte
86,127✔
1082
                                if c.argBuf != nil {
95,005✔
1083
                                        arg = c.argBuf
8,878✔
1084
                                        c.argBuf = nil
8,878✔
1085
                                } else {
86,127✔
1086
                                        arg = buf[c.as : i-c.drop]
77,249✔
1087
                                }
77,249✔
1088
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
86,127✔
1089
                                        return err
×
1090
                                }
×
1091
                                if err := c.processInfo(arg); err != nil {
86,127✔
1092
                                        return err
×
1093
                                }
×
1094
                                c.drop, c.as, c.state = 0, i+1, OP_START
86,127✔
1095
                        default:
32,568,292✔
1096
                                if c.argBuf != nil {
33,550,742✔
1097
                                        c.argBuf = append(c.argBuf, b)
982,450✔
1098
                                }
982,450✔
1099
                        }
1100
                case OP_PLUS:
×
1101
                        switch b {
×
1102
                        case 'O', 'o':
×
1103
                                c.state = OP_PLUS_O
×
1104
                        default:
×
1105
                                goto parseErr
×
1106
                        }
1107
                case OP_PLUS_O:
×
1108
                        switch b {
×
1109
                        case 'K', 'k':
×
1110
                                c.state = OP_PLUS_OK
×
1111
                        default:
×
1112
                                goto parseErr
×
1113
                        }
1114
                case OP_PLUS_OK:
×
1115
                        switch b {
×
1116
                        case '\n':
×
1117
                                c.drop, c.state = 0, OP_START
×
1118
                        }
1119
                case OP_MINUS:
141✔
1120
                        switch b {
141✔
1121
                        case 'E', 'e':
141✔
1122
                                c.state = OP_MINUS_E
141✔
1123
                        default:
×
1124
                                goto parseErr
×
1125
                        }
1126
                case OP_MINUS_E:
141✔
1127
                        switch b {
141✔
1128
                        case 'R', 'r':
141✔
1129
                                c.state = OP_MINUS_ER
141✔
1130
                        default:
×
1131
                                goto parseErr
×
1132
                        }
1133
                case OP_MINUS_ER:
141✔
1134
                        switch b {
141✔
1135
                        case 'R', 'r':
141✔
1136
                                c.state = OP_MINUS_ERR
141✔
1137
                        default:
×
1138
                                goto parseErr
×
1139
                        }
1140
                case OP_MINUS_ERR:
141✔
1141
                        switch b {
141✔
1142
                        case ' ', '\t':
141✔
1143
                                c.state = OP_MINUS_ERR_SPC
141✔
1144
                        default:
×
1145
                                goto parseErr
×
1146
                        }
1147
                case OP_MINUS_ERR_SPC:
141✔
1148
                        switch b {
141✔
1149
                        case ' ', '\t':
×
1150
                                continue
×
1151
                        default:
141✔
1152
                                c.state = MINUS_ERR_ARG
141✔
1153
                                c.as = i
141✔
1154
                        }
1155
                case MINUS_ERR_ARG:
4,681✔
1156
                        switch b {
4,681✔
1157
                        case '\r':
141✔
1158
                                c.drop = 1
141✔
1159
                        case '\n':
141✔
1160
                                var arg []byte
141✔
1161
                                if c.argBuf != nil {
142✔
1162
                                        arg = c.argBuf
1✔
1163
                                        c.argBuf = nil
1✔
1164
                                } else {
141✔
1165
                                        arg = buf[c.as : i-c.drop]
140✔
1166
                                }
140✔
1167
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
141✔
1168
                                        return err
×
1169
                                }
×
1170
                                c.processErr(string(arg))
141✔
1171
                                c.drop, c.as, c.state = 0, i+1, OP_START
141✔
1172
                        default:
4,399✔
1173
                                if c.argBuf != nil {
4,402✔
1174
                                        c.argBuf = append(c.argBuf, b)
3✔
1175
                                }
3✔
1176
                        }
1177
                default:
×
1178
                        goto parseErr
×
1179
                }
1180
        }
1181

1182
        // Check for split buffer scenarios for any ARG state.
1183
        if c.state == SUB_ARG || c.state == UNSUB_ARG ||
13,052,525✔
1184
                c.state == PUB_ARG || c.state == HPUB_ARG ||
13,052,525✔
1185
                c.state == ASUB_ARG || c.state == AUSUB_ARG ||
13,052,525✔
1186
                c.state == MSG_ARG || c.state == HMSG_ARG ||
13,052,525✔
1187
                c.state == MINUS_ERR_ARG || c.state == CONNECT_ARG || c.state == INFO_ARG {
13,552,871✔
1188

500,346✔
1189
                // Setup a holder buffer to deal with split buffer scenario.
500,346✔
1190
                if c.argBuf == nil {
1,000,365✔
1191
                        c.argBuf = c.scratch[:0]
500,019✔
1192
                        c.argBuf = append(c.argBuf, buf[c.as:i-c.drop]...)
500,019✔
1193
                }
500,019✔
1194
                // Check for violations of control line length here. Note that this is not
1195
                // exact at all but the performance hit is too great to be precise, and
1196
                // catching here should prevent memory exhaustion attacks.
1197
                if err := c.overMaxControlLineLimit(c.argBuf, mcl); err != nil {
500,347✔
1198
                        return err
1✔
1199
                }
1✔
1200
        }
1201

1202
        // Check for split msg
1203
        if (c.state == MSG_PAYLOAD || c.state == MSG_END_R || c.state == MSG_END_N) && c.msgBuf == nil {
14,335,011✔
1204
                // We need to clone the pubArg if it is still referencing the
1,282,487✔
1205
                // read buffer and we are not able to process the msg.
1,282,487✔
1206

1,282,487✔
1207
                if c.argBuf == nil {
2,564,974✔
1208
                        // Works also for MSG_ARG, when message comes from ROUTE or GATEWAY.
1,282,487✔
1209
                        if err := c.clonePubArg(lmsg); err != nil {
1,282,487✔
1210
                                goto parseErr
×
1211
                        }
1212
                }
1213

1214
                // If we will overflow the scratch buffer, just create a
1215
                // new buffer to hold the split message.
1216
                if c.pa.size > cap(c.scratch)-len(c.argBuf) {
1,317,362✔
1217
                        lrem := len(buf[c.as:])
34,875✔
1218
                        // Consider it a protocol error when the remaining payload
34,875✔
1219
                        // is larger than the reported size for PUB. It can happen
34,875✔
1220
                        // when processing incomplete messages from rogue clients.
34,875✔
1221
                        if lrem > c.pa.size+LEN_CR_LF {
34,875✔
1222
                                goto parseErr
×
1223
                        }
1224
                        c.msgBuf = make([]byte, lrem, c.pa.size+LEN_CR_LF)
34,875✔
1225
                        copy(c.msgBuf, buf[c.as:])
34,875✔
1226
                } else {
1,247,612✔
1227
                        c.msgBuf = c.scratch[len(c.argBuf):len(c.argBuf)]
1,247,612✔
1228
                        c.msgBuf = append(c.msgBuf, (buf[c.as:])...)
1,247,612✔
1229
                }
1,247,612✔
1230
        }
1231

1232
        return nil
13,052,524✔
1233

13,052,524✔
1234
authErr:
13,052,524✔
1235
        c.authViolation()
29✔
1236
        return ErrAuthentication
29✔
1237

29✔
1238
parseErr:
29✔
1239
        c.sendErr("Unknown Protocol Operation")
4✔
1240
        snip := protoSnippet(i, PROTO_SNIPPET_SIZE, buf)
4✔
1241
        err := fmt.Errorf("%s parser ERROR, state=%d, i=%d: proto='%s...'", c.kindString(), c.state, i, snip)
4✔
1242
        return err
4✔
1243
}
1244

1245
func protoSnippet(start, max int, buf []byte) string {
5✔
1246
        stop := start + max
5✔
1247
        bufSize := len(buf)
5✔
1248
        if start >= bufSize {
5✔
1249
                return `""`
×
1250
        }
×
1251
        if stop > bufSize {
8✔
1252
                stop = bufSize - 1
3✔
1253
        }
3✔
1254
        return fmt.Sprintf("%q", buf[start:stop])
5✔
1255
}
1256

1257
// Check if the length of buffer `arg` is over the max control line limit `mcl`.
1258
// If so, an error is sent to the client and the connection is closed.
1259
// The error ErrMaxControlLine is returned.
1260
func (c *client) overMaxControlLineLimit(arg []byte, mcl int32) error {
21,987,295✔
1261
        if c.kind != CLIENT {
36,604,201✔
1262
                return nil
14,616,906✔
1263
        }
14,616,906✔
1264
        if len(arg) > int(mcl) {
7,370,390✔
1265
                err := NewErrorCtx(ErrMaxControlLine, "State %d, max_control_line %d, Buffer len %d (snip: %s...)",
1✔
1266
                        c.state, int(mcl), len(c.argBuf), protoSnippet(0, MAX_CONTROL_LINE_SNIPPET_SIZE, arg))
1✔
1267
                c.sendErr(err.Error())
1✔
1268
                c.closeConnection(MaxControlLineExceeded)
1✔
1269
                return err
1✔
1270
        }
1✔
1271
        return nil
7,370,388✔
1272
}
1273

1274
// clonePubArg is used when the split buffer scenario has the pubArg in the existing read buffer, but
1275
// we need to hold onto it into the next read.
1276
func (c *client) clonePubArg(lmsg bool) error {
1,282,487✔
1277
        // Just copy and re-process original arg buffer.
1,282,487✔
1278
        c.argBuf = c.scratch[:0]
1,282,487✔
1279
        c.argBuf = append(c.argBuf, c.pa.arg...)
1,282,487✔
1280

1,282,487✔
1281
        switch c.kind {
1,282,487✔
1282
        case ROUTER, GATEWAY:
1,241,623✔
1283
                if lmsg {
1,242,215✔
1284
                        return c.processRoutedOriginClusterMsgArgs(c.argBuf)
592✔
1285
                }
592✔
1286
                if c.pa.hdr < 0 {
2,409,267✔
1287
                        return c.processRoutedMsgArgs(c.argBuf)
1,168,236✔
1288
                } else {
1,241,031✔
1289
                        return c.processRoutedHeaderMsgArgs(c.argBuf)
72,795✔
1290
                }
72,795✔
1291
        case LEAF:
1,591✔
1292
                if c.pa.hdr < 0 {
3,057✔
1293
                        return c.processLeafMsgArgs(c.argBuf)
1,466✔
1294
                } else {
1,591✔
1295
                        return c.processLeafHeaderMsgArgs(c.argBuf)
125✔
1296
                }
125✔
1297
        default:
39,273✔
1298
                if c.pa.hdr < 0 {
77,511✔
1299
                        return c.processPub(c.argBuf)
38,238✔
1300
                } else {
39,273✔
1301
                        return c.processHeaderPub(c.argBuf, nil)
1,035✔
1302
                }
1,035✔
1303
        }
1304
}
1305

1306
func (ps *parseState) getHeader() http.Header {
810✔
1307
        if ps.header == nil {
1,620✔
1308
                if hdr := ps.pa.hdr; hdr > 0 {
883✔
1309
                        reader := bufio.NewReader(bytes.NewReader(ps.msgBuf[0:hdr]))
73✔
1310
                        tp := textproto.NewReader(reader)
73✔
1311
                        tp.ReadLine() // skip over first line, contains version
73✔
1312
                        if mimeHeader, err := tp.ReadMIMEHeader(); err == nil {
146✔
1313
                                ps.header = http.Header(mimeHeader)
73✔
1314
                        }
73✔
1315
                }
1316
        }
1317
        return ps.header
810✔
1318
}
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