{"record":{"id":"310ea6ad0b912b8c","repo":"grpc/grpc-java","slug":"invalid-input-expected-span-id-at-offset-pos","errorCode":null,"errorMessage":"Invalid input: expected span ID at offset ${pos}","messagePattern":"Invalid input: expected span ID at offset (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"opentelemetry/src/main/java/io/grpc/opentelemetry/BinaryFormat.java","lineNumber":133,"sourceCode":"      throw new IllegalArgumentException(\"Invalid input: truncated\");\n    }\n    String traceId;\n    String spanId;\n    TraceFlags traceFlags = TraceFlags.getDefault();\n    int pos = 1;\n    if (serialized[pos] == TRACE_ID_FIELD_ID) {\n      traceId = TraceId.fromBytes(\n          Arrays.copyOfRange(serialized, pos + ID_SIZE, pos + ID_SIZE + TRACE_ID_SIZE));\n      pos += ID_SIZE + TRACE_ID_SIZE;\n    } else {\n      throw new IllegalArgumentException(\"Invalid input: expected trace ID at offset \" + pos);\n    }\n    if (serialized[pos] == SPAN_ID_FIELD_ID) {\n      spanId = SpanId.fromBytes(\n          Arrays.copyOfRange(serialized, pos + ID_SIZE, pos + ID_SIZE + SPAN_ID_SIZE));\n      pos += ID_SIZE + SPAN_ID_SIZE;\n    } else {\n      throw new IllegalArgumentException(\"Invalid input: expected span ID at offset \" + pos);\n    }\n    if (serialized.length > pos && serialized[pos] == TRACE_FLAG_FIELD_ID) {\n      if (serialized.length < ALL_FORMAT_LENGTH) {\n        throw new IllegalArgumentException(\"Invalid input: truncated\");\n      }\n      traceFlags = TraceFlags.fromByte(serialized[pos + ID_SIZE]);\n    }\n    return SpanContext.create(traceId, spanId, traceFlags, TraceState.getDefault());\n  }\n}\n","sourceCodeStart":115,"sourceCodeEnd":144,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/opentelemetry/src/main/java/io/grpc/opentelemetry/BinaryFormat.java#L115-L144","documentation":"After successfully parsing the trace ID, BinaryFormat.parseBytes expects the next byte to be the span-ID field ID (0x01) followed by an 8-byte span ID. This IllegalArgumentException is thrown when the byte at the current offset does not match SPAN_ID_FIELD_ID, indicating the serialized span context is not in the expected OpenTelemetry binary format.","triggerScenarios":"Calling parseBytes with bytes where the span-ID field is missing, reordered, or replaced — e.g. a truncated payload containing version + trace ID but no span-ID field, or bytes written by a non-conforming encoder.","commonSituations":"Hand-rolled propagation serializers that emit only the trace ID, a truncated header due to size-limited metadata carriers, or version skew between writer and reader implementations of the binary format.","solutions":["Ensure the writer serializes both trace ID (16 bytes) and span ID (8 bytes) fields with their field IDs in the OpenTelemetry binary format order.","Check carrier/header size limits are not truncating the serialized span context before it reaches parseBytes.","Validate byte-array length (at least 29 bytes for version + trace + span fields) before parsing.","Catch IllegalArgumentException and fall back to an invalid/absent span context for graceful degradation."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"boolean hasSpanIdField(byte[] b) {\n  // version(1) + fieldId(1) + traceId(16) + fieldId(1) = offset 19 must hold SPAN_ID_FIELD_ID (0x01)\n  return b != null && b.length >= 20 && b[19] == 0x01;\n}","typeGuard":null,"tryCatchPattern":"try {\n  ctx = BinaryFormat.parseBytes(bytes);\n} catch (IllegalArgumentException e) {\n  log.warn(\"malformed span context\", e);\n  ctx = SpanContext.getInvalid();\n}","preventionTips":["Ensure senders serialize both trace and span ID fields in format order.","Check metadata carrier size limits are not truncating propagation values.","Keep propagator library versions aligned across services.","Wrap parsing with a fallback to an invalid span context."],"tags":["propagation","trace-context","deserialization","grpc"],"backgroundTag":"invalid-argument-format","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}