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

nats-io / nats-server / 20422154122

17 Dec 2025 03:32PM UTC coverage: 84.565% (-0.02%) from 84.582%
20422154122

push

github

web-flow
NRG: Fix single node election (#7642)

This commit fixes single node election: previously, a single node would
simply store its vote, and never check if it already reached a majority.
So it would never transition to the leader state.

Signed-off-by: Daniele Sciascia <daniele@nats.io>

74022 of 87533 relevant lines covered (84.56%)

359359.34 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 {
7,544,925✔
137
        // Branch out to mqtt clients. c.mqtt is immutable, but should it become
7,544,925✔
138
        // an issue (say data race detection), we could branch outside in readLoop
7,544,925✔
139
        if c.isMqtt() {
7,547,308✔
140
                return c.mqttParse(buf)
2,383✔
141
        }
2,383✔
142
        var i int
7,542,542✔
143
        var b byte
7,542,542✔
144
        var lmsg bool
7,542,542✔
145

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

7,542,542✔
155
        // Move to loop instead of range syntax to allow jumping of i
7,542,542✔
156
        for i = 0; i < len(buf); i++ {
649,796,976✔
157
                b = buf[i]
642,254,434✔
158

642,254,434✔
159
                switch c.state {
642,254,434✔
160
                case OP_START:
16,136,777✔
161
                        c.op = b
16,136,777✔
162
                        if b != 'C' && b != 'c' {
32,229,770✔
163
                                if authSet {
16,093,007✔
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 {
16,092,981✔
190
                                        // Use auth violation since no CONNECT was sent.
×
191
                                        // It could be a parseErr too.
×
192
                                        goto authErr
×
193
                                }
194
                        }
195
                        switch b {
16,136,765✔
196
                        case 'P', 'p':
6,990,817✔
197
                                c.state = OP_P
6,990,817✔
198
                        case 'H', 'h':
795,703✔
199
                                c.state = OP_H
795,703✔
200
                        case 'S', 's':
135,526✔
201
                                c.state = OP_S
135,526✔
202
                        case 'U', 'u':
13,337✔
203
                                c.state = OP_U
13,337✔
204
                        case 'R', 'r':
7,894,933✔
205
                                if c.kind == CLIENT {
7,894,933✔
206
                                        goto parseErr
×
207
                                } else {
7,894,933✔
208
                                        c.state = OP_R
7,894,933✔
209
                                }
7,894,933✔
210
                        case 'L', 'l':
174,900✔
211
                                if c.kind != LEAF && c.kind != ROUTER {
174,900✔
212
                                        goto parseErr
×
213
                                } else {
174,900✔
214
                                        c.state = OP_L
174,900✔
215
                                }
174,900✔
216
                        case 'A', 'a':
31✔
217
                                if c.kind == CLIENT {
31✔
218
                                        goto parseErr
×
219
                                } else {
31✔
220
                                        c.state = OP_A
31✔
221
                                }
31✔
222
                        case 'C', 'c':
43,784✔
223
                                c.state = OP_C
43,784✔
224
                        case 'I', 'i':
87,544✔
225
                                c.state = OP_I
87,544✔
226
                        case '+':
3✔
227
                                c.state = OP_PLUS
3✔
228
                        case '-':
175✔
229
                                c.state = OP_MINUS
175✔
230
                        default:
12✔
231
                                goto parseErr
12✔
232
                        }
233
                case OP_H:
795,703✔
234
                        switch b {
795,703✔
235
                        case 'P', 'p':
341,786✔
236
                                c.state = OP_HP
341,786✔
237
                        case 'M', 'm':
453,917✔
238
                                c.state = OP_HM
453,917✔
239
                        default:
×
240
                                goto parseErr
×
241
                        }
242
                case OP_HP:
341,786✔
243
                        switch b {
341,786✔
244
                        case 'U', 'u':
341,786✔
245
                                c.state = OP_HPU
341,786✔
246
                        default:
×
247
                                goto parseErr
×
248
                        }
249
                case OP_HPU:
341,786✔
250
                        switch b {
341,786✔
251
                        case 'B', 'b':
341,786✔
252
                                c.state = OP_HPUB
341,786✔
253
                        default:
×
254
                                goto parseErr
×
255
                        }
256
                case OP_HPUB:
341,786✔
257
                        switch b {
341,786✔
258
                        case ' ', '\t':
341,786✔
259
                                c.state = OP_HPUB_SPC
341,786✔
260
                        default:
×
261
                                goto parseErr
×
262
                        }
263
                case OP_HPUB_SPC:
341,786✔
264
                        switch b {
341,786✔
265
                        case ' ', '\t':
×
266
                                continue
×
267
                        default:
341,786✔
268
                                c.pa.hdr = 0
341,786✔
269
                                c.state = HPUB_ARG
341,786✔
270
                                c.as = i
341,786✔
271
                        }
272
                case HPUB_ARG:
12,015,081✔
273
                        switch b {
12,015,081✔
274
                        case '\r':
341,786✔
275
                                c.drop = 1
341,786✔
276
                        case '\n':
341,786✔
277
                                var arg []byte
341,786✔
278
                                if c.argBuf != nil {
341,875✔
279
                                        arg = c.argBuf
89✔
280
                                        c.argBuf = nil
89✔
281
                                } else {
341,786✔
282
                                        arg = buf[c.as : i-c.drop]
341,697✔
283
                                }
341,697✔
284
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
341,786✔
285
                                        return err
×
286
                                }
×
287
                                if trace {
404,464✔
288
                                        c.traceInOp("HPUB", arg)
62,678✔
289
                                }
62,678✔
290
                                var remaining []byte
341,786✔
291
                                if i < len(buf) {
683,572✔
292
                                        remaining = buf[i+1:]
341,786✔
293
                                }
341,786✔
294
                                if err := c.processHeaderPub(arg, remaining); err != nil {
341,790✔
295
                                        return err
4✔
296
                                }
4✔
297

298
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
341,782✔
299
                                // If we don't have a saved buffer then jump ahead with
341,782✔
300
                                // the index. If this overruns what is left we fall out
341,782✔
301
                                // and process split buffer.
341,782✔
302
                                if c.msgBuf == nil {
683,564✔
303
                                        i = c.as + c.pa.size - LEN_CR_LF
341,782✔
304
                                }
341,782✔
305
                        default:
11,331,509✔
306
                                if c.argBuf != nil {
11,332,572✔
307
                                        c.argBuf = append(c.argBuf, b)
1,063✔
308
                                }
1,063✔
309
                        }
310
                case OP_HM:
453,917✔
311
                        switch b {
453,917✔
312
                        case 'S', 's':
453,917✔
313
                                c.state = OP_HMS
453,917✔
314
                        default:
×
315
                                goto parseErr
×
316
                        }
317
                case OP_HMS:
453,917✔
318
                        switch b {
453,917✔
319
                        case 'G', 'g':
453,917✔
320
                                c.state = OP_HMSG
453,917✔
321
                        default:
×
322
                                goto parseErr
×
323
                        }
324
                case OP_HMSG:
453,917✔
325
                        switch b {
453,917✔
326
                        case ' ', '\t':
453,917✔
327
                                c.state = OP_HMSG_SPC
453,917✔
328
                        default:
×
329
                                goto parseErr
×
330
                        }
331
                case OP_HMSG_SPC:
453,917✔
332
                        switch b {
453,917✔
333
                        case ' ', '\t':
×
334
                                continue
×
335
                        default:
453,917✔
336
                                c.pa.hdr = 0
453,917✔
337
                                c.state = HMSG_ARG
453,917✔
338
                                c.as = i
453,917✔
339
                        }
340
                case HMSG_ARG:
36,151,509✔
341
                        switch b {
36,151,509✔
342
                        case '\r':
453,917✔
343
                                c.drop = 1
453,917✔
344
                        case '\n':
453,917✔
345
                                var arg []byte
453,917✔
346
                                if c.argBuf != nil {
457,704✔
347
                                        arg = c.argBuf
3,787✔
348
                                        c.argBuf = nil
3,787✔
349
                                } else {
453,917✔
350
                                        arg = buf[c.as : i-c.drop]
450,130✔
351
                                }
450,130✔
352
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
453,917✔
353
                                        return err
×
354
                                }
×
355
                                var err error
453,917✔
356
                                if c.kind == ROUTER || c.kind == GATEWAY {
907,434✔
357
                                        if trace {
455,674✔
358
                                                c.traceInOp("HMSG", arg)
2,157✔
359
                                        }
2,157✔
360
                                        err = c.processRoutedHeaderMsgArgs(arg)
453,517✔
361
                                } else if c.kind == LEAF {
800✔
362
                                        if trace {
554✔
363
                                                c.traceInOp("HMSG", arg)
154✔
364
                                        }
154✔
365
                                        err = c.processLeafHeaderMsgArgs(arg)
400✔
366
                                }
367
                                if err != nil {
453,918✔
368
                                        return err
1✔
369
                                }
1✔
370
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
453,916✔
371

453,916✔
372
                                // jump ahead with the index. If this overruns
453,916✔
373
                                // what is left we fall out and process split
453,916✔
374
                                // buffer.
453,916✔
375
                                i = c.as + c.pa.size - LEN_CR_LF
453,916✔
376
                        default:
35,243,675✔
377
                                if c.argBuf != nil {
35,388,694✔
378
                                        c.argBuf = append(c.argBuf, b)
145,019✔
379
                                }
145,019✔
380
                        }
381
                case OP_P:
6,990,816✔
382
                        switch b {
6,990,816✔
383
                        case 'U', 'u':
6,922,945✔
384
                                c.state = OP_PU
6,922,945✔
385
                        case 'I', 'i':
39,912✔
386
                                c.state = OP_PI
39,912✔
387
                        case 'O', 'o':
27,958✔
388
                                c.state = OP_PO
27,958✔
389
                        default:
1✔
390
                                goto parseErr
1✔
391
                        }
392
                case OP_PU:
6,922,945✔
393
                        switch b {
6,922,945✔
394
                        case 'B', 'b':
6,922,944✔
395
                                c.state = OP_PUB
6,922,944✔
396
                        default:
1✔
397
                                goto parseErr
1✔
398
                        }
399
                case OP_PUB:
6,922,944✔
400
                        switch b {
6,922,944✔
401
                        case ' ', '\t':
6,922,943✔
402
                                c.state = OP_PUB_SPC
6,922,943✔
403
                        default:
1✔
404
                                goto parseErr
1✔
405
                        }
406
                case OP_PUB_SPC:
6,922,943✔
407
                        switch b {
6,922,943✔
408
                        case ' ', '\t':
1✔
409
                                continue
1✔
410
                        default:
6,922,942✔
411
                                c.pa.hdr = -1
6,922,942✔
412
                                c.state = PUB_ARG
6,922,942✔
413
                                c.as = i
6,922,942✔
414
                        }
415
                case PUB_ARG:
119,535,250✔
416
                        switch b {
119,535,250✔
417
                        case '\r':
6,922,934✔
418
                                c.drop = 1
6,922,934✔
419
                        case '\n':
6,922,934✔
420
                                var arg []byte
6,922,934✔
421
                                if c.argBuf != nil {
6,950,409✔
422
                                        arg = c.argBuf
27,475✔
423
                                        c.argBuf = nil
27,475✔
424
                                } else {
6,922,934✔
425
                                        arg = buf[c.as : i-c.drop]
6,895,459✔
426
                                }
6,895,459✔
427
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
6,922,935✔
428
                                        return err
1✔
429
                                }
1✔
430
                                if trace {
7,408,695✔
431
                                        c.traceInOp("PUB", arg)
485,762✔
432
                                }
485,762✔
433
                                if err := c.processPub(arg); err != nil {
6,922,944✔
434
                                        return err
11✔
435
                                }
11✔
436

437
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
6,922,922✔
438
                                // If we don't have a saved buffer then jump ahead with
6,922,922✔
439
                                // the index. If this overruns what is left we fall out
6,922,922✔
440
                                // and process split buffer.
6,922,922✔
441
                                if c.msgBuf == nil {
13,845,844✔
442
                                        i = c.as + c.pa.size - LEN_CR_LF
6,922,922✔
443
                                }
6,922,922✔
444
                        default:
105,689,382✔
445
                                if c.argBuf != nil {
106,184,468✔
446
                                        c.argBuf = append(c.argBuf, b)
495,086✔
447
                                }
495,086✔
448
                        }
449
                case MSG_PAYLOAD:
14,783,459✔
450
                        if c.msgBuf != nil {
15,788,521✔
451
                                // copy as much as we can to the buffer and skip ahead.
1,005,062✔
452
                                toCopy := c.pa.size - len(c.msgBuf)
1,005,062✔
453
                                avail := len(buf) - i
1,005,062✔
454
                                if avail < toCopy {
1,248,003✔
455
                                        toCopy = avail
242,941✔
456
                                }
242,941✔
457
                                if toCopy > 0 {
2,010,124✔
458
                                        start := len(c.msgBuf)
1,005,062✔
459
                                        // This is needed for copy to work.
1,005,062✔
460
                                        c.msgBuf = c.msgBuf[:start+toCopy]
1,005,062✔
461
                                        copy(c.msgBuf[start:], buf[i:i+toCopy])
1,005,062✔
462
                                        // Update our index
1,005,062✔
463
                                        i = (i + toCopy) - 1
1,005,062✔
464
                                } else {
1,005,062✔
465
                                        // Fall back to append if needed.
×
466
                                        c.msgBuf = append(c.msgBuf, b)
×
467
                                }
×
468
                                if len(c.msgBuf) >= c.pa.size {
1,767,183✔
469
                                        c.state = MSG_END_R
762,121✔
470
                                }
762,121✔
471
                        } else if i-c.as+1 >= c.pa.size {
27,556,794✔
472
                                c.state = MSG_END_R
13,778,397✔
473
                        }
13,778,397✔
474
                case MSG_END_R:
14,540,516✔
475
                        if b != '\r' {
14,540,519✔
476
                                goto parseErr
3✔
477
                        }
478
                        if c.msgBuf != nil {
15,308,874✔
479
                                c.msgBuf = append(c.msgBuf, b)
768,361✔
480
                        }
768,361✔
481
                        c.state = MSG_END_N
14,540,513✔
482
                case MSG_END_N:
14,540,500✔
483
                        if b != '\n' {
14,540,501✔
484
                                goto parseErr
1✔
485
                        }
486
                        if c.msgBuf != nil {
15,311,341✔
487
                                c.msgBuf = append(c.msgBuf, b)
770,842✔
488
                        } else {
14,540,499✔
489
                                c.msgBuf = buf[c.as : i+1]
13,769,657✔
490
                        }
13,769,657✔
491

492
                        var mt *msgTrace
14,540,499✔
493
                        if c.pa.hdr > 0 {
15,336,648✔
494
                                mt = c.initMsgTrace()
796,149✔
495
                        }
796,149✔
496
                        // Check for mappings.
497
                        if (c.kind == CLIENT || c.kind == LEAF) && c.in.flags.isSet(hasMappings) {
14,573,389✔
498
                                changed := c.selectMappedSubject()
32,890✔
499
                                if changed {
45,485✔
500
                                        if trace {
12,673✔
501
                                                c.traceInOp("MAPPING", []byte(fmt.Sprintf("%s -> %s", c.pa.mapped, c.pa.subject)))
78✔
502
                                        }
78✔
503
                                        // c.pa.subject is the subject the original is now mapped to.
504
                                        mt.addSubjectMappingEvent(c.pa.subject)
12,595✔
505
                                }
506
                        }
507
                        if trace {
15,100,154✔
508
                                c.traceMsg(c.msgBuf)
559,655✔
509
                        }
559,655✔
510

511
                        c.processInboundMsg(c.msgBuf)
14,540,499✔
512

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

1,174,137✔
659
                                switch c.kind {
1,174,137✔
660
                                case CLIENT:
135,520✔
661
                                        if trace {
146,379✔
662
                                                c.traceInOp("SUB", arg)
10,859✔
663
                                        }
10,859✔
664
                                        err = c.parseSub(arg, false)
135,520✔
665
                                case ROUTER:
844,704✔
666
                                        switch c.op {
844,704✔
667
                                        case 'R', 'r':
832,977✔
668
                                                if trace {
835,843✔
669
                                                        c.traceInOp("RS+", arg)
2,866✔
670
                                                }
2,866✔
671
                                                err = c.processRemoteSub(arg, false)
832,977✔
672
                                        case 'L', 'l':
11,727✔
673
                                                if trace {
11,921✔
674
                                                        c.traceInOp("LS+", arg)
194✔
675
                                                }
194✔
676
                                                err = c.processRemoteSub(arg, true)
11,727✔
677
                                        }
678
                                case GATEWAY:
160,906✔
679
                                        if trace {
161,321✔
680
                                                c.traceInOp("RS+", arg)
415✔
681
                                        }
415✔
682
                                        err = c.processGatewayRSub(arg)
160,906✔
683
                                case LEAF:
33,007✔
684
                                        if trace {
33,128✔
685
                                                c.traceInOp("LS+", arg)
121✔
686
                                        }
121✔
687
                                        err = c.processLeafSub(arg)
33,007✔
688
                                }
689
                                if err != nil {
1,174,141✔
690
                                        return err
4✔
691
                                }
4✔
692
                                c.drop, c.as, c.state = 0, i+1, OP_START
1,174,133✔
693
                        default:
41,590,444✔
694
                                if c.argBuf != nil {
42,287,551✔
695
                                        c.argBuf = append(c.argBuf, b)
697,107✔
696
                                }
697,107✔
697
                        }
698
                case OP_L:
174,900✔
699
                        switch b {
174,900✔
700
                        case 'S', 's':
59,269✔
701
                                c.state = OP_LS
59,269✔
702
                        case 'M', 'm':
115,631✔
703
                                c.state = OP_M
115,631✔
704
                        default:
×
705
                                goto parseErr
×
706
                        }
707
                case OP_LS:
59,268✔
708
                        switch b {
59,268✔
709
                        case '+':
44,734✔
710
                                c.state = OP_SUB
44,734✔
711
                        case '-':
14,534✔
712
                                c.state = OP_UNSUB
14,534✔
713
                        default:
×
714
                                goto parseErr
×
715
                        }
716
                case OP_R:
7,894,933✔
717
                        switch b {
7,894,933✔
718
                        case 'S', 's':
1,188,628✔
719
                                c.state = OP_RS
1,188,628✔
720
                        case 'M', 'm':
6,706,305✔
721
                                c.state = OP_M
6,706,305✔
722
                        default:
×
723
                                goto parseErr
×
724
                        }
725
                case OP_RS:
1,188,627✔
726
                        switch b {
1,188,627✔
727
                        case '+':
993,897✔
728
                                c.state = OP_SUB
993,897✔
729
                        case '-':
194,730✔
730
                                c.state = OP_UNSUB
194,730✔
731
                        default:
×
732
                                goto parseErr
×
733
                        }
734
                case OP_U:
13,337✔
735
                        switch b {
13,337✔
736
                        case 'N', 'n':
13,336✔
737
                                c.state = OP_UN
13,336✔
738
                        default:
1✔
739
                                goto parseErr
1✔
740
                        }
741
                case OP_UN:
13,336✔
742
                        switch b {
13,336✔
743
                        case 'S', 's':
13,335✔
744
                                c.state = OP_UNS
13,335✔
745
                        default:
1✔
746
                                goto parseErr
1✔
747
                        }
748
                case OP_UNS:
13,335✔
749
                        switch b {
13,335✔
750
                        case 'U', 'u':
13,334✔
751
                                c.state = OP_UNSU
13,334✔
752
                        default:
1✔
753
                                goto parseErr
1✔
754
                        }
755
                case OP_UNSU:
13,334✔
756
                        switch b {
13,334✔
757
                        case 'B', 'b':
13,333✔
758
                                c.state = OP_UNSUB
13,333✔
759
                        default:
1✔
760
                                goto parseErr
1✔
761
                        }
762
                case OP_UNSUB:
222,597✔
763
                        switch b {
222,597✔
764
                        case ' ', '\t':
222,591✔
765
                                c.state = OP_UNSUB_SPC
222,591✔
766
                        default:
6✔
767
                                goto parseErr
6✔
768
                        }
769
                case OP_UNSUB_SPC:
222,607✔
770
                        switch b {
222,607✔
771
                        case ' ', '\t':
17✔
772
                                continue
17✔
773
                        default:
222,590✔
774
                                c.state = UNSUB_ARG
222,590✔
775
                                c.as = i
222,590✔
776
                        }
777
                case UNSUB_ARG:
6,213,642✔
778
                        switch b {
6,213,642✔
779
                        case '\r':
222,585✔
780
                                c.drop = 1
222,585✔
781
                        case '\n':
222,587✔
782
                                var arg []byte
222,587✔
783
                                if c.argBuf != nil {
229,195✔
784
                                        arg = c.argBuf
6,608✔
785
                                        c.argBuf = nil
6,608✔
786
                                } else {
222,587✔
787
                                        arg = buf[c.as : i-c.drop]
215,979✔
788
                                }
215,979✔
789
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
222,587✔
790
                                        return err
×
791
                                }
×
792
                                var err error
222,587✔
793

222,587✔
794
                                switch c.kind {
222,587✔
795
                                case CLIENT:
13,327✔
796
                                        if trace {
23,937✔
797
                                                c.traceInOp("UNSUB", arg)
10,610✔
798
                                        }
10,610✔
799
                                        err = c.processUnsub(arg)
13,327✔
800
                                case ROUTER:
164,384✔
801
                                        if trace && c.srv != nil {
164,772✔
802
                                                switch c.op {
388✔
803
                                                case 'R', 'r':
194✔
804
                                                        c.traceInOp("RS-", arg)
194✔
805
                                                case 'L', 'l':
194✔
806
                                                        c.traceInOp("LS-", arg)
194✔
807
                                                }
808
                                        }
809
                                        leafUnsub := c.op == 'L' || c.op == 'l'
164,384✔
810
                                        err = c.processRemoteUnsub(arg, leafUnsub)
164,384✔
811
                                case GATEWAY:
41,493✔
812
                                        if trace {
42,684✔
813
                                                c.traceInOp("RS-", arg)
1,191✔
814
                                        }
1,191✔
815
                                        err = c.processGatewayRUnsub(arg)
41,493✔
816
                                case LEAF:
3,383✔
817
                                        if trace {
3,393✔
818
                                                c.traceInOp("LS-", arg)
10✔
819
                                        }
10✔
820
                                        err = c.processLeafUnsub(arg)
3,383✔
821
                                }
822
                                if err != nil {
222,589✔
823
                                        return err
2✔
824
                                }
2✔
825
                                c.drop, c.as, c.state = 0, i+1, OP_START
222,585✔
826
                        default:
5,768,470✔
827
                                if c.argBuf != nil {
5,890,865✔
828
                                        c.argBuf = append(c.argBuf, b)
122,395✔
829
                                }
122,395✔
830
                        }
