Added missing append fallback.

pull/13171/head
Joshua Haberman 5 years ago
parent c4b64e6a20
commit f9efbcd5d6
  1. 20
      tests/test_generated_code.c
  2. 31
      upb/msg.c

@ -301,9 +301,29 @@ static void test_int32_map() {
upb_arena_free(arena);
}
void test_repeated() {
upb_arena *arena = upb_arena_new();
protobuf_test_messages_proto3_TestAllTypesProto3 *msg =
protobuf_test_messages_proto3_TestAllTypesProto3_new(arena);
size_t size;
const int *elems;
protobuf_test_messages_proto3_TestAllTypesProto3_add_repeated_int32(
msg, 5, arena);
elems = protobuf_test_messages_proto3_TestAllTypesProto3_repeated_int32(
msg, &size);
ASSERT(size == 1);
ASSERT(elems[0] == 5);
upb_arena_free(arena);
}
int run_tests(int argc, char *argv[]) {
test_scalars();
test_string_map();
test_int32_map();
test_repeated();
return 0;
}

@ -137,21 +137,44 @@ bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
return true;
}
void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
upb_fieldtype_t type, upb_arena *arena) {
static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
upb_arena *arena) {
upb_array *arr = *arr_ptr;
if (!arr) {
arr = _upb_array_new(arena, type);
if (!arr) return NULL;
*arr_ptr = arr;
}
return arr;
}
static bool resize_array(upb_array *arr, size_t size, upb_arena *arena) {
if (size > arr->size && !_upb_array_realloc(arr, size, arena)) {
return NULL;
return false;
}
arr->len = size;
return _upb_array_ptr(arr);
return true;
}
void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
upb_fieldtype_t type, upb_arena *arena) {
upb_array *arr = getorcreate_array(arr_ptr, type, arena);
return arr && resize_array(arr, size, arena) ? _upb_array_ptr(arr) : NULL;
}
bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
upb_fieldtype_t type, upb_arena *arena) {
upb_array *arr = getorcreate_array(arr_ptr, type, arena);
size_t elem = arr->len;
int lg2 = _upb_fieldtype_to_sizelg2[type];
char *data;
if (!arr || !resize_array(arr, elem + 1, arena)) return false;
data = _upb_array_ptr(arr);
memcpy(data + (elem << lg2), value, 1 << lg2);
return true;
}
/** upb_map *******************************************************************/

Loading…
Cancel
Save