Skip to content

fix: surface malformed handshake packets instead of trapping - #5

Merged
datlechin merged 1 commit into
tablepro-mainfrom
fix/server-side-piggyback-decode-trap
Jun 15, 2026
Merged

datlechin merged 1 commit into
tablepro-mainfrom
fix/server-side-piggyback-decode-trap

Conversation

@datlechin

Copy link
Copy Markdown
Member

Problem

Decoding a server packet during the connection handshake could hit a Swift runtime trap (brk #1) on the NIO channel thread. That crashes the whole process and cannot be caught by the connection's error handling, so the app dies on connect with no surfaced error.

Proven by symbolicating a crashed OracleDriver build (TablePro #1683, UUID-matched): the trap was in ServerSidePiggyback.decode, the .sync case, at a non-throwing moveReaderIndex(forwardBy:). Oracle 12c+ sends a .sync server-side piggyback during normal session setup; on some servers the packet is shorter than the decoder's fixed skips, so the move runs past the buffer end and traps. It is server/version specific, which is why only some users hit it.

Changes

  • ServerSidePiggyback.decode: replace every non-throwing moveReaderIndex(forwardBy:) with throwingMoveReaderIndex, so a short sync/sessRet/ltxID/acReplayContext packet surfaces an OraclePartialDecodingError instead of trapping.
  • OracleChannelHandler: a server-side piggyback was handled with fatalError (the comment claimed it only happens with LOBs and "should never happen"). It is informational session metadata; log it and keep waiting. This is a second crash on the same path.
  • Protocol.decode: the charset index ix = 6 + fdo[5] + fdo[6] used UInt8 arithmetic (overflows) and unguarded fdo[] subscripts. Compute in Int, bounds-check the fdo buffer, and widen before shifting so the national charset id is computed correctly.
  • BackendError.decode: writing batch error offsets/messages indexed past the batch array when the server reported more offsets/messages than codes. Guard the index.
  • ByteBuffer UB/SB readers and skipUB: preconditionFailure/fatalError on out-of-range or short length prefixes. Return nil or throw instead.

Tests

  • ServerSidePiggybackTests: a truncated sync/sessRet throws instead of trapping; a well-formed sync decodes; a short piggyback inside a data packet surfaces an error.
  • ProtocolMessageTests: a short FDO throws; a well-formed FDO computes the full 16-bit charset id.
  • BackendErrorTests: more batch offsets than codes does not trap.
  • ByteBufferExtensionTests: UB/SB readers return nil and throwingSkipUB throws on out-of-range/short length.

Local swift test is blocked here by an Xcode 27 beta toolchain issue in a dependency; CI validates the full build and tests.

Decoding a server packet during the connection handshake could hit a Swift
runtime trap on the NIO channel thread, which crashes the whole process and
cannot be caught by the connection's error handling.

- ServerSidePiggyback.decode skipped fields with the non-throwing
  moveReaderIndex(forwardBy:), so a short sync/sessRet piggyback (sent by
  Oracle 12c+ during normal session setup) trapped past the buffer end.
  Use throwingMoveReaderIndex so a short packet surfaces a decoding error.
- OracleChannelHandler treated every server-side piggyback as a fatalError.
  Handle it as informational (sync, sessRet, ...) and keep waiting.
- Protocol.decode computed the charset index with UInt8 arithmetic (which
  overflows) and unguarded fdo[] subscripts. Compute in Int, bounds-check the
  fdo buffer, and widen before shifting so the national charset id is correct.
- BackendError.decode wrote batch error offsets/messages past the batch array
  when the server reported more than the code count. Guard the index.
- The UB/SB readers and skipUB hit preconditionFailure/fatalError on
  out-of-range or short length prefixes. Return nil or throw instead.

Adds OracleNIOTests coverage for each path.
@datlechin
datlechin force-pushed the fix/server-side-piggyback-decode-trap branch from b2a5e27 to 8b39c44 Compare June 15, 2026 09:48
@datlechin
datlechin changed the base branch from main to tablepro-main June 15, 2026 09:48
@datlechin
datlechin merged commit 2919d73 into tablepro-main Jun 15, 2026
@datlechin
datlechin deleted the fix/server-side-piggyback-decode-trap branch June 15, 2026 10:06
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 this pull request may close these issues.

1 participant