831
                case OP_PI:
39,912✔
832
                        switch b {
39,912✔
833
                        case 'N', 'n':
39,911✔
834
                                c.state = OP_PIN
39,911✔
835
                        default:
1✔
836
                                goto parseErr
1✔
837
                        }
838
                case OP_PIN:
39,911✔
839
                        switch b {
39,911✔
840
                        case 'G', 'g':
39,910✔
841
                                c.state = OP_PING
39,910✔
842
                        default:
1✔
843
                                goto parseErr
1✔
844
                        }
845
                case OP_PING:
79,825✔
846
                        switch b {
79,825✔
847
                        case '\n':
39,909✔
848
                                if trace {
40,545✔
849
                                        c.traceInOp("PING", nil)
636✔
850
                                }
636✔
851
                                c.processPing()
39,909✔
852
                                c.drop, c.state = 0, OP_START
39,909✔
853
                        }
854
                case OP_PO:
27,958✔
855
                        switch b {
27,958✔
856
                        case 'N', 'n':
27,957✔
857
                                c.state = OP_PON
27,957✔
858
                        default:
1✔
859
                                goto parseErr
1✔
860
                        }
861
                case OP_PON:
27,957✔
862
                        switch b {
27,957✔
863
                        case 'G', 'g':
27,956✔
864
                                c.state = OP_PONG
27,956✔
865
                        default:
1✔
866
                                goto parseErr
1✔
867
                        }
