{"record":{"id":"ac59ec5bd8f84005","repo":"grpc/grpc-java","slug":"invalid-input-truncated","errorCode":null,"errorMessage":"Invalid input: truncated","messagePattern":"Invalid input: truncated","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"warning","filePath":"opentelemetry/src/main/java/io/grpc/opentelemetry/BinaryFormat.java","lineNumber":115,"sourceCode":"    bytes[VERSION_ID_OFFSET] = VERSION_ID;\n    bytes[TRACE_ID_FIELD_ID_OFFSET] = TRACE_ID_FIELD_ID;\n    System.arraycopy(spanContext.getTraceIdBytes(), 0, bytes, TRACE_ID_OFFSET, TRACE_ID_SIZE);\n    bytes[SPAN_ID_FIELD_ID_OFFSET] = SPAN_ID_FIELD_ID;\n    System.arraycopy(spanContext.getSpanIdBytes(), 0, bytes, SPAN_ID_OFFSET, SPAN_ID_SIZE);\n    bytes[TRACE_FLAG_FIELD_ID_OFFSET] = TRACE_FLAG_FIELD_ID;\n    bytes[TRACE_FLAG_OFFSET] = spanContext.getTraceFlags().asByte();\n    return bytes;\n  }\n\n\n  @Override\n  public SpanContext parseBytes(byte[] serialized) {\n    checkNotNull(serialized, \"bytes\");\n    if (serialized.length == 0 || serialized[0] != VERSION_ID) {\n      throw new IllegalArgumentException(\"Unsupported version.\");\n    }\n    if (serialized.length < REQUIRED_FORMAT_LENGTH) {\n      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);","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/opentelemetry/src/main/java/io/grpc/opentelemetry/BinaryFormat.java#L97-L133","documentation":"After checking the version byte, BinaryFormat.parseBytes requires the serialized SpanContext blob to be at least REQUIRED_FORMAT_LENGTH bytes long (version + trace-id + span-id + flags fields). Shorter input is rejected with IllegalArgumentException 'Invalid input: truncated'.","triggerScenarios":"Calling parseBytes with a byte array shorter than REQUIRED_FORMAT_LENGTH (but non-empty and version-correct) — e.g. a carrier written partially, truncated network payload, or wrong field bytes.","commonSituations":"Carrier maps truncated by intermediaries, hand-rolled serialization that omits trailing fields, binary/HTTP header propagation mix-ups dropping bytes.","solutions":["Serialize the full SpanContext with the library's toBytes() and pass the resulting array untouched","Check the byte array length before calling parseBytes","Verify no intermediary (proxy, header limit, custom carrier) is truncating the propagation payload"],"exampleFix":"// before\nSpanContext ctx = propagator.parseBytes(shortBytes);\n// after\nif (shortBytes != null && shortBytes.length >= 29) { SpanContext ctx = propagator.parseBytes(shortBytes); }","handlingStrategy":"validation","validationCode":"boolean isCompleteSpanContext(byte[] b) { return b != null && b.length >= 29 && b[0] == 0; }","typeGuard":null,"tryCatchPattern":"try { ctx = propagator.parseBytes(bytes); }\ncatch (IllegalArgumentException e) {\n  if (\"Invalid input: truncated\".equals(e.getMessage())) { /* treat as missing trace context */ ctx = SpanContext.INVALID; }\n}","preventionTips":["Round-trip contexts only via toBytes() output","Check header/carrier size limits that may drop propagation bytes","Log byte array length when propagation fails to spot truncation sources"],"tags":["grpc","opentelemetry","tracing","propagation","truncated-input"],"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"}