Skip to content

Commit 419ff1f

Browse files
xaionaro@dx.centerxaionaro@dx.center
authored andcommitted
fix: use UTF-16 wire format for @utf8InCpp strings in codegen
The @utf8InCpp annotation only affects the C++ in-memory representation (std::string vs android::String16). The wire format is always UTF-16 because the C++ binder backend uses writeUtf8AsUtf16/readUtf8FromUtf16. Remove the special-case in marshalForType that mapped @utf8InCpp String to WriteString/ReadString (UTF-8), and fix AttributionSourceState to use WriteString16/ReadString16 accordingly.
1 parent 81df415 commit 419ff1f

3 files changed

Lines changed: 20 additions & 17 deletions

File tree

android/content/attributionsourcestate.go

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/pkg/codegen/marshal.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,11 @@ func MarshalForTypeWithRegistry(
9999
return MarshalInfo{}
100100
}
101101

102-
// Check for @utf8InCpp annotation on String type.
103-
if ts.Name == "String" && hasAnnotation(ts.Annots, "utf8InCpp") {
104-
return MarshalInfo{
105-
WriteExpr: "_data.WriteString(%s)",
106-
ReadExpr: "_reply.ReadString()",
107-
}
108-
}
102+
// The @utf8InCpp annotation only affects the in-memory representation
103+
// in C++ (std::string vs android::String16). The wire format is always
104+
// UTF-16 because the C++ binder backend uses writeUtf8AsUtf16 /
105+
// readUtf8FromUtf16 for these fields. We therefore do NOT special-case
106+
// @utf8InCpp here -- all Strings use WriteString16/ReadString16.
109107

110108
if info, ok := marshalPrimitiveMap[ts.Name]; ok {
111109
return info

tools/pkg/codegen/marshal_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,16 @@ func TestMarshalForType_Primitives(t *testing.T) {
9090
}
9191

9292
func TestMarshalForType_Utf8String(t *testing.T) {
93+
// @utf8InCpp only affects the C++ in-memory representation; the wire
94+
// format is always UTF-16. Verify that @utf8InCpp String produces the
95+
// same marshal info as a plain String.
9396
ts := &parser.TypeSpecifier{
9497
Name: "String",
9598
Annots: []*parser.Annotation{{Name: "utf8InCpp"}},
9699
}
97100
info := MarshalForType(ts)
98-
assert.Equal(t, "_data.WriteString(%s)", info.WriteExpr)
99-
assert.Equal(t, "_reply.ReadString()", info.ReadExpr)
101+
assert.Equal(t, "_data.WriteString16(%s)", info.WriteExpr)
102+
assert.Equal(t, "_reply.ReadString16()", info.ReadExpr)
100103
assert.False(t, info.NeedsCast)
101104
}
102105

0 commit comments

Comments
 (0)