868
                case OP_PONG:
55,917✔
869
                        switch b {
55,917✔
870
                        case '\n':
27,955✔
871
                                if trace {
28,006✔
872
                                        c.traceInOp("PONG", nil)
51✔
873
                                }
51✔
874
                                c.processPong()
27,955✔
875
                                c.drop, c.state = 0, OP_START
27,955✔
876
                        }
877
                case OP_C:
43,784✔
878
                        switch b {
43,784✔
879
                        case 'O', 'o':
43,783✔
880
                                c.state = OP_CO
43,783✔
881
                        default:
1✔
882
                                goto parseErr
1✔
883
                        }
884
                case OP_CO:
43,783✔
885
                        switch b {
43,783✔
886
                        case 'N', 'n':
43,782✔
887
                                c.state = OP_CON
43,782✔
888
                        default:
1✔
889
                                goto parseErr
1✔
890
                        }
891
                case OP_CON:
43,782✔
892
                        switch b {
43,782✔
893
                        case 'N', 'n':
43,781✔
894
                                c.state = OP_CONN
43,781✔
895
                        default:
1✔
896
                                goto parseErr
1✔
897
                        }
898
                case OP_CONN:
43,781✔
899
                        switch b {
43,781✔
900
                        case 'E', 'e':
43,780✔
901
                                c.state = OP_CONNE
43,780✔
902
                        default:
1✔
903
                                goto parseErr
1✔
904
                        }
