Skip to content

Commit 5fdcfac

Browse files
adazhcopybara-github
authored andcommitted
Pass the starting buffer pointer for the backalloc to _upb_Encode_Field and _upb_Encode_Extension.
PiperOrigin-RevId: 914346686
1 parent 5f77738 commit 5fdcfac

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

upb/wire/encode.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,7 @@ upb_EncodeStatus UPB_PRIVATE(_upb_Encode_Field)(upb_encstate* e,
855855
int options) {
856856
e->options = options;
857857
e->depth = upb_EncodeOptions_GetEffectiveMaxDepth(options);
858-
859-
char* ptr = e->alloc.limit;
858+
char* ptr = *buf;
860859
if (encode_shouldencode(msg, field)) {
861860
ptr = encode_field(ptr, e, msg, field);
862861
}
@@ -871,8 +870,7 @@ upb_EncodeStatus UPB_PRIVATE(_upb_Encode_Extension)(
871870
int options) {
872871
e->options = options;
873872
e->depth = upb_EncodeOptions_GetEffectiveMaxDepth(options);
874-
875-
char* ptr = e->alloc.limit;
873+
char* ptr = *buf;
876874
ptr = encode_ext(ptr, e, ext, ext_val, is_message_set);
877875
*size = upb_BackAlloc_Finish(&e->alloc, ptr);
878876
*buf = ptr;

upb/wire/encode_test.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ TEST(EncodeTest, EncodeFieldSuccess) {
6060
// Encode field.
6161
const upb_MiniTable* mt = &upb_0wire_0test__TestInt32_msg_init;
6262
const upb_MiniTableField* field = upb_MiniTable_FindFieldByNumber(mt, 1);
63-
char* buf;
63+
char* buf = e.alloc.limit;
6464
size_t size;
6565
upb_EncodeStatus status = UPB_PRIVATE(_upb_Encode_Field)(
6666
&e, (upb_Message*)msg, field, &buf, &size, e.options);
@@ -91,7 +91,7 @@ TEST(EncodeTest, EncodeFieldSuccessEmptyMessage) {
9191
// Encode empty message field.
9292
const upb_MiniTable* mt = &upb_0wire_0test__TestInt32_msg_init;
9393
const upb_MiniTableField* field = upb_MiniTable_FindFieldByNumber(mt, 1);
94-
char* buf;
94+
char* buf = e.alloc.limit;
9595
size_t size;
9696
upb_EncodeStatus status = UPB_PRIVATE(_upb_Encode_Field)(
9797
&e, (upb_Message*)msg, field, &buf, &size, e.options);
@@ -115,7 +115,7 @@ TEST(EncodeTest, EncodeFieldMaxDepthExceeded) {
115115

116116
const upb_MiniTable* mt = &upb_0wire_0test__TestRecursive_msg_init;
117117
const upb_MiniTableField* field = upb_MiniTable_FindFieldByNumber(mt, 1);
118-
char* buf;
118+
char* buf = e.alloc.limit;
119119
size_t size;
120120
e.options = upb_EncodeOptions_MaxDepth(1);
121121
DoEncodeFieldMaxDepthExceeded(err, e, (upb_Message*)msg, field, buf, size);
@@ -136,7 +136,7 @@ TEST(EncodeTest, EncodeExtensionSuccess) {
136136
ext_val.int32_val = 42;
137137

138138
// Encode extension.
139-
char* buf;
139+
char* buf = e.alloc.limit;
140140
size_t size;
141141
upb_EncodeStatus status = UPB_PRIVATE(_upb_Encode_Extension)(
142142
&e, ext, ext_val, false, &buf, &size, e.options);
@@ -172,7 +172,7 @@ TEST(EncodeTest, EncodeExtensionSuccessEmptyMessage) {
172172
ext_val.int32_val = 0;
173173

174174
// Encode empty extension.
175-
char* buf;
175+
char* buf = e.alloc.limit;
176176
size_t size;
177177
upb_EncodeStatus status = UPB_PRIVATE(_upb_Encode_Extension)(
178178
&e, ext, ext_val, false, &buf, &size, e.options);
@@ -194,7 +194,7 @@ TEST(EncodeTest, EncodeExtensionMaxDepthExceeded) {
194194
upb_MessageValue ext_val;
195195
ext_val.msg_val = (upb_Message*)upb_wire_test_TestRecursive_new(arena);
196196

197-
char* buf;
197+
char* buf = e.alloc.limit;
198198
size_t size;
199199
e.options = upb_EncodeOptions_MaxDepth(1);
200200
DoEncodeExtensionMaxDepthExceeded(err, e, ext, ext_val, buf, size);

0 commit comments

Comments
 (0)