From a7aa78069174194f6a0645ad3a3043b3873bf956 Mon Sep 17 00:00:00 2001 From: "Mark D. Roth" Date: Thu, 4 Jun 2020 13:32:27 -0700 Subject: [PATCH] Squashed 'third_party/upb/' changes from 7f5b8d73b0..b717575cef b717575cef Added -Wextra and -Wshorten-64-to-32 and fixed resulting errors. (#289) 2e335ab9d2 Merge pull request #288 from haberman/ubsan 408d34797d Disable UBSan, Clang is not available in upb's Kokoro image atm. 6b808a4072 Fixed all UBSan issues and added UBSan CI checks. git-subtree-dir: third_party/upb git-subtree-split: b717575ceff5e759a28f3ed3d60a65eee041a30c --- BUILD | 10 ++ generated_for_cmake/upb/json/parser.c | 146 ++++++++++++++------------ kokoro/ubuntu/build.sh | 14 ++- tests/conformance_upb.c | 2 +- tests/json/test_json.cc | 9 +- tests/pb/test_decoder.cc | 8 +- tests/test_cpp.cc | 4 +- tests/test_util.h | 6 +- upb/bindings/lua/upbc.cc | 4 +- upb/decode.c | 6 +- upb/def.c | 17 ++- upb/encode.c | 12 +-- upb/handlers.c | 22 ---- upb/json/parser.rl | 8 +- upb/json_decode.c | 26 +++-- upb/json_encode.c | 4 +- upb/msg.c | 11 +- upb/msg.h | 24 +++-- upb/pb/decoder.h | 4 +- upb/pb/varint.int.h | 3 +- upb/reflection.c | 61 ++++++----- upb/table.c | 32 +++--- upb/text_encode.c | 11 +- upb/upb.c | 4 +- upb/upb.h | 2 +- upbc/generator.cc | 6 +- 26 files changed, 242 insertions(+), 214 deletions(-) diff --git a/BUILD b/BUILD index 202154c9d05..bcecd387af4 100644 --- a/BUILD +++ b/BUILD @@ -28,6 +28,8 @@ exports_files([ CPPOPTS = [ # copybara:strip_for_google3_begin + "-Wextra", + # "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang) "-Werror", "-Wno-long-long", # copybara:strip_end @@ -164,6 +166,10 @@ cc_library( hdrs = [ "upb/text_encode.h", ], + copts = select({ + ":windows": [], + "//conditions:default": COPTS, + }), visibility = ["//visibility:public"], deps = [ ":port", @@ -181,6 +187,10 @@ cc_library( "upb/json_decode.h", "upb/json_encode.h", ], + copts = select({ + ":windows": [], + "//conditions:default": COPTS, + }), deps = [ ":port", ":reflection", diff --git a/generated_for_cmake/upb/json/parser.c b/generated_for_cmake/upb/json/parser.c index 1423add9c53..dd4fd82f2f3 100644 --- a/generated_for_cmake/upb/json/parser.c +++ b/generated_for_cmake/upb/json/parser.c @@ -644,7 +644,9 @@ static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, } if (p->accumulated != p->accumulate_buf) { - memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len); + if (p->accumulated_len) { + memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len); + } p->accumulated = p->accumulate_buf; } @@ -975,6 +977,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint32(p->top->sink, parser_getsel(p), (int32_t)val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT32: { unsigned long val = strtoul(buf, &end, 0); @@ -986,6 +989,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint32(p->top->sink, parser_getsel(p), (uint32_t)val); return true; } + UPB_UNREACHABLE(); } /* XXX: We can't handle [u]int64 properly on 32-bit machines because * strto[u]ll isn't in C89. */ @@ -997,6 +1001,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT64: { unsigned long val = strtoul(p->accumulated, &end, 0); @@ -1008,6 +1013,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } default: break; @@ -2573,11 +2579,11 @@ static bool does_fieldmask_end(upb_json_parser *p) { * final state once, when the closing '"' is seen. */ -#line 2778 "upb/json/parser.rl" +#line 2784 "upb/json/parser.rl" -#line 2581 "upb/json/parser.c" +#line 2587 "upb/json/parser.c" static const char _json_actions[] = { 0, 1, 0, 1, 1, 1, 3, 1, 4, 1, 6, 1, 7, 1, 8, 1, @@ -2832,7 +2838,7 @@ static const int json_en_value_machine = 78; static const int json_en_main = 1; -#line 2781 "upb/json/parser.rl" +#line 2787 "upb/json/parser.rl" size_t parse(void *closure, const void *hd, const char *buf, size_t size, const upb_bufhandle *handle) { @@ -2855,7 +2861,7 @@ size_t parse(void *closure, const void *hd, const char *buf, size_t size, capture_resume(parser, buf); -#line 2859 "upb/json/parser.c" +#line 2865 "upb/json/parser.c" { int _klen; unsigned int _trans; @@ -2930,147 +2936,147 @@ _match: switch ( *_acts++ ) { case 1: -#line 2586 "upb/json/parser.rl" +#line 2592 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 2: -#line 2588 "upb/json/parser.rl" +#line 2594 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 23;goto _again;} } break; case 3: -#line 2592 "upb/json/parser.rl" +#line 2598 "upb/json/parser.rl" { start_text(parser, p); } break; case 4: -#line 2593 "upb/json/parser.rl" +#line 2599 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_text(parser, p)); } break; case 5: -#line 2599 "upb/json/parser.rl" +#line 2605 "upb/json/parser.rl" { start_hex(parser); } break; case 6: -#line 2600 "upb/json/parser.rl" +#line 2606 "upb/json/parser.rl" { hexdigit(parser, p); } break; case 7: -#line 2601 "upb/json/parser.rl" +#line 2607 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hex(parser)); } break; case 8: -#line 2607 "upb/json/parser.rl" +#line 2613 "upb/json/parser.rl" { CHECK_RETURN_TOP(escape(parser, p)); } break; case 9: -#line 2613 "upb/json/parser.rl" +#line 2619 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 10: -#line 2618 "upb/json/parser.rl" +#line 2624 "upb/json/parser.rl" { start_year(parser, p); } break; case 11: -#line 2619 "upb/json/parser.rl" +#line 2625 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_year(parser, p)); } break; case 12: -#line 2623 "upb/json/parser.rl" +#line 2629 "upb/json/parser.rl" { start_month(parser, p); } break; case 13: -#line 2624 "upb/json/parser.rl" +#line 2630 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_month(parser, p)); } break; case 14: -#line 2628 "upb/json/parser.rl" +#line 2634 "upb/json/parser.rl" { start_day(parser, p); } break; case 15: -#line 2629 "upb/json/parser.rl" +#line 2635 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_day(parser, p)); } break; case 16: -#line 2633 "upb/json/parser.rl" +#line 2639 "upb/json/parser.rl" { start_hour(parser, p); } break; case 17: -#line 2634 "upb/json/parser.rl" +#line 2640 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_hour(parser, p)); } break; case 18: -#line 2638 "upb/json/parser.rl" +#line 2644 "upb/json/parser.rl" { start_minute(parser, p); } break; case 19: -#line 2639 "upb/json/parser.rl" +#line 2645 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_minute(parser, p)); } break; case 20: -#line 2643 "upb/json/parser.rl" +#line 2649 "upb/json/parser.rl" { start_second(parser, p); } break; case 21: -#line 2644 "upb/json/parser.rl" +#line 2650 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_second(parser, p)); } break; case 22: -#line 2649 "upb/json/parser.rl" +#line 2655 "upb/json/parser.rl" { start_duration_base(parser, p); } break; case 23: -#line 2650 "upb/json/parser.rl" +#line 2656 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_duration_base(parser, p)); } break; case 24: -#line 2652 "upb/json/parser.rl" +#line 2658 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 25: -#line 2657 "upb/json/parser.rl" +#line 2663 "upb/json/parser.rl" { start_timestamp_base(parser); } break; case 26: -#line 2659 "upb/json/parser.rl" +#line 2665 "upb/json/parser.rl" { start_timestamp_fraction(parser, p); } break; case 27: -#line 2660 "upb/json/parser.rl" +#line 2666 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_fraction(parser, p)); } break; case 28: -#line 2662 "upb/json/parser.rl" +#line 2668 "upb/json/parser.rl" { start_timestamp_zone(parser, p); } break; case 29: -#line 2663 "upb/json/parser.rl" +#line 2669 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_timestamp_zone(parser, p)); } break; case 30: -#line 2665 "upb/json/parser.rl" +#line 2671 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 31: -#line 2670 "upb/json/parser.rl" +#line 2676 "upb/json/parser.rl" { start_fieldmask_path_text(parser, p); } break; case 32: -#line 2671 "upb/json/parser.rl" +#line 2677 "upb/json/parser.rl" { end_fieldmask_path_text(parser, p); } break; case 33: -#line 2676 "upb/json/parser.rl" +#line 2682 "upb/json/parser.rl" { start_fieldmask_path(parser); } break; case 34: -#line 2677 "upb/json/parser.rl" +#line 2683 "upb/json/parser.rl" { end_fieldmask_path(parser); } break; case 35: -#line 2683 "upb/json/parser.rl" +#line 2689 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; case 36: -#line 2688 "upb/json/parser.rl" +#line 2694 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_TIMESTAMP)) { {stack[top++] = cs; cs = 47;goto _again;} @@ -3084,11 +3090,11 @@ _match: } break; case 37: -#line 2701 "upb/json/parser.rl" +#line 2707 "upb/json/parser.rl" { p--; {stack[top++] = cs; cs = 78;goto _again;} } break; case 38: -#line 2706 "upb/json/parser.rl" +#line 2712 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_member(parser, p); @@ -3098,11 +3104,11 @@ _match: } break; case 39: -#line 2713 "upb/json/parser.rl" +#line 2719 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_membername(parser)); } break; case 40: -#line 2716 "upb/json/parser.rl" +#line 2722 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { end_any_member(parser, p); @@ -3112,7 +3118,7 @@ _match: } break; case 41: -#line 2727 "upb/json/parser.rl" +#line 2733 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { start_any_object(parser, p); @@ -3122,7 +3128,7 @@ _match: } break; case 42: -#line 2736 "upb/json/parser.rl" +#line 2742 "upb/json/parser.rl" { if (is_wellknown_msg(parser, UPB_WELLKNOWN_ANY)) { CHECK_RETURN_TOP(end_any_object(parser, p)); @@ -3132,54 +3138,54 @@ _match: } break; case 43: -#line 2748 "upb/json/parser.rl" +#line 2754 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_array(parser)); } break; case 44: -#line 2752 "upb/json/parser.rl" +#line 2758 "upb/json/parser.rl" { end_array(parser); } break; case 45: -#line 2757 "upb/json/parser.rl" +#line 2763 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_number(parser, p)); } break; case 46: -#line 2758 "upb/json/parser.rl" +#line 2764 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 47: -#line 2760 "upb/json/parser.rl" +#line 2766 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_stringval(parser)); } break; case 48: -#line 2761 "upb/json/parser.rl" +#line 2767 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_stringval(parser)); } break; case 49: -#line 2763 "upb/json/parser.rl" +#line 2769 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2765 "upb/json/parser.rl" +#line 2771 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2767 "upb/json/parser.rl" +#line 2773 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 52: -#line 2769 "upb/json/parser.rl" +#line 2775 "upb/json/parser.rl" { CHECK_RETURN_TOP(start_subobject_full(parser)); } break; case 53: -#line 2770 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { end_subobject_full(parser); } break; case 54: -#line 2775 "upb/json/parser.rl" +#line 2781 "upb/json/parser.rl" { p--; {cs = stack[--top]; goto _again;} } break; -#line 3183 "upb/json/parser.c" +#line 3189 "upb/json/parser.c" } } @@ -3196,32 +3202,32 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 0: -#line 2584 "upb/json/parser.rl" +#line 2590 "upb/json/parser.rl" { p--; {cs = stack[--top]; if ( p == pe ) goto _test_eof; goto _again;} } break; case 46: -#line 2758 "upb/json/parser.rl" +#line 2764 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_number(parser, p)); } break; case 49: -#line 2763 "upb/json/parser.rl" +#line 2769 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, true)); } break; case 50: -#line 2765 "upb/json/parser.rl" +#line 2771 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_bool(parser, false)); } break; case 51: -#line 2767 "upb/json/parser.rl" +#line 2773 "upb/json/parser.rl" { CHECK_RETURN_TOP(end_null(parser)); } break; case 53: -#line 2770 "upb/json/parser.rl" +#line 2776 "upb/json/parser.rl" { end_subobject_full(parser); } break; -#line 3225 "upb/json/parser.c" +#line 3231 "upb/json/parser.c" } } } @@ -3229,7 +3235,7 @@ goto _again;} } _out: {} } -#line 2803 "upb/json/parser.rl" +#line 2809 "upb/json/parser.rl" if (p != pe) { upb_status_seterrf(parser->status, "Parse error at '%.*s'\n", pe - p, p); @@ -3272,13 +3278,13 @@ static void json_parser_reset(upb_json_parser *p) { /* Emit Ragel initialization of the parser. */ -#line 3276 "upb/json/parser.c" +#line 3282 "upb/json/parser.c" { cs = json_start; top = 0; } -#line 2845 "upb/json/parser.rl" +#line 2851 "upb/json/parser.rl" p->current_state = cs; p->parser_top = top; accumulate_clear(p); diff --git a/kokoro/ubuntu/build.sh b/kokoro/ubuntu/build.sh index e73deef0ab8..3888d15f6ae 100644 --- a/kokoro/ubuntu/build.sh +++ b/kokoro/ubuntu/build.sh @@ -1,12 +1,18 @@ #!/bin/bash +set -ex + # Install the latest version of Bazel. -use_bazel.sh latest +if [ -x "$(command -v use_bazel.sh)" ]; then + use_bazel.sh latest +fi # Verify/query CMake echo PATH=$PATH ls -l `which cmake` cmake --version +echo CC=${CC:-cc} +${CC:-cc} --version # Log the bazel path and version. which bazel @@ -19,4 +25,10 @@ if [[ $(uname) = "Linux" ]]; then # Verify the ASAN build. Have to exclude test_conformance_upb as protobuf # currently leaks memory in the conformance test runner. bazel test --copt=-fsanitize=address --linkopt=-fsanitize=address --test_output=errors -- :all -:test_conformance_upb + + # Verify the UBSan build. Have to exclude Lua as the version we are using + # fails some UBSan tests. + + # For some reason kokoro doesn't have Clang available right now. + #CC=clang CXX=clang++ bazel test -c dbg --copt=-fsanitize=undefined --copt=-fno-sanitize=function,vptr --linkopt=-fsanitize=undefined --action_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1 -- :all -:test_lua fi diff --git a/tests/conformance_upb.c b/tests/conformance_upb.c index 4a04adf8e49..fe9f5224e52 100644 --- a/tests/conformance_upb.c +++ b/tests/conformance_upb.c @@ -138,7 +138,7 @@ void serialize_json(const upb_msg *msg, const upb_msgdef *m, const ctx *c) { upb_status_clear(&status); len = upb_json_encode(msg, m, c->symtab, opts, NULL, 0, &status); - if (len == -1) { + if (len == (size_t)-1) { const char *inerr = upb_status_errmsg(&status); size_t len = strlen(inerr); char *err = upb_arena_malloc(c->arena, len + 1); diff --git a/tests/json/test_json.cc b/tests/json/test_json.cc index e5f01035842..18ab07fe50a 100644 --- a/tests/json/test_json.cc +++ b/tests/json/test_json.cc @@ -149,6 +149,7 @@ static TestCase kTestSkipUnknown[] = { static TestCase kTestFailure[] = { { TEST("{\"optionalEnum\":\"UNKNOWN_ENUM_VALUE\"}"), + EXPECT("{}"), /* Actually we expect error, this is checked later. */ }, TEST_SENTINEL }; @@ -242,7 +243,7 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers, parser_method, i, + serialize_handlers, parser_method, (int)i, false); } } @@ -257,7 +258,7 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers, parser_method, i, + serialize_handlers, parser_method, (int)i, true); } } @@ -274,7 +275,7 @@ void test_json_roundtrip() { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_roundtrip_message(test_case->input, expected, - serialize_handlers, parser_method, i, + serialize_handlers, parser_method, (int)i, false); } } @@ -319,7 +320,7 @@ void test_json_failure() { test_case->input != NULL; test_case++) { for (size_t i = 0; i < strlen(test_case->input); i++) { test_json_parse_failure(test_case->input, serialize_handlers, - parser_method, i); + parser_method, (int)i); } } } diff --git a/tests/pb/test_decoder.cc b/tests/pb/test_decoder.cc index ef40f8f3e26..14fd72abbcc 100644 --- a/tests/pb/test_decoder.cc +++ b/tests/pb/test_decoder.cc @@ -509,8 +509,8 @@ void do_run_decoder(VerboseParserEnvironment* env, upb::pb::DecoderPtr decoder, } bool ok = env->Start() && - parse(env, decoder, i) && - parse(env, decoder, j - i) && + parse(env, decoder, (int)i) && + parse(env, decoder, (int)(j - i)) && parse(env, decoder, -1) && env->End(); @@ -1108,7 +1108,7 @@ void test_valid() { run_decoder(buf, &textbuf); } -void empty_callback(const void *closure, upb::Handlers* h_ptr) {} +void empty_callback(const void* /* closure */, upb::Handlers* /* h_ptr */) {} void test_emptyhandlers(upb::SymbolTable* symtab) { // Create an empty handlers to make sure that the decoder can handle empty @@ -1176,7 +1176,7 @@ extern "C" { int run_tests(int argc, char *argv[]) { if (argc > 1) - filter_hash = strtol(argv[1], NULL, 16); + filter_hash = (uint32_t)strtol(argv[1], NULL, 16); for (int i = 0; i < MAX_NESTING; i++) { closures[i] = i; } diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 55cc077c880..4ca81e1c353 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -681,6 +681,7 @@ void DoNothingEndMessageHandler(C* closure, upb_status *status) { void RegisterMismatchedTypes(const void* closure, upb::Handlers* h_ptr) { upb::HandlersPtr h(h_ptr); + UPB_UNUSED(closure); upb::MessageDefPtr md(h.message_def()); ASSERT(md); @@ -797,6 +798,7 @@ void RegisterMismatchedTypes(const void* closure, upb::Handlers* h_ptr) { void RegisterMismatchedTypes2(const void* closure, upb::Handlers* h_ptr) { upb::HandlersPtr h(h_ptr); + UPB_UNUSED(closure); upb::MessageDefPtr md(h.message_def()); ASSERT(md); @@ -941,7 +943,7 @@ void TestArena() { extern "C" { -int run_tests(int argc, char *argv[]) { +int run_tests() { TestHandler(); TestHandler(); TestHandler(); diff --git a/tests/test_util.h b/tests/test_util.h index 485410082df..418a9d8899e 100644 --- a/tests/test_util.h +++ b/tests/test_util.h @@ -102,7 +102,7 @@ class VerboseParserEnvironment { bool ParseBuffer(int bytes) { if (bytes < 0) { - bytes = len_ - ofs_; + bytes = (int)(len_ - ofs_); } ASSERT((size_t)bytes <= (len_ - ofs_)); @@ -130,7 +130,7 @@ class VerboseParserEnvironment { (unsigned)bytes, (unsigned)ofs_, (unsigned)(ofs_ + bytes)); } - int parsed = sink_.PutBuffer(subc_, buf2, bytes, &global_handle); + int parsed = (int)sink_.PutBuffer(subc_, buf2, bytes, &global_handle); free(buf2); if (verbose_) { @@ -156,7 +156,7 @@ class VerboseParserEnvironment { } if (parsed > bytes && skip_until_ >= 0) { - skip_until_ = ofs_ + parsed; + skip_until_ = (int)(ofs_ + parsed); } ofs_ += UPB_MIN(parsed, bytes); diff --git a/upb/bindings/lua/upbc.cc b/upb/bindings/lua/upbc.cc index e7563bc0bab..c3988f6519b 100644 --- a/upb/bindings/lua/upbc.cc +++ b/upb/bindings/lua/upbc.cc @@ -73,9 +73,9 @@ static void PrintString(int max_cols, absl::string_view* str, bool LuaGenerator::Generate( const protobuf::FileDescriptor* file, - const std::string& parameter, + const std::string& /* parameter */, protoc::GeneratorContext* context, - std::string* error) const { + std::string* /* error */) const { std::string filename = Filename(file); protobuf::io::ZeroCopyOutputStream* out = context->Open(filename); protobuf::io::Printer printer(out, '$'); diff --git a/upb/decode.c b/upb/decode.c index 7ee199f51af..cecd68a8e74 100644 --- a/upb/decode.c +++ b/upb/decode.c @@ -155,7 +155,7 @@ static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg, UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); } -static bool decode_reserve(upb_decstate *d, upb_array *arr, int elem) { +static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) { bool need_realloc = arr->size - arr->len < elem; if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) { decode_err(d); @@ -324,7 +324,7 @@ static const char *decode_toarray(upb_decstate *d, const char *ptr, /* Fixed packed. */ int lg2 = op - OP_FIXPCK_LG2(0); int mask = (1 << lg2) - 1; - int count = val.str_val.size >> lg2; + size_t count = val.str_val.size >> lg2; if ((val.str_val.size & mask) != 0) { decode_err(d); /* Length isn't a round multiple of elem size. */ } @@ -408,7 +408,7 @@ static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg, /* Set presence if necessary. */ if (field->presence < 0) { /* Oneof case */ - int32_t *oneof_case = _upb_oneofcase_field(msg, field); + uint32_t *oneof_case = _upb_oneofcase_field(msg, field); if (op == OP_SUBMSG && *oneof_case != field->number) { memset(mem, 0, sizeof(void*)); } diff --git a/upb/def.c b/upb/def.c index 50dccd8ff1e..10f6b94bf1e 100644 --- a/upb/def.c +++ b/upb/def.c @@ -18,7 +18,7 @@ static str_t *newstr(upb_alloc *alloc, const char *data, size_t len) { str_t *ret = upb_malloc(alloc, sizeof(*ret) + len); if (!ret) return NULL; ret->len = len; - memcpy(ret->str, data, len); + if (len) memcpy(ret->str, data, len); ret->str[len] = '\0'; return ret; } @@ -1909,11 +1909,11 @@ static bool build_filedef( } /* Now that all names are in the table, build layouts and resolve refs. */ - for (i = 0; i < file->ext_count; i++) { + for (i = 0; i < (size_t)file->ext_count; i++) { CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i])); } - for (i = 0; i < file->msg_count; i++) { + for (i = 0; i < (size_t)file->msg_count; i++) { const upb_msgdef *m = &file->msgs[i]; int j; for (j = 0; j < m->field_count; j++) { @@ -1922,7 +1922,7 @@ static bool build_filedef( } if (!ctx->layouts) { - for (i = 0; i < file->msg_count; i++) { + for (i = 0; i < (size_t)file->msg_count; i++) { const upb_msgdef *m = &file->msgs[i]; make_layout(ctx->symtab, m); } @@ -1931,8 +1931,7 @@ static bool build_filedef( return true; } -static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx, - upb_status *status) { +static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx) { const upb_filedef *file = ctx->file; upb_alloc *alloc = upb_arena_alloc(s->arena); upb_strtable_iter iter; @@ -2075,10 +2074,8 @@ static const upb_filedef *_upb_symtab_addfile( ctx.layouts = layouts; ctx.status = status; - ok = file && - upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && - build_filedef(&ctx, file, file_proto) && - upb_symtab_addtotabs(s, &ctx, status); + ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) && + build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx); upb_arena_free(tmparena); return ok ? file : NULL; diff --git a/upb/encode.c b/upb/encode.c index 010f5c5c394..87162325ad9 100644 --- a/upb/encode.c +++ b/upb/encode.c @@ -335,13 +335,11 @@ static bool upb_encode_map(upb_encstate *e, const char *field_mem, size_t size; upb_strview key = upb_strtable_iter_key(&i); const upb_value val = upb_strtable_iter_value(&i); - const void *keyp = - map->key_size == UPB_MAPTYPE_STRING ? (void *)&key : key.data; - const void *valp = - map->val_size == UPB_MAPTYPE_STRING ? upb_value_getptr(val) : &val; - - CHK(upb_encode_scalarfield(e, valp, entry, val_field, false)); - CHK(upb_encode_scalarfield(e, keyp, entry, key_field, false)); + upb_map_entry ent; + _upb_map_fromkey(key, &ent.k, map->key_size); + _upb_map_fromvalue(val, &ent.v, map->val_size); + CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false)); + CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false)); size = (e->limit - e->ptr) - pre_len; CHK(upb_put_varint(e, size)); CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED)); diff --git a/upb/handlers.c b/upb/handlers.c index 5cf7ccc0746..b90b32c71f0 100644 --- a/upb/handlers.c +++ b/upb/handlers.c @@ -152,28 +152,6 @@ const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f, return ret; } -/* Checks whether the START* handler specified by f & type is missing even - * though it is required to convert the established type of an outer frame - * ("closure_type") into the established type of an inner frame (represented in - * the return closure type of this handler's attr. */ -bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type, - upb_status *status) { - const void *closure_type; - const upb_handlerattr *attr; - const void *return_closure_type; - - upb_selector_t sel = handlers_getsel(h, f, type); - if (h->table[sel].func) return true; - closure_type = effective_closure_type(h, f, type); - attr = &h->table[sel].attr; - return_closure_type = attr->return_closure_type; - if (closure_type && return_closure_type && - closure_type != return_closure_type) { - return false; - } - return true; -} - static upb_handlers *upb_handlers_new(const upb_msgdef *md, upb_handlercache *cache, upb_arena *arena) { diff --git a/upb/json/parser.rl b/upb/json/parser.rl index eccd53039ff..f1ea0762811 100644 --- a/upb/json/parser.rl +++ b/upb/json/parser.rl @@ -642,7 +642,9 @@ static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len, } if (p->accumulated != p->accumulate_buf) { - memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len); + if (p->accumulated_len) { + memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len); + } p->accumulated = p->accumulate_buf; } @@ -973,6 +975,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint32(p->top->sink, parser_getsel(p), (int32_t)val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT32: { unsigned long val = strtoul(buf, &end, 0); @@ -984,6 +987,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint32(p->top->sink, parser_getsel(p), (uint32_t)val); return true; } + UPB_UNREACHABLE(); } /* XXX: We can't handle [u]int64 properly on 32-bit machines because * strto[u]ll isn't in C89. */ @@ -995,6 +999,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } case UPB_TYPE_UINT64: { unsigned long val = strtoul(p->accumulated, &end, 0); @@ -1006,6 +1011,7 @@ static bool parse_number_from_buffer(upb_json_parser *p, const char *buf, upb_sink_putuint64(p->top->sink, parser_getsel(p), val); return true; } + UPB_UNREACHABLE(); } default: break; diff --git a/upb/json_decode.c b/upb/json_decode.c index e49ee854c89..301f16a1453 100644 --- a/upb/json_decode.c +++ b/upb/json_decode.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -83,8 +84,9 @@ static bool jsondec_tryparsech(jsondec *d, char ch) { } static void jsondec_parselit(jsondec *d, const char *lit) { + size_t avail = d->end - d->ptr; size_t len = strlen(lit); - if (d->end - d->ptr < len || memcmp(d->ptr, lit, len) != 0) { + if (avail < len || memcmp(d->ptr, lit, len) != 0) { jsondec_errf(d, "Expected: '%s'", lit); } d->ptr += len; @@ -473,7 +475,7 @@ static void jsondec_skipval(jsondec *d) { /* Base64 decoding for bytes fields. ******************************************/ -static int jsondec_base64_tablelookup(const char ch) { +static unsigned int jsondec_base64_tablelookup(const char ch) { /* Table includes the normal base64 chars plus the URL-safe variant. */ const signed char table[256] = { -1, -1, -1, -1, -1, -1, -1, @@ -677,7 +679,7 @@ static upb_msgval jsondec_int(jsondec *d, const upb_fielddef *f) { if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) { jsondec_err(d, "Integer out of range."); } - val.int32_val = val.int64_val; + val.int32_val = (int32_t)val.int64_val; } return val; @@ -713,7 +715,7 @@ static upb_msgval jsondec_uint(jsondec *d, const upb_fielddef *f) { if (val.uint64_val > UINT32_MAX) { jsondec_err(d, "Integer out of range."); } - val.uint32_val = val.uint64_val; + val.uint32_val = (uint32_t)val.uint64_val; } return val; @@ -960,15 +962,17 @@ static int jsondec_tsdigits(jsondec *d, const char **ptr, size_t digits, const char *end = p + digits; size_t after_len = after ? strlen(after) : 0; - assert(digits <= 9); /* int can't overflow. */ + UPB_ASSERT(digits <= 9); /* int can't overflow. */ if (jsondec_buftouint64(d, p, end, &val) != end || (after_len && memcmp(end, after, after_len) != 0)) { jsondec_err(d, "Malformed timestamp"); } + UPB_ASSERT(val < INT_MAX); + *ptr = end + after_len; - return val; + return (int)val; } static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) { @@ -977,7 +981,7 @@ static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) { if (p != end && *p == '.') { const char *nano_end = jsondec_buftouint64(d, p + 1, end, &nanos); - int digits = nano_end - p - 1; + int digits = (int)(nano_end - p - 1); int exp_lg10 = 9 - digits; if (digits > 9) { jsondec_err(d, "Too many digits for partial seconds"); @@ -986,14 +990,16 @@ static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) { *ptr = nano_end; } - return nanos; + UPB_ASSERT(nanos < INT_MAX); + + return (int)nanos; } /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */ int jsondec_epochdays(int y, int m, int d) { const uint32_t year_base = 4800; /* Before min year, multiple of 400. */ const uint32_t m_adj = m - 3; /* March-based month. */ - const uint32_t carry = m_adj > m ? 1 : 0; + const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0; const uint32_t adjust = carry ? 12 : 0; const uint32_t y_adj = y + year_base - carry; const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048; @@ -1038,7 +1044,7 @@ static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) { switch (*ptr++) { case '-': neg = true; - /* Fallthrough intended. */ + /* fallthrough */ case '+': if ((end - ptr) != 5) goto malformed; ofs = jsondec_tsdigits(d, &ptr, 2, ":00"); diff --git a/upb/json_encode.c b/upb/json_encode.c index 04c9594ed7b..21f661f8f6e 100644 --- a/upb/json_encode.c +++ b/upb/json_encode.c @@ -52,7 +52,7 @@ static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) { memcpy(e->ptr, data, len); e->ptr += len; } else { - memcpy(e->ptr, data, have); + if (have) memcpy(e->ptr, data, have); e->ptr += have; e->overflow += (len - have); } @@ -117,7 +117,7 @@ static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg, * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for * Processing Calendar Dates," Communications of the Association of * Computing Machines, vol. 11 (1968), p. 657. */ - L = (seconds / 86400) + 68569 + 2440588; + L = (int)(seconds / 86400) + 68569 + 2440588; N = 4 * L / 146097; L = L - (146097 * N + 3) / 4; I = 4000 * (L + 1) / 1461001; diff --git a/upb/msg.c b/upb/msg.c index fd6c59b0b2d..25747c84817 100644 --- a/upb/msg.c +++ b/upb/msg.c @@ -35,16 +35,17 @@ static size_t upb_msg_sizeof(const upb_msglayout *l) { return l->size + upb_msg_internalsize(l); } -static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) { - return UPB_PTR_AT(msg, -sizeof(upb_msg_internal), upb_msg_internal); +static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) { + ptrdiff_t size = sizeof(upb_msg_internal); + return UPB_PTR_AT(msg, -size, upb_msg_internal); } -static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) { - return UPB_PTR_AT(msg, -sizeof(upb_msg_internal), upb_msg_internal); +static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) { + return (upb_msg_internal*)upb_msg_getinternal_const(msg); } void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) { - size_t internal = upb_msg_internalsize(l); + ptrdiff_t internal = upb_msg_internalsize(l); void *mem = UPB_PTR_AT(msg, -internal, char); memset(mem, 0, l->size + internal); } diff --git a/upb/msg.h b/upb/msg.h index 66f04a0e80f..1ded2cf3e25 100644 --- a/upb/msg.h +++ b/upb/msg.h @@ -40,10 +40,10 @@ enum { typedef struct { uint32_t number; uint16_t offset; - int16_t presence; /* If >0, hasbit_index. If <0, ~oneof_index. */ + int16_t presence; /* If >0, hasbit_index. If <0, ~oneof_index. */ uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */ uint8_t descriptortype; - uint8_t label; + uint8_t label; /* google.protobuf.Label or _UPB_LABEL_* above. */ } upb_msglayout_field; typedef struct upb_msglayout { @@ -131,12 +131,12 @@ UPB_INLINE void _upb_clearhas_field(const upb_msg *msg, /** Oneof case access *********************************************************/ -UPB_INLINE int32_t *_upb_oneofcase(upb_msg *msg, size_t case_ofs) { - return PTR_AT(msg, case_ofs, int32_t); +UPB_INLINE uint32_t *_upb_oneofcase(upb_msg *msg, size_t case_ofs) { + return PTR_AT(msg, case_ofs, uint32_t); } -UPB_INLINE int32_t _upb_getoneofcase(const void *msg, size_t case_ofs) { - return *PTR_AT(msg, case_ofs, int32_t); +UPB_INLINE uint32_t _upb_getoneofcase(const void *msg, size_t case_ofs) { + return *PTR_AT(msg, case_ofs, uint32_t); } UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f) { @@ -144,13 +144,13 @@ UPB_INLINE size_t _upb_oneofcase_ofs(const upb_msglayout_field *f) { return ~(int64_t)f->presence; } -UPB_INLINE int32_t *_upb_oneofcase_field(upb_msg *msg, - const upb_msglayout_field *f) { +UPB_INLINE uint32_t *_upb_oneofcase_field(upb_msg *msg, + const upb_msglayout_field *f) { return _upb_oneofcase(msg, _upb_oneofcase_ofs(f)); } -UPB_INLINE int32_t _upb_getoneofcase_field(const upb_msg *msg, - const upb_msglayout_field *f) { +UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_msg *msg, + const upb_msglayout_field *f) { return _upb_getoneofcase(msg, _upb_oneofcase_ofs(f)); } @@ -162,6 +162,10 @@ UPB_INLINE bool _upb_isrepeated(const upb_msglayout_field *field) { return (field->label & 3) == UPB_LABEL_REPEATED; } +UPB_INLINE bool _upb_repeated_or_map(const upb_msglayout_field *field) { + return field->label >= UPB_LABEL_REPEATED; +} + /** upb_array *****************************************************************/ /* Our internal representation for repeated fields. */ diff --git a/upb/pb/decoder.h b/upb/pb/decoder.h index 709db49e96a..8c4d8bf5d5c 100644 --- a/upb/pb/decoder.h +++ b/upb/pb/decoder.h @@ -158,7 +158,9 @@ class upb::pb::DecoderPtr { * Setting the limit will fail if the parser is currently suspended at a depth * greater than this, or if memory allocation of the stack fails. */ size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); } - bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); } + bool set_max_nesting(size_t max) { + return upb_pbdecoder_setmaxnesting(ptr(), max); + } void Reset() { upb_pbdecoder_reset(ptr()); } diff --git a/upb/pb/varint.int.h b/upb/pb/varint.int.h index 59158d3b753..8ab0d9f1418 100644 --- a/upb/pb/varint.int.h +++ b/upb/pb/varint.int.h @@ -111,7 +111,8 @@ UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) { UPB_INLINE int upb_value_size(uint64_t val) { #ifdef __GNUC__ - int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */ + /* 0-based, undef if val == 0. */ + int high_bit = val ? 63 - __builtin_clzll(val) : 0; #else int high_bit = 0; uint64_t tmp = val; diff --git a/upb/reflection.c b/upb/reflection.c index 057da629cc0..487241b2b56 100644 --- a/upb/reflection.c +++ b/upb/reflection.c @@ -7,27 +7,30 @@ #include "upb/port_def.inc" -static char field_size[] = { - 0,/* 0 */ - 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */ - 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */ - 8, /* UPB_DESCRIPTOR_TYPE_INT64 */ - 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */ - 4, /* UPB_DESCRIPTOR_TYPE_INT32 */ - 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */ - 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */ - 1, /* UPB_DESCRIPTOR_TYPE_BOOL */ - sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */ - sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */ - sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */ - sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */ - 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */ - 4, /* UPB_DESCRIPTOR_TYPE_ENUM */ - 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */ - 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */ - 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */ - 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */ -}; +static size_t get_field_size(const upb_msglayout_field *f) { + static unsigned char sizes[] = { + 0,/* 0 */ + 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */ + 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */ + 8, /* UPB_DESCRIPTOR_TYPE_INT64 */ + 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */ + 4, /* UPB_DESCRIPTOR_TYPE_INT32 */ + 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */ + 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */ + 1, /* UPB_DESCRIPTOR_TYPE_BOOL */ + sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */ + sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */ + sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */ + sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */ + 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */ + 4, /* UPB_DESCRIPTOR_TYPE_ENUM */ + 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */ + 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */ + 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */ + 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */ + }; + return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype]; +} /* Strings/bytes are special-cased in maps. */ static char _upb_fieldtype_to_mapsize[12] = { @@ -59,9 +62,7 @@ static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) { const upb_msglayout_field *field = upb_fielddef_layout(f); const char *mem = UPB_PTR_AT(msg, field->offset, char); upb_msgval val = {0}; - int size = upb_fielddef_isseq(f) ? sizeof(void *) - : field_size[field->descriptortype]; - memcpy(&val, mem, size); + memcpy(&val, mem, get_field_size(field)); return val; } @@ -175,9 +176,8 @@ void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val, upb_arena *a) { const upb_msglayout_field *field = upb_fielddef_layout(f); char *mem = UPB_PTR_AT(msg, field->offset, char); - int size = upb_fielddef_isseq(f) ? sizeof(void *) - : field_size[field->descriptortype]; - memcpy(mem, &val, size); + UPB_UNUSED(a); /* We reserve the right to make set insert into a map. */ + memcpy(mem, &val, get_field_size(field)); if (field->presence > 0) { _upb_sethas_field(msg, field); } else if (in_oneof(field)) { @@ -188,18 +188,16 @@ void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val, void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) { const upb_msglayout_field *field = upb_fielddef_layout(f); char *mem = UPB_PTR_AT(msg, field->offset, char); - int size = upb_fielddef_isseq(f) ? sizeof(void *) - : field_size[field->descriptortype]; if (field->presence > 0) { _upb_clearhas_field(msg, field); } else if (in_oneof(field)) { - int32_t *oneof_case = _upb_oneofcase_field(msg, field); + uint32_t *oneof_case = _upb_oneofcase_field(msg, field); if (*oneof_case != field->number) return; *oneof_case = 0; } - memset(mem, 0, size); + memset(mem, 0, get_field_size(field)); } void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) { @@ -212,6 +210,7 @@ bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m, size_t i = *iter; const upb_msgval zero = {0}; const upb_fielddef *f; + UPB_UNUSED(ext_pool); while ((f = _upb_msgdef_field(m, (int)++i)) != NULL) { upb_msgval val = _upb_msg_getraw(msg, f); diff --git a/upb/table.c b/upb/table.c index c5ca3ac49bd..e89f72c6988 100644 --- a/upb/table.c +++ b/upb/table.c @@ -263,7 +263,7 @@ static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) { char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1); if (str == NULL) return 0; memcpy(str, &len, sizeof(uint32_t)); - memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len); + if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len); str[sizeof(uint32_t) + k2.str.len] = '\0'; return (uintptr_t)str; } @@ -277,10 +277,11 @@ static uint32_t strhash(upb_tabkey key) { static bool streql(upb_tabkey k1, lookupkey_t k2) { uint32_t len; char *str = upb_tabstr(k1, &len); - return len == k2.str.len && memcmp(str, k2.str.str, len) == 0; + return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0); } bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) { + UPB_UNUSED(ctype); /* TODO(haberman): rm */ return init(&t->t, 2, a); } @@ -473,6 +474,7 @@ bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2, } bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) { + UPB_UNUSED(ctype); /* TODO(haberman): rm */ return upb_inttable_sizedinit(t, 0, 4, a); } @@ -786,8 +788,8 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { int32_t sr; switch(align) { - case 1: t |= data[2] << 16; - case 2: t |= data[1] << 8; + case 1: t |= data[2] << 16; /* fallthrough */ + case 2: t |= data[1] << 8; /* fallthrough */ case 3: t |= data[0]; } @@ -825,9 +827,9 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { uint32_t k; switch(align) { - case 3: d |= data[2] << 16; - case 2: d |= data[1] << 8; - case 1: d |= data[0]; + case 3: d |= data[2] << 16; /* fallthrough */ + case 2: d |= data[1] << 8; /* fallthrough */ + case 1: d |= data[0]; /* fallthrough */ } k = (t >> sr) | (d << sl); @@ -840,15 +842,15 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { * Handle tail bytes */ switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; - case 1: h ^= data[0]; h *= m; + case 3: h ^= data[2] << 16; /* fallthrough */ + case 2: h ^= data[1] << 8; /* fallthrough */ + case 1: h ^= data[0]; h *= m; /* fallthrough */ }; } else { switch(len) { - case 3: d |= data[2] << 16; - case 2: d |= data[1] << 8; - case 1: d |= data[0]; + case 3: d |= data[2] << 16; /* fallthrough */ + case 2: d |= data[1] << 8; /* fallthrough */ + case 1: d |= data[0]; /* fallthrough */ case 0: h ^= (t >> sr) | (d << sl); h *= m; } } @@ -872,8 +874,8 @@ uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) { * Handle tail bytes */ switch(len) { - case 3: h ^= data[2] << 16; - case 2: h ^= data[1] << 8; + case 3: h ^= data[2] << 16; /* fallthrough */ + case 2: h ^= data[1] << 8; /* fallthrough */ case 1: h ^= data[0]; h *= m; }; diff --git a/upb/text_encode.c b/upb/text_encode.c index d426e67ea27..7c99228131e 100644 --- a/upb/text_encode.c +++ b/upb/text_encode.c @@ -27,7 +27,7 @@ static void txtenc_putbytes(txtenc *e, const void *data, size_t len) { memcpy(e->ptr, data, len); e->ptr += len; } else { - memcpy(e->ptr, data, have); + if (have) memcpy(e->ptr, data, have); e->ptr += have; e->overflow += (len - have); } @@ -263,7 +263,7 @@ static const char *txtenc_unknown(txtenc *e, const char *ptr, const char *end, tag = (uint32_t)tag_64; if ((tag & 7) == UPB_WIRE_TYPE_END_GROUP) { - CHK((tag >> 3) == groupnum); + CHK((tag >> 3) == (uint32_t)groupnum); return ptr; } @@ -295,10 +295,11 @@ static const char *txtenc_unknown(txtenc *e, const char *ptr, const char *end, } case UPB_WIRE_TYPE_DELIMITED: { uint64_t len; + size_t avail = end - ptr; char *start = e->ptr; size_t start_overflow = e->overflow; CHK(ptr = txtenc_parsevarint(ptr, end, &len)); - CHK(end - ptr >= len); + CHK(avail >= len); /* Speculatively try to parse as message. */ txtenc_putstr(e, "{"); @@ -310,10 +311,12 @@ static const char *txtenc_unknown(txtenc *e, const char *ptr, const char *end, txtenc_putstr(e, "}"); } else { /* Didn't work out, print as raw bytes. */ + upb_strview str; e->indent_depth--; e->ptr = start; e->overflow = start_overflow; - upb_strview str = {ptr, len}; + str.data = ptr; + str.size = len; txtenc_string(e, str, true); } ptr += len; diff --git a/upb/upb.c b/upb/upb.c index 84852f49a63..c2d406950d3 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -110,10 +110,10 @@ static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) { mem_block *block = ptr; block->next = a->freelist; - block->size = size; + block->size = (uint32_t)size; block->cleanups = 0; a->freelist = block; - a->last_size = size; + a->last_size = block->size; if (!a->freelist_tail) a->freelist_tail = block; a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char); diff --git a/upb/upb.h b/upb/upb.h index 19ee059b8f2..c3e1c5f3d59 100644 --- a/upb/upb.h +++ b/upb/upb.h @@ -271,7 +271,7 @@ typedef enum { UPB_DTYPE_SINT64 = 18 } upb_descriptortype_t; -#define UPB_MAP_BEGIN -1 +#define UPB_MAP_BEGIN ((size_t)-1) #include "upb/port_undef.inc" diff --git a/upbc/generator.cc b/upbc/generator.cc index af39cacd12e..d5c27fb2afa 100644 --- a/upbc/generator.cc +++ b/upbc/generator.cc @@ -43,7 +43,7 @@ static std::string DefSourceFilename(std::string proto_filename) { class Output { public: Output(protobuf::io::ZeroCopyOutputStream* stream) : stream_(stream) {} - ~Output() { stream_->BackUp(size_); } + ~Output() { stream_->BackUp((int)size_); } template void operator()(absl::string_view format, const Arg&... arg) { @@ -922,9 +922,9 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) { } bool Generator::Generate(const protobuf::FileDescriptor* file, - const std::string& parameter, + const std::string& /* parameter */, protoc::GeneratorContext* context, - std::string* error) const { + std::string* /* error */) const { Output h_output(context->Open(HeaderFilename(file->name()))); WriteHeader(file, h_output);