905
                case OP_CONNE:
43,780✔
906
                        switch b {
43,780✔
907
                        case 'C', 'c':
43,779✔
908
                                c.state = OP_CONNEC
43,779✔
909
                        default:
1✔
910
                                goto parseErr
1✔
911
                        }
912
                case OP_CONNEC:
43,779✔
913
                        switch b {
43,779✔
914
                        case 'T', 't':
43,777✔
915
                                c.state = OP_CONNECT
43,777✔
916
                        default:
2✔
917
                                goto parseErr
2✔
918
                        }
919
                case OP_CONNECT:
87,554✔
920
                        switch b {
87,554✔
921
                        case ' ', '\t':
43,777✔
922
                                continue
43,777✔
923
                        default:
43,777✔
924
                                c.state = CONNECT_ARG
43,777✔
925
                                c.as = i
43,777✔
926
                        }
927
                case CONNECT_ARG:
9,271,640✔
928
                        switch b {
9,271,640✔
929
                        case '\r':
43,776✔
930
                                c.drop = 1
43,776✔
931
                        case '\n':
43,777✔
932
                                var arg []byte
43,777✔
933
                                if c.argBuf != nil {
46,198✔
934
                                        arg = c.argBuf
2,421✔
935
                                        c.argBuf = nil
2,421✔
936
                                } else {
43,777✔
937
                                        arg = buf[c.as : i-c.drop]
41,356✔
938
                                }
41,356✔
939
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
43,777✔
940
                                        return err
×
941
                                }
