google / flatbuffers

FlatBuffers: Memory Efficient Serialization Library

Home Page:https://flatbuffers.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception of Invalid UTF-8

0pow0 opened this issue · comments

commented

Discussed in #7117

Originally posted by tzyrq February 22, 2022
I'm using flatbuffers to serialize data through TCP socket from a cpp program to a java program.
What I'm doing on cpp side:

...
std::string str;
auto str_ = builder.CreateString (str);
auto foo = CreateFooTable(builder, str_, ...);
builder.Finish(foo);
uint8_t* buf = builder.GetBufferPointer();
SocketSend(buf);

What I'm doing on Java side:

byte[] bytes = ....;
SocketRead(bytes);
java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(bytes);
FooTable foo = FooTable.getRootAsFooTable(buf);
int type = foo.infoType();
if (type == FooType.Type1) {
	UnionElement1 ele = (UnionElement1) foo.info(new UnionElement1());
	System.out.println(ele.Id()); /*<-- java.lang.IllegalArgumentException: Invalid UTF-8*/
}
Caused by: java.lang.IllegalArgumentException: Invalid UTF-8
	at com.google.flatbuffers.Utf8$DecodeUtil.handleThreeBytes(Utf8.java:185)
	at com.google.flatbuffers.Utf8Safe.decodeUtf8Array(Utf8Safe.java:175)
	at com.google.flatbuffers.Utf8Safe.decodeUtf8(Utf8Safe.java:290)
	at com.google.flatbuffers.Table.__string(Table.java:113)
	at com.google.flatbuffers.Table.__string(Table.java:94)

My .fbs file:

table UnionElement1 {
  filed1: string;
  filed2: string;
}

table UnionElement2 {
  filed1: string;
  filed2: string;
  filed3: string;
}

union FooType {
  Type1: UnionElement1,
  Type2: UnionElement2
}

table FooTable {
  delay: string;
  info: FooType;
}

root_type FooTable;

This whole procedure will be repeated by thousands of times, it works correctly at the begin but after that raise this exception.

flatbuffers version: 2.0.0

Any idea? 🥲 Thanks a lot!

I posted some thoughts in the Discussion if you haven't seen it.

@tzyrq Any update to this? Otherwise I will close it.