From 2c63d76205c1a7533f78b73255d0d7923ca225cc Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sun, 2 Sep 2018 20:01:32 -0400 Subject: [PATCH 1/8] protoc-c: Fix -Wsign-compare diagnostics MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit fixes the following compiler diagnostics: ../protoc-c/c_helpers.cc: In function ‘void google::protobuf::compiler::c::PrintComment(google::protobuf::io::Printer*, std::__cxx11::string)’: ../protoc-c/c_helpers.cc:221:25: warning: comparison of integer expressions of different signedness: ‘int’ and ‘std::vector >::size_type’ {aka ‘long unsigned int’} [-Wsign-compare] for (int i = 0; i < comment_lines.size(); i++) ~~^~~~~~~~~~~~~~~~~~~~~~ ../protoc-c/c_helpers.cc: In function ‘std::set > google::protobuf::compiler::c::MakeKeywordsMap()’: ../protoc-c/c_helpers.cc:273:21: warning: comparison of integer expressions of different signedness: ‘int’ and ‘long unsigned int’ [-Wsign-compare] for (int i = 0; i < GOOGLE_ARRAYSIZE(kKeywordList); i++) { ^ --- protoc-c/c_helpers.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protoc-c/c_helpers.cc b/protoc-c/c_helpers.cc index 6fd0cd3..ea693c6 100644 --- a/protoc-c/c_helpers.cc +++ b/protoc-c/c_helpers.cc @@ -234,7 +234,7 @@ void PrintComment (io::Printer* printer, std::string comment) std::vector comment_lines; SplitStringUsing (comment, "\r\n", &comment_lines); printer->Print ("/*\n"); - for (int i = 0; i < comment_lines.size(); i++) + for (size_t i = 0; i < comment_lines.size(); i++) { if (!comment_lines[i].empty()) { @@ -286,7 +286,7 @@ const char* const kKeywordList[] = { std::set MakeKeywordsMap() { std::set result; - for (int i = 0; i < GOOGLE_ARRAYSIZE(kKeywordList); i++) { + for (size_t i = 0; i < GOOGLE_ARRAYSIZE(kKeywordList); i++) { result.insert(kKeywordList[i]); } return result; From de5ea41b4f60ebc802365c192623e220fe4590ee Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sun, 2 Sep 2018 17:43:52 -0400 Subject: [PATCH 2/8] t/: Use "int main(void)" for the main C signature This commit updates a few tests where we aren't using the correct C signature for main() or are not using any of its parameters (in the case of t/version/version.c.) --- t/generated-code2/test-generated-code2.c | 2 +- t/version/version.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/t/generated-code2/test-generated-code2.c b/t/generated-code2/test-generated-code2.c index 15f5dc8..5198af6 100755 --- a/t/generated-code2/test-generated-code2.c +++ b/t/generated-code2/test-generated-code2.c @@ -2370,7 +2370,7 @@ static Test tests[] = }; #define n_tests (sizeof(tests)/sizeof(Test)) -int main () +int main(void) { unsigned i; for (i = 0; i < n_tests; i++) diff --git a/t/version/version.c b/t/version/version.c index 6ee3c41..9fec1ab 100644 --- a/t/version/version.c +++ b/t/version/version.c @@ -35,7 +35,7 @@ #include "protobuf-c.h" int -main(int argc, char **argv) +main(void) { printf("PACKAGE_VERSION = %s\n", PACKAGE_VERSION); From 5312647592dec35c51060a9ceaee4fe08cf61a1f Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sun, 2 Sep 2018 18:03:48 -0400 Subject: [PATCH 3/8] protoc-c: Rename 'legacy_name' to 'standalone_name' --- protoc-c/main.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protoc-c/main.cc b/protoc-c/main.cc index 93c42d0..2095a49 100644 --- a/protoc-c/main.cc +++ b/protoc-c/main.cc @@ -9,9 +9,9 @@ int main(int argc, char* argv[]) { std::string invocation_name = argv[0]; std::string invocation_basename = invocation_name.substr(invocation_name.find_last_of("/") + 1); - const std::string legacy_name = "protoc-c"; + const std::string standalone_name = "protoc-c"; - if (invocation_basename == legacy_name) { + if (invocation_basename == standalone_name) { google::protobuf::compiler::CommandLineInterface cli; cli.RegisterGenerator("--c_out", &c_generator, "Generate C/H files."); cli.SetVersionInfo(PACKAGE_STRING); From d465ae9d442973b329da8a104b3a6d4520a02482 Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Tue, 20 Sep 2022 10:13:35 +0200 Subject: [PATCH 4/8] Fix codesmell "or with 0" in generated flags Signed-off-by: Daniel Egger [edmonds: From https://github.com/protobuf-c/protobuf-c/pull/523.] [edmonds: Adjust find string from "0 |" to "0 | ", test against `== 0` rather than `!= std::string::npos` so that the find/erase calls are anchored to the same portion of the string.] --- protoc-c/c_field.cc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/protoc-c/c_field.cc b/protoc-c/c_field.cc index 0716744..d49c001 100644 --- a/protoc-c/c_field.cc +++ b/protoc-c/c_field.cc @@ -159,6 +159,11 @@ void FieldGenerator::GenerateDescriptorInitializerGeneric(io::Printer* printer, if (oneof != NULL) variables["flags"] += " | PROTOBUF_C_FIELD_FLAG_ONEOF"; + // Eliminate codesmell "or with 0" + if (variables["flags"].find("0 | ") == 0) { + variables["flags"].erase(0, 4); + } + printer->Print("{\n"); if (descriptor_->file()->options().has_optimize_for() && descriptor_->file()->options().optimize_for() == From 0ce231b16dcdf948229de25b070774d7bb9a30cc Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sat, 8 Jul 2023 21:18:06 -0400 Subject: [PATCH 5/8] cmake: Add -I${PROTOBUF_INCLUDE_DIR} to protoc invocation Apparently in some cases the protobuf compiler is unable to find its own files, and this fix has been reported to work. See https://github.com/protobuf-c/protobuf-c/issues/491. --- build-cmake/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-cmake/CMakeLists.txt b/build-cmake/CMakeLists.txt index c71dd89..379dd1a 100644 --- a/build-cmake/CMakeLists.txt +++ b/build-cmake/CMakeLists.txt @@ -101,7 +101,7 @@ SET(CMAKE_CXX_STANDARD_REQUIRED ON) SET(CMAKE_CXX_EXTENSIONS OFF) ADD_CUSTOM_COMMAND(OUTPUT protobuf-c/protobuf-c.pb.cc protobuf-c/protobuf-c.pb.h COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} - ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I${MAIN_DIR} ${MAIN_DIR}/protobuf-c/protobuf-c.proto) + ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I${PROTOBUF_INCLUDE_DIR} -I${MAIN_DIR} ${MAIN_DIR}/protobuf-c/protobuf-c.proto) FILE(GLOB PROTOC_GEN_C_SRC ${MAIN_DIR}/protoc-c/*.h ${MAIN_DIR}/protoc-c/*.cc ) ADD_EXECUTABLE(protoc-gen-c ${PROTOC_GEN_C_SRC} protobuf-c/protobuf-c.pb.cc protobuf-c/protobuf-c.pb.h) target_include_directories(protoc-gen-c From bee044643e510854b46783606f358f7b2f373557 Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sat, 8 Jul 2023 21:31:23 -0400 Subject: [PATCH 6/8] protobuf-c: parse_required_member(): Remove duplicate NULL check `*pstr != NULL` was already checked here. See https://github.com/protobuf-c/protobuf-c/issues/517. --- protobuf-c/protobuf-c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protobuf-c/protobuf-c.c b/protobuf-c/protobuf-c.c index 3d676a2..d740d93 100644 --- a/protobuf-c/protobuf-c.c +++ b/protobuf-c/protobuf-c.c @@ -2557,7 +2557,7 @@ parse_required_member(ScannedMember *scanned_member, if (maybe_clear && *pstr != NULL) { const char *def = scanned_member->field->default_value; - if (*pstr != NULL && *pstr != def) + if (*pstr != def) do_free(allocator, *pstr); } *pstr = do_alloc(allocator, len - pref_len + 1); From 951a5278e0e14522c991a5b57aae0643cc80a1be Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sat, 8 Jul 2023 21:34:09 -0400 Subject: [PATCH 7/8] protobuf-c: pack_buffer_packed_payload(): Remove unused increment Since this case immediately returns, the `rv` variable is not used. See https://github.com/protobuf-c/protobuf-c/issues/517. --- protobuf-c/protobuf-c.c | 1 - 1 file changed, 1 deletion(-) diff --git a/protobuf-c/protobuf-c.c b/protobuf-c/protobuf-c.c index d740d93..4b8b3f2 100644 --- a/protobuf-c/protobuf-c.c +++ b/protobuf-c/protobuf-c.c @@ -1888,7 +1888,6 @@ pack_buffer_packed_payload(const ProtobufCFieldDescriptor *field, for (i = 0; i < count; i++) { unsigned len = boolean_pack(((protobuf_c_boolean *) array)[i], scratch); buffer->append(buffer, len, scratch); - rv += len; } return count; default: From cda9f8475264c6d2ed3510f8acd7b9a6769f93b1 Mon Sep 17 00:00:00 2001 From: Robert Edmonds Date: Sat, 8 Jul 2023 21:45:14 -0400 Subject: [PATCH 8/8] protoc-c: Silence compilers that complain about sprintf() --- protoc-c/c_helpers.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protoc-c/c_helpers.cc b/protoc-c/c_helpers.cc index ea693c6..7ae356d 100644 --- a/protoc-c/c_helpers.cc +++ b/protoc-c/c_helpers.cc @@ -526,7 +526,7 @@ static int CEscapeInternal(const char* src, int src_len, char* dest, if (!isprint(*src) || (last_hex_escape && isxdigit(*src))) { if (dest_len - used < 4) // need space for 4 letter escape return -1; - sprintf(dest + used, (use_hex ? "\\x%02x" : "\\%03o"), + snprintf(dest + used, dest_len - used, (use_hex ? "\\x%02x" : "\\%03o"), static_cast(*src)); is_hex_escape = use_hex; used += 4;