×
942
                                if trace {
44,463✔
943
                                        c.traceInOp("CONNECT", removeSecretsFromTrace(arg))
686✔
944
                                }
686✔
945
                                if err := c.processConnect(arg); err != nil {
44,103✔
946
                                        return err
326✔
947
                                }
326✔
948
                                c.drop, c.state = 0, OP_START
43,451✔
949
                                // Reset notion on authSet
43,451✔
950
                                c.mu.Lock()
43,451✔
951
                                authSet = c.awaitingAuth()
43,451✔
952
                                c.mu.Unlock()
43,451✔
953
                        default:
9,184,087✔
954
                                if c.argBuf != nil {
9,939,368✔
955
                                        c.argBuf = append(c.argBuf, b)
755,281✔
956
                                }
755,281✔
957
                        }
958
                case OP_M:
6,821,936✔
959
                        switch b {
6,821,936✔
960
                        case 'S', 's':
6,821,936✔
961
                                c.state = OP_MS
6,821,936✔
962
                        default:
×
963
                                goto parseErr
×
964
                        }
965
                case OP_MS:
6,821,936✔
966
                        switch b {
6,821,936✔
967
                        case 'G', 'g':
6,821,936✔
968
                                c.state = OP_MSG
6,821,936✔
969
                        default:
×
970
                                goto parseErr
×
971
                        }
