Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StatusBadEncodingLimitsExceeded on single read request #678

Closed
dtq-two opened this issue Aug 23, 2023 · 9 comments · Fixed by #755
Closed

StatusBadEncodingLimitsExceeded on single read request #678

dtq-two opened this issue Aug 23, 2023 · 9 comments · Fixed by #755
Milestone

Comments

@dtq-two
Copy link

dtq-two commented Aug 23, 2023

After connecting to an OPC UA Server by "Softing dataFEED" I did a simple ReadRequest which resulted in a StatusBadEncodingLimitsExceeded.

The request was done like this:

        // ... create client and connect
	_, err = client.ReadWithContext(ctx, &ua.ReadRequest{
		NodesToRead: []*ua.ReadValueID{{
			NodeID:      ua.MustParseNodeID("ns=0;i=2269"),
			AttributeID: ua.AttributeIDValue,
		}},
	})
	if err != nil {
		return fmt.Errorf("read with context: %w", err)
	}

Debugging showed that the error was created at here:

opcua/ua/variant.go

Lines 117 to 146 in 807c2da

// Decode implements the codec interface.
func (m *Variant) Decode(b []byte) (int, error) {
buf := NewBuffer(b)
m.mask = buf.ReadByte()
// a null value specifies that no other fields are encoded
if m.Type() == TypeIDNull {
return buf.Pos(), buf.Error()
}
// check the type
typ, ok := variantTypeIDToType[m.Type()]
if !ok {
return buf.Pos(), errors.Errorf("invalid type id: %d", m.Type())
}
// read single value and return
if !m.Has(VariantArrayValues) {
m.value = m.decodeValue(buf)
return buf.Pos(), buf.Error()
}
// get total array length (flattened for multi-dimensional arrays)
m.arrayLength = buf.ReadInt32()
// read flattened array elements
n := int(m.arrayLength)
if n < 0 || n > MaxVariantArrayLength {
return buf.Pos(), StatusBadEncodingLimitsExceeded
}

The []byte that was decoded looked like this:

[]uint8 len: 17, cap: 65478, [140,255,255,255,255,96,227,5,236,109,213,217,1,255,255,255,255]

which resulted in an m.arrayLength of -1

Looking at the Node-ID in UaExpert

image

I'm unsure how I could work around this error or fix this issue. Another problem is that the client.State() becomes Disconnected and Reconnecting after this issue without returning back to Connected.

@magiconair
Copy link
Member

Which gopcua version are you using?

Which data structure did you use for the Read response?

Regarding state: lets try to recreate the issue and then we can probably see where it breaks.

@dtq-two
Copy link
Author

dtq-two commented Aug 23, 2023

Which gopcua version are you using?

I was using version v0.3.5

Which data structure did you use for the Read response?

What do you mean? In version v0.3.5 the ReadWithContext returns (*ua.ReadResponse, error), but I'm unsure if this is what you meant.

@magiconair
Copy link
Member

magiconair commented Aug 23, 2023

What do you mean? In version v0.3.5 the ReadWithContext returns (*ua.ReadResponse, error), but I'm unsure if this is what you meant.

I may have confused something here. So you're saying that you get that error just by reading node i=2269, correct?

Can you try this with the latest version v0.5.1?

cd ./examples/read
go run read.go -endpoint opc.tcp://<host:port> -node 'i=2269' -debug

@dtq-two
Copy link
Author

dtq-two commented Aug 23, 2023

That is correct and I upgraded to v0.5.1 with the same results.
This was the output from your suggestion:

% cd ./examples/read
% go run read.go -endpoint opc.tcp://<myhost:myport> -node 'i=2269' -debug
debug: uacp: connecting to opc.tcp://<myhost:myport>
debug: uacp 1: start HEL/ACK handshake
debug: uacp 1: sent HELF with 101 bytes
debug: uacp 1: recv ACKF with 28 bytes
debug: uacp 1: server has no chunk limit. Using 512
debug: uacp 1: recv &uacp.Acknowledge{Version:0x0, ReceiveBufSize:0xffff, SendBufSize:0xffff, MaxMessageSize:0x1000000, MaxChunkCount:0x200}
debug: uasc 1/1: send *ua.OpenSecureChannelRequest with 132 bytes
debug: uacp 1: recv OPNF with 135 bytes
debug: uasc 1/1: recv OPNF with 135 bytes
debug: uasc 1/1: recv *ua.OpenSecureChannelResponse
debug: uasc 1/1: sending *ua.OpenSecureChannelResponse to handler
debug: uasc 1: received security token. channelID=3407 tokenID=1 createdAt=2023-08-23T11:04:50Z lifetime=1h0m0s
debug: uasc 1/2: send *ua.CreateSessionRequest with 308 bytes
debug: uasc 1: security token is refreshed at 2023-08-23T11:49:50Z (45m0s). channelID=3407 tokenID=1
debug: uasc 1: security token expires at 2023-08-23T12:19:50Z. channelID=3407 tokenID=1
debug: uacp 1: recv MSGF with 14763 bytes
debug: uasc 1/2: recv MSGF with 14763 bytes
debug: uasc 1/2: recv *ua.CreateSessionResponse
debug: uasc 1/2: sending *ua.CreateSessionResponse to handler
debug: uasc 1/3: send *ua.ActivateSessionRequest with 156 bytes
debug: uacp 1: recv MSGF with 96 bytes
debug: uasc 1/3: recv MSGF with 96 bytes
debug: uasc 1/3: recv *ua.ActivateSessionResponse
debug: uasc 1/3: sending *ua.ActivateSessionResponse to handler
debug: uasc 1/4: send *ua.ReadRequest with 131 bytes
debug: client: monitor: start
debug: sub: pause
debug: uacp 1: recv MSGF with 299 bytes
debug: uasc 1/4: recv MSGF with 299 bytes
debug: uasc 1/4: recv *ua.ReadResponse
debug: uasc 1/4: sending *ua.ReadResponse to handler
debug: uasc 1/5: send *ua.ReadRequest with 128 bytes
debug: uacp 1: recv MSGF with 82 bytes
debug: uasc 1/5: recv MSGF with 82 bytes
debug: uasc 1/5: err: The message encoding/decoding limits imposed by the stack have been exceeded. StatusBadEncodingLimitsExceeded (0x80080000)
debug: uasc 1/5: sending <nil> to handler
Read failed: The message encoding/decoding limits imposed by the stack have been exceeded. StatusBadEncodingLimitsExceeded (0x80080000)
exit status 1

@magiconair
Copy link
Member

OK, could you capture a tcpdump and attach this here? If you don't want to attach it here you can send it to me via Keybase.

@magiconair
Copy link
Member

I'm having trouble accessing my Keybase account right now. I might be able to get to it later tonight.

@dtq-two
Copy link
Author

dtq-two commented Aug 23, 2023

OK, could you capture a tcpdump and attach this here? If you don't want to attach it here you can send it to me via Keybase.

13:36:24.439016 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [S], seq 3640104227, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 791976457 ecr 0,sackOK,eol], length 0
13:36:24.439541 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [S.], seq 3279600835, ack 3640104228, win 65535, options [mss 1460,nop,wscale 1,sackOK,TS val 42226121 ecr 791976457], length 0
13:36:24.439624 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 1, win 2058, options [nop,nop,TS val 791976457 ecr 42226121], length 0
13:36:24.439745 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [P.], seq 1:102, ack 1, win 2058, options [nop,nop,TS val 791976457 ecr 42226121], length 101
13:36:24.440260 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [P.], seq 1:29, ack 102, win 32717, options [nop,nop,TS val 42226121 ecr 791976457], length 28
13:36:24.440353 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 29, win 2058, options [nop,nop,TS val 791976458 ecr 42226121], length 0
13:36:24.440507 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [P.], seq 102:234, ack 29, win 2058, options [nop,nop,TS val 791976458 ecr 42226121], length 132
13:36:24.440992 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [P.], seq 29:164, ack 234, win 32651, options [nop,nop,TS val 42226121 ecr 791976458], length 135
13:36:24.441076 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 164, win 2056, options [nop,nop,TS val 791976458 ecr 42226121], length 0
13:36:24.441328 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [P.], seq 234:542, ack 164, win 2056, options [nop,nop,TS val 791976458 ecr 42226121], length 308
13:36:24.442739 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 164:1612, ack 542, win 32497, options [nop,nop,TS val 42226121 ecr 791976458], length 1448
13:36:24.442740 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 1612:3060, ack 542, win 32497, options [nop,nop,TS val 42226121 ecr 791976458], length 1448
13:36:24.442809 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 1612, win 2033, options [nop,nop,TS val 791976461 ecr 42226121], length 0
13:36:24.442812 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 3060, win 2011, options [nop,nop,TS val 791976461 ecr 42226121], length 0
13:36:24.443187 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 3060:4508, ack 542, win 32497, options [nop,nop,TS val 42226121 ecr 791976461], length 1448
13:36:24.443188 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 4508:5956, ack 542, win 32497, options [nop,nop,TS val 42226121 ecr 791976461], length 1448
13:36:24.443190 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 5956:7404, ack 542, win 32497, options [nop,nop,TS val 42226121 ecr 791976461], length 1448
13:36:24.443191 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 7404:8852, ack 542, win 32497, options [nop,nop,TS val 42226121 ecr 791976461], length 1448
13:36:24.443238 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 4508, win 2025, options [nop,nop,TS val 791976461 ecr 42226121], length 0
13:36:24.443240 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 5956, win 2002, options [nop,nop,TS val 791976461 ecr 42226121], length 0
13:36:24.443368 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 8852, win 2025, options [nop,nop,TS val 791976461 ecr 42226121], length 0
13:36:24.443589 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 8852:10300, ack 542, win 32497, options [nop,nop,TS val 42226122 ecr 791976461], length 1448
13:36:24.443595 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 10300:11748, ack 542, win 32497, options [nop,nop,TS val 42226122 ecr 791976461], length 1448
13:36:24.443597 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 11748:13196, ack 542, win 32497, options [nop,nop,TS val 42226122 ecr 791976461], length 1448
13:36:24.443601 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], seq 13196:14644, ack 542, win 32497, options [nop,nop,TS val 42226122 ecr 791976461], length 1448
13:36:24.443607 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [P.], seq 14644:14927, ack 542, win 32497, options [nop,nop,TS val 42226122 ecr 791976461], length 283
13:36:24.443672 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 10300, win 2025, options [nop,nop,TS val 791976461 ecr 42226122], length 0
13:36:24.443798 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 14927, win 2020, options [nop,nop,TS val 791976461 ecr 42226122], length 0
13:36:24.443922 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [P.], seq 542:698, ack 14927, win 2048, options [nop,nop,TS val 791976462 ecr 42226122], length 156
13:36:24.444363 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [P.], seq 14927:15023, ack 698, win 32419, options [nop,nop,TS val 42226122 ecr 791976462], length 96
13:36:24.444401 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 15023, win 2046, options [nop,nop,TS val 791976462 ecr 42226122], length 0
13:36:24.444539 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [P.], seq 698:829, ack 15023, win 2048, options [nop,nop,TS val 791976462 ecr 42226122], length 131
13:36:24.444819 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [P.], seq 15023:15322, ack 829, win 32354, options [nop,nop,TS val 42226122 ecr 791976462], length 299
13:36:24.444844 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 15322, win 2043, options [nop,nop,TS val 791976462 ecr 42226122], length 0
13:36:24.444963 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [P.], seq 829:957, ack 15322, win 2048, options [nop,nop,TS val 791976463 ecr 42226122], length 128
13:36:24.445189 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [P.], seq 15322:15404, ack 957, win 32290, options [nop,nop,TS val 42226122 ecr 791976463], length 82
13:36:24.445214 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 15404, win 2046, options [nop,nop,TS val 791976463 ecr 42226122], length 0
13:36:24.445608 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [F.], seq 957, ack 15404, win 2048, options [nop,nop,TS val 791976463 ecr 42226122], length 0
13:36:24.445822 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [.], ack 958, win 32290, options [nop,nop,TS val 42226122 ecr 791976463], length 0
13:36:24.445822 IP 10.10.20.11.4982 > 10.10.10.66.54401: Flags [F.], seq 15404, ack 958, win 32290, options [nop,nop,TS val 42226122 ecr 791976463], length 0
13:36:24.445883 IP 10.10.10.66.54401 > 10.10.20.11.4982: Flags [.], ack 15405, win 2048, options [nop,nop,TS val 791976463 ecr 42226122], length 0


@magiconair
Copy link
Member

Thanks. Can you attach the file? I need to look at the content of the response to understand where it breaks.

@dtq-two
Copy link
Author

dtq-two commented Aug 24, 2023

Thanks. Can you attach the file? I need to look at the content of the response to understand where it breaks.

This zip contains the tcpdump pcap file:

read.pcap.zip

magiconair added a commit that referenced this issue Dec 9, 2024
This patch updates the variant decoder to correctly handle Variant
values with nil arrays. They have a length of -1. The decoder returned
an error of StatusBadEncodingLimitsExceeded which is wrong.

Closes #678
@magiconair magiconair added this to the v0.6.1 milestone Dec 9, 2024
magiconair added a commit that referenced this issue Dec 9, 2024
Fix the Variant encoder and decoder to handle nil slices
correctly by setting the length to -1.

Closes #678
magiconair added a commit that referenced this issue Dec 11, 2024
Issue #678: Fix Variant to handle nil slices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants