Skip to content

Commit cfe93d0

Browse files
authored
Small refactoring to make safety clearer (#8083)
1 parent 4c43b37 commit cfe93d0

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

api/all/src/main/java/io/opentelemetry/api/common/JsonEncoding.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void append(StringBuilder sb, Value<?> value) {
3737
appendMap(sb, (List<KeyValue>) value.getValue());
3838
break;
3939
case BYTES:
40-
appendBytes(sb, (ByteBuffer) value.getValue());
40+
appendBytes(sb, (Value<ByteBuffer>) value);
4141
break;
4242
case EMPTY:
4343
sb.append("null");
@@ -97,9 +97,11 @@ private static void appendDouble(StringBuilder sb, double value) {
9797
}
9898
}
9999

100-
private static void appendBytes(StringBuilder sb, ByteBuffer value) {
101-
byte[] bytes = new byte[value.remaining()];
102-
value.get(bytes);
100+
private static void appendBytes(StringBuilder sb, Value<ByteBuffer> value) {
101+
ByteBuffer buf = value.getValue();
102+
byte[] bytes = new byte[buf.remaining()];
103+
// getValue() above returns a new ByteBuffer, so mutating its position here is safe
104+
buf.get(bytes);
103105
sb.append('"').append(Base64.getEncoder().encodeToString(bytes)).append('"');
104106
}
105107

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AnyValueMarshaler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static MarshalerWithSize create(Value<?> value) {
3737
case KEY_VALUE_LIST:
3838
return KeyValueListAnyValueMarshaler.create((List<KeyValue>) value.getValue());
3939
case BYTES:
40-
return BytesAnyValueMarshaler.create((ByteBuffer) value.getValue());
40+
return BytesAnyValueMarshaler.create((Value<ByteBuffer>) value);
4141
case EMPTY:
4242
return EmptyAnyValueMarshaler.INSTANCE;
4343
}

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AnyValueStatelessMarshaler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void writeTo(Serializer output, Value<?> value, MarshalerContext context)
6464
return;
6565
case BYTES:
6666
BytesAnyValueStatelessMarshaler.INSTANCE.writeTo(
67-
output, (ByteBuffer) value.getValue(), context);
67+
output, (Value<ByteBuffer>) value, context);
6868
return;
6969
case EMPTY:
7070
// no field to write
@@ -105,7 +105,7 @@ public int getBinarySerializedSize(Value<?> value, MarshalerContext context) {
105105
context);
106106
case BYTES:
107107
return BytesAnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize(
108-
(ByteBuffer) value.getValue(), context);
108+
(Value<ByteBuffer>) value, context);
109109
case EMPTY:
110110
return 0;
111111
}

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/BytesAnyValueMarshaler.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.exporter.internal.otlp;
77

8+
import io.opentelemetry.api.common.Value;
89
import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
910
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
1011
import io.opentelemetry.exporter.internal.marshal.Serializer;
@@ -21,9 +22,11 @@ private BytesAnyValueMarshaler(byte[] value) {
2122
this.value = value;
2223
}
2324

24-
static MarshalerWithSize create(ByteBuffer value) {
25-
byte[] bytes = new byte[value.remaining()];
26-
value.get(bytes);
25+
static MarshalerWithSize create(Value<ByteBuffer> value) {
26+
ByteBuffer buf = value.getValue();
27+
byte[] bytes = new byte[buf.remaining()];
28+
// getValue() above returns a new ByteBuffer, so mutating its position here is safe
29+
buf.get(bytes);
2730
return new BytesAnyValueMarshaler(bytes);
2831
}
2932

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/BytesAnyValueStatelessMarshaler.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.exporter.internal.otlp;
77

8+
import io.opentelemetry.api.common.Value;
89
import io.opentelemetry.exporter.internal.marshal.CodedOutputStream;
910
import io.opentelemetry.exporter.internal.marshal.MarshalerContext;
1011
import io.opentelemetry.exporter.internal.marshal.Serializer;
@@ -14,22 +15,24 @@
1415
import java.nio.ByteBuffer;
1516

1617
/** See {@link BytesAnyValueMarshaler}. */
17-
final class BytesAnyValueStatelessMarshaler implements StatelessMarshaler<ByteBuffer> {
18+
final class BytesAnyValueStatelessMarshaler implements StatelessMarshaler<Value<ByteBuffer>> {
1819
static final BytesAnyValueStatelessMarshaler INSTANCE = new BytesAnyValueStatelessMarshaler();
1920

2021
private BytesAnyValueStatelessMarshaler() {}
2122

2223
@Override
23-
public void writeTo(Serializer output, ByteBuffer value, MarshalerContext context)
24+
public void writeTo(Serializer output, Value<ByteBuffer> value, MarshalerContext context)
2425
throws IOException {
2526
byte[] bytes = context.getData(byte[].class);
2627
output.writeBytes(AnyValue.BYTES_VALUE, bytes);
2728
}
2829

2930
@Override
30-
public int getBinarySerializedSize(ByteBuffer value, MarshalerContext context) {
31-
byte[] bytes = new byte[value.remaining()];
32-
value.get(bytes);
31+
public int getBinarySerializedSize(Value<ByteBuffer> value, MarshalerContext context) {
32+
ByteBuffer buf = value.getValue();
33+
byte[] bytes = new byte[buf.remaining()];
34+
// getValue() above returns a new ByteBuffer, so mutating its position here is safe
35+
buf.get(bytes);
3336
context.addData(bytes);
3437
return AnyValue.BYTES_VALUE.getTagSize() + CodedOutputStream.computeByteArraySizeNoTag(bytes);
3538
}

0 commit comments

Comments
 (0)