972
                case OP_MSG:
6,821,936✔
973
                        switch b {
6,821,936✔
974
                        case ' ', '\t':
6,821,936✔
975
                                c.state = OP_MSG_SPC
6,821,936✔
976
                        default:
×
977
                                goto parseErr
×
978
                        }
979
                case OP_MSG_SPC:
6,821,936✔
980
                        switch b {
6,821,936✔
981
                        case ' ', '\t':
×
982
                                continue
×
983
                        default:
6,821,936✔
984
                                c.pa.hdr = -1
6,821,936✔
985
                                c.state = MSG_ARG
6,821,936✔
986
                                c.as = i
6,821,936✔
987
                        }
988
                case MSG_ARG:
249,451,366✔
989
                        switch b {
249,451,366✔
990
                        case '\r':
6,821,934✔
991
                                c.drop = 1
6,821,934✔
992
                        case '\n':
6,821,934✔
993
                                var arg []byte
6,821,934✔
994
                                if c.argBuf != nil {
6,989,636✔
995
                                        arg = c.argBuf
167,702✔
996
                                        c.argBuf = nil
167,702✔
997
                                } else {
6,821,934✔
998
                                        arg = buf[c.as : i-c.drop]
6,654,232✔
999
                                }
6,654,232✔
1000
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
6,821,934✔
1001
                                        return err
×
1002
                                }
