PVA: harden handling of protocol sizes - #3878
Conversation
Protocol handling was prone to memory exhaustion because of this pattern: ``` int count = buffer.getInteger(); Whatever[] data = new Whatever[count]; .. then read data from buffer ``` A misconfigured package can contain a huge `count`. This update checks the `count` against the remaining package size, refusing packets that are obviously too small or rather the `count` is obviously too large for the packet size. Along the same lines, one could introduce an upper packet size limit similar to `EPICS_CA_MAX_ARRAY_BYTES` in channel access, but at this time, confirmed in a 2026-07-10 EPICS core telecon, PVA does not impose any such size limits, using all available memory.
|
jacomago
left a comment
There was a problem hiding this comment.
I think making custom exceptions is a good suggestion by sonarlint.
| { | ||
| final int size = PVASize.decodeSize(buffer); | ||
| if (size < 0 || size > buffer.remaining()) | ||
| throw new Exception("Array size " + size + " with only " + buffer.remaining() + " bytes in buffer"); |
There was a problem hiding this comment.
Why not throw this inside PVASize.decodeSize? Then you don't need to update every single type.
There was a problem hiding this comment.
Decoding the size doesn't fail. It parses the size just fine.
The calling code then checks if that size is possible within the context. If not, it throws an exception because we're stuck in a situation with no good way out other than give up and move on with the next message at a higher level. The exception text gives some potential hint to experts, showing if this is about an array size or structure size or string size or ...
Never understood that. Exceptions are not status codes where based on the status code you would know exactly what to do. An exception indicates that something happened about which we can't do anything. |
It's not just about catching custom exceptions (although I don't really understand your point about status codes, dB exception for EntityNotFoundException would makes sense there). Custom exceptions give you data structure when debugging and logging, for example you repeated the same exception message over and over in this pr. If you just make an ArrayBufferOutOFRangeException where you pass in the min, max and size you can write the message once, and format it once. Its also clear in the logs this was the type of exception that failed, I can also quickly find all places that exception is raised rather than exception I have to search for the message string. There are many other advantages I won't bother to list them all, but using raw Exception class is a very bad code smell to me. |


Protocol handling was prone to memory exhaustion because of this pattern:
A misconfigured package can contain a huge
count. This update checks thecountagainst the remaining package size, refusing packets that are obviously too small or rather thecountis obviously too large for the packet size.Along the same lines, one could introduce an upper packet size limit similar to
EPICS_CA_MAX_ARRAY_BYTESin channel access, but at this time, confirmed in a 2026-07-10 EPICS core telecon, PVA does not impose any such size limits, using all available memory.