×
1003
                                var err error
6,821,934✔
1004
                                if c.kind == ROUTER || c.kind == GATEWAY {
13,535,040✔
1005
                                        switch c.op {
6,713,106✔
1006
                                        case 'R', 'r':
6,706,303✔
1007
                                                if trace {
6,714,916✔
1008
                                                        c.traceInOp("RMSG", arg)
8,613✔
1009
                                                }
8,613✔
1010
                                                err = c.processRoutedMsgArgs(arg)
6,706,303✔
1011
                                        case 'L', 'l':
6,803✔
1012
                                                if trace {
7,083✔
1013
                                                        c.traceInOp("LMSG", arg)
280✔
1014
                                                }
280✔
1015
                                                lmsg = true
6,803✔
1016
                                                err = c.processRoutedOriginClusterMsgArgs(arg)
6,803✔
1017
                                        }
1018
                                } else if c.kind == LEAF {
217,656✔
1019
                                        if trace {
108,847✔
1020
                                                c.traceInOp("LMSG", arg)
19✔
1021
                                        }
19✔
1022
                                        err = c.processLeafMsgArgs(arg)
108,828✔
1023
                                }
1024
                                if err != nil {
6,821,935✔
1025
                                        return err
1✔
1026
                                }
1✔
1027
                                c.drop, c.as, c.state = 0, i+1, MSG_PAYLOAD
6,821,933✔
1028

6,821,933✔
1029
                                // jump ahead with the index. If this overruns
6,821,933✔
1030
                                // what is left we fall out and process split
6,821,933✔
1031
                                // buffer.
6,821,933✔
1032
                                i = c.as + c.pa.size - LEN_CR_LF
6,821,933✔
1033
                        default:
235,807,498✔
1034
                                if c.argBuf != nil {
237,754,716✔
1035
                                        c.argBuf = append(c.argBuf, b)
1,947,218✔
1036
                                }
1,947,218✔
1037
                        }
1038
                case OP_I:
87,544✔
1039
                        switch b {
87,544✔
1040
                        case 'N', 'n':
87,543✔
1041
                                c.state = OP_IN
87,543✔
1042
                        default:
1✔
1043
                                goto parseErr
1✔
1044
                        }
1045
                case OP_IN:
87,543✔
1046
                        switch b {
87,543✔
1047
                        case 'F', 'f':
87,542✔
1048
                                c.state = OP_INF
87,542✔
1049
                        default:
1✔
1050
                                goto parseErr
1✔
1051
                        }
1052
                case OP_INF:
87,542✔
1053
                        switch b {
87,542✔
1054
                        case 'O', 'o':
87,541✔
1055
                                c.state = OP_INFO
87,541✔
1056
                        default:
1✔
1057
                                goto parseErr
1✔
1058
                        }
1059
                case OP_INFO:
175,083✔
1060
                        switch b {
175,083✔
1061
                        case ' ', '\t':
87,542✔
1062
                                continue
87,542✔
1063
                        default:
87,541✔
1064
                                c.state = INFO_ARG
87,541✔
1065
                                c.as = i
87,541✔
1066
                        }
1067
                case INFO_ARG:
33,146,894✔
1068
                        switch b {
33,146,894✔
1069
                        case '\r':
87,536✔
1070
                                c.drop = 1
87,536✔
1071
                        case '\n':
87,537✔
1072
                                var arg []byte
87,537✔
1073
                                if c.argBuf != nil {
95,980✔
1074
                                        arg = c.argBuf
8,443✔
1075
                                        c.argBuf = nil
8,443✔
1076
                                } else {
87,537✔
1077
                                        arg = buf[c.as : i-c.drop]
79,094✔
1078
                                }
79,094✔
1079
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
87,537✔
1080
                                        return err
×
1081
                                }
×
1082
                                if err := c.processInfo(arg); err != nil {
87,538✔
1083
                                        return err
1✔
1084
                                }
1✔
1085
                                c.drop, c.as, c.state = 0, i+1, OP_START
87,536✔
1086
                        default:
32,971,821✔
1087
                                if c.argBuf != nil {
33,941,058✔
1088
                                        c.argBuf = append(c.argBuf, b)
969,237✔
1089
                                }
969,237✔
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:
175✔
1111
                        switch b {
175✔
1112
                        case 'E', 'e':
174✔
1113
                                c.state = OP_MINUS_E
174✔
1114
                        default:
1✔
1115
                                goto parseErr
1✔
1116
                        }
1117
                case OP_MINUS_E:
174✔
1118
                        switch b {
174✔
1119
                        case 'R', 'r':
173✔
1120
                                c.state = OP_MINUS_ER
173✔
1121
                        default:
1✔
1122
                                goto parseErr
1✔
1123
                        }
1124
                case OP_MINUS_ER:
173✔
1125
                        switch b {
173✔
1126
                        case 'R', 'r':
172✔
1127
                                c.state = OP_MINUS_ERR
172✔
1128
                        default:
1✔
1129
                                goto parseErr
1✔
1130
                        }
1131
                case OP_MINUS_ERR:
172✔
1132
                        switch b {
172✔
1133
                        case ' ', '\t':
171✔
1134
                                c.state = OP_MINUS_ERR_SPC
171✔
1135
                        default:
1✔
1136
                                goto parseErr
1✔
1137
                        }
1138
                case OP_MINUS_ERR_SPC:
171✔
1139
                        switch b {
171✔
1140
                        case ' ', '\t':
×
1141
                                continue
×
1142
                        default:
171✔
1143
                                c.state = MINUS_ERR_ARG
171✔
1144
                                c.as = i
171✔
1145
                        }
1146
                case MINUS_ERR_ARG:
6,988✔
1147
                        switch b {
6,988✔
1148
                        case '\r':
171✔
1149
                                c.drop = 1
171✔
1150
                        case '\n':
171✔
1151
                                var arg []byte
171✔
1152
                                if c.argBuf != nil {
172✔
1153
                                        arg = c.argBuf
1✔
1154
                                        c.argBuf = nil
1✔
1155
                                } else {
171✔
1156
                                        arg = buf[c.as : i-c.drop]
170✔
1157
                                }
170✔
1158
                                if err := c.overMaxControlLineLimit(arg, mcl); err != nil {
171✔
1159
                                        return err
×
1160
                                }
×
1161
                                c.processErr(string(arg))
171✔
1162
                                c.drop, c.as, c.state = 0, i+1, OP_START
171✔
1163
                        default:
6,646✔
1164
                                if c.argBuf != nil {
6,646✔
1165
                                        c.argBuf = append(c.argBuf, b)
×
1166
                                }
×
1167
                        }
1168
                default:
×
1169
                        goto parseErr
×
1170
                }
1171
        }
1172

1173
        // Check for split buffer scenarios for any ARG state.
1174
        if c.state == SUB_ARG || c.state == UNSUB_ARG ||
7,542,126✔
1175
                c.state == PUB_ARG || c.state == HPUB_ARG ||
7,542,126✔
1176
                c.state == ASUB_ARG || c.state == AUSUB_ARG ||
7,542,126✔
1177
                c.state == MSG_ARG || c.state == HMSG_ARG ||
7,542,126✔
1178
                c.state == MINUS_ERR_ARG || c.state == CONNECT_ARG || c.state == INFO_ARG {
7,787,008✔
1179

244,882✔
1180
                // Setup a holder buffer to deal with split buffer scenario.
244,882✔
1181
                if c.argBuf == nil {
489,424✔
1182
                        c.argBuf = c.scratch[:0]
244,542✔
1183
                        c.argBuf = append(c.argBuf, buf[c.as:i-c.drop]...)
244,542✔
1184
                }
244,542✔
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 {
244,884✔
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 {
8,313,016✔
1195
                // We need to clone the pubArg if it is still referencing the
770,892✔
1196
                // read buffer and we are not able to process the msg.
770,892✔
1197

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

1223
        return nil
7,542,124✔
1224

7,542,124✔
1225
authErr:
7,542,124✔
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 {
16,313,693✔
1252
        if c.kind != CLIENT {
25,172,647✔
1253
                return nil
8,858,954✔
1254
        }
8,858,954✔
1255
        if len(arg) > int(mcl) {
7,454,742✔
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
7,454,736✔
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 {
770,892✔
1268
        // Just copy and re-process original arg buffer.
770,892✔
1269
        c.argBuf = c.scratch[:0]
770,892✔
1270
        c.argBuf = append(c.argBuf, c.pa.arg...)
770,892✔
1271

770,892✔
1272
        switch c.kind {
770,892✔
1273
        case ROUTER, GATEWAY:
729,807✔
1274
                if lmsg {
730,460✔
1275
                        return c.processRoutedOriginClusterMsgArgs(c.argBuf)
653✔
1276
                }
653✔
1277
                if c.pa.hdr < 0 {
1,403,043✔
1278
                        return c.processRoutedMsgArgs(c.argBuf)
673,889✔
1279
                } else {
729,154✔
1280
                        return c.processRoutedHeaderMsgArgs(c.argBuf)
55,265✔
1281
                }
55,265✔
1282
        case LEAF:
2,115✔
1283
                if c.pa.hdr < 0 {
4,130✔
1284
                        return c.processLeafMsgArgs(c.argBuf)
2,015✔
1285
                } else {
2,115✔
1286
                        return c.processLeafHeaderMsgArgs(c.argBuf)
100✔
1287
                }
100✔
1288
        default:
38,970✔
1289
                if c.pa.hdr < 0 {
77,299✔
1290
                        return c.processPub(c.argBuf)
38,329✔
1291
                } else {
38,970✔
1292
                        return c.processHeaderPub(c.argBuf, nil)
641✔
1293
                }
641✔
1294
        }
1295
}
1296

1297
func (ps *parseState) getHeader() http.Header {
846✔
1298
        if ps.header == nil {
1,692✔
1299
                if hdr := ps.pa.hdr; hdr > 0 {
919✔
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
846✔
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