From a619fa060d1dcec528dd9f104c860e020a50e542 Mon Sep 17 00:00:00 2001 From: wenlong12 Date: Wed, 16 Feb 2022 18:01:02 +0800 Subject: [PATCH] fix bytrace cidecheck issue Signed-off-by: wenlong12 Signed-off-by: wenlong12 --- bin/src/bytrace.cpp | 43 ++++++++++++++----- bin/src/bytrace_impl.cpp | 5 +-- .../common/native/bytrace_ndk_test.cpp | 9 +++- interfaces/innerkits/native/include/bytrace.h | 1 - 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/bin/src/bytrace.cpp b/bin/src/bytrace.cpp index 74e7af5..3f6bb59 100755 --- a/bin/src/bytrace.cpp +++ b/bin/src/bytrace.cpp @@ -15,6 +15,7 @@ #include "bytrace.h" #include +#include #include #include #include @@ -26,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -48,7 +48,7 @@ struct option g_longOptions[] = { { "list_categories", no_argument, nullptr, 0 }, { "overwrite", no_argument, nullptr, 0 }, }; -const int CHUNK_SIZE = 65536; +const unsigned int CHUNK_SIZE = 65536; const int BLOCK_SIZE = 4096; const string TRACE_TAG_PROPERTY = "debug.bytrace.tags.enableflags"; @@ -100,8 +100,8 @@ static bool IsTraceMounted() g_traceRootPath = tracefsPath; return true; } - - fprintf(stderr, "Error: Did not find trace folder\n"); + if (fprintf(stderr, "Error: Did not find trace folder\n") == -1) { + } return false; } @@ -168,9 +168,28 @@ static bool IsTagSupported(const string& name) return findPath; } +static string CanonicalizeSpecPath(const char* src) +{ + char resolvedPath[PATH_MAX] = { 0 }; +#if defined(_WIN32) + if (!_fullpath(resolvedPath, src, PATH_MAX)) { + fprintf(stderr, "Error: _fullpath %s failed", src); + return ""; + } +#else + if (realpath(src, resolvedPath) == nullptr) { + fprintf(stderr, "Error: _fullpath %s failed", src); + return ""; + } +#endif + string res(resolvedPath); + return res; +} + static string ReadFile(const string& filename) { - ifstream fin((g_traceRootPath + filename).c_str()); + string resolvedPath = CanonicalizeSpecPath((g_traceRootPath + filename).c_str()); + ifstream fin(resolvedPath.c_str()); if (!fin.is_open()) { fprintf(stderr, "open file: %s failed!", (g_traceRootPath + filename).c_str()); return ""; @@ -546,7 +565,7 @@ static void DumpCompressedTrace(int traceFd, int outFd) fprintf(stderr, "Error: initializing zlib: %d\n", ret); return; } - int have; + unsigned int have = 0; std::unique_ptr in = std::make_unique(CHUNK_SIZE); std::unique_ptr out = std::make_unique(CHUNK_SIZE); int flush = Z_NO_FLUSH; @@ -586,7 +605,8 @@ static void DumpCompressedTrace(int traceFd, int outFd) static void DumpTrace(int outFd, const string& path) { - int traceFd = open((g_traceRootPath + path).c_str(), O_RDWR); + string resolvedPath = CanonicalizeSpecPath((g_traceRootPath + path).c_str()); + int traceFd = open(resolvedPath.c_str(), O_RDWR); if (traceFd == -1) { fprintf(stderr, "error opening %s: %s (%d)\n", path.c_str(), strerror(errno), errno); @@ -613,7 +633,8 @@ static bool MarkOthersClockSync() { constexpr unsigned int bufferSize = 128; // buffer size char buffer[bufferSize] = { 0 }; - int fd = open((g_traceRootPath + TRACE_MARKER_PATH).c_str(), O_WRONLY); + string resolvedPath = CanonicalizeSpecPath((g_traceRootPath + TRACE_MARKER_PATH).c_str()); + int fd = open(resolvedPath.c_str(), O_WRONLY); if (fd == -1) { fprintf(stderr, "Error: opening %s: %s (%d)\n", TRACE_MARKER_PATH.c_str(), strerror(errno), errno); return false; @@ -799,7 +820,8 @@ static void InitAllSupportTags() g_tagMap["ace"] = { "ace", "ACE development framework", BYTRACE_TAG_ACE, USER, {}}; g_tagMap["notification"] = { "notification", "Notification Module", BYTRACE_TAG_NOTIFICATION, USER, {}}; g_tagMap["misc"] = { "misc", "Misc Module", BYTRACE_TAG_MISC, USER, {}}; - g_tagMap["multimodalinput"] = { "multimodalinput", "Multimodal Input Module", BYTRACE_TAG_MULTIMODALINPUT, USER, {}}; + g_tagMap["multimodalinput"] = { "multimodalinput", "Multimodal Input Module", + BYTRACE_TAG_MULTIMODALINPUT, USER, {}}; g_tagMap["sensors"] = { "sensors", "Sensors Module", BYTRACE_TAG_SENSORS, USER, {}}; g_tagMap["msdp"] = { "msdp", "Multimodal Sensor Data Platform", BYTRACE_TAG_MSDP, USER, {}}; g_tagMap["dsoftbus"] = { "dsoftbus", "Distributed Softbus", BYTRACE_TAG_DSOFTBUS, USER, {}}; @@ -862,7 +884,8 @@ int main(int argc, char **argv) int outFd = STDOUT_FILENO; if (g_outputFile.size() > 0) { printf("write trace to %s\n", g_outputFile.c_str()); - outFd = open(g_outputFile.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + string resolvedPath = CanonicalizeSpecPath(g_outputFile.c_str()); + outFd = open(resolvedPath.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); } if (outFd == -1) { printf("Failed to open '%s', err=%d", g_outputFile.c_str(), errno); diff --git a/bin/src/bytrace_impl.cpp b/bin/src/bytrace_impl.cpp index 9019b11..6bc69a6 100755 --- a/bin/src/bytrace_impl.cpp +++ b/bin/src/bytrace_impl.cpp @@ -13,12 +13,11 @@ * limitations under the License. */ #include +#include #include #include #include -#include #include -#include #include #include #include "bytrace.h" @@ -120,7 +119,7 @@ void OpenTraceMarkerFile() } }; // namespace -inline void AddBytraceMarker(MarkerType type, uint64_t tag, std::string name, std::string value) +void AddBytraceMarker(MarkerType type, uint64_t tag, const std::string& name, const std::string& value) { if (UNEXPECTANTLY(!g_isBytraceInit)) { std::call_once(g_onceFlag, OpenTraceMarkerFile); diff --git a/bin/test/unittest/common/native/bytrace_ndk_test.cpp b/bin/test/unittest/common/native/bytrace_ndk_test.cpp index 95189b9..3430172 100755 --- a/bin/test/unittest/common/native/bytrace_ndk_test.cpp +++ b/bin/test/unittest/common/native/bytrace_ndk_test.cpp @@ -283,7 +283,7 @@ MyTrace GetTraceResult(const string& checkContent, const vector& list) regex pattern(checkContent); smatch match; Param param {""}; - for (int i = list.size() - 1; i >= 0; i--) { + for (unsigned int i = list.size() - 1; i >= 0; i--) { if (regex_match(list[i], match, pattern)) { param.m_task = match[TASK]; param.m_tid = match[TID]; @@ -341,8 +341,13 @@ bool CleanTrace() static stringstream ReadFile(const string& filename) { - ifstream fin(filename.c_str()); stringstream ss; + char resolvedPath[PATH_MAX] = { 0 }; + if (realpath(filename.c_str(), resolvedPath) == nullptr) { + fprintf(stderr, "Error: _fullpath %s failed", filename.c_str()); + return ss; + } + ifstream fin(resolvedPath); if (!fin.is_open()) { fprintf(stderr, "opening file: %s failed!", filename.c_str()); return ss; diff --git a/interfaces/innerkits/native/include/bytrace.h b/interfaces/innerkits/native/include/bytrace.h index dab48ca..a23cc49 100755 --- a/interfaces/innerkits/native/include/bytrace.h +++ b/interfaces/innerkits/native/include/bytrace.h @@ -22,7 +22,6 @@ extern "C" { #endif - constexpr uint64_t BYTRACE_TAG_NEVER = 0; // This tag is never enabled. constexpr uint64_t BYTRACE_TAG_ALWAYS = (1ULL << 0); // This tag is always enabled. constexpr uint64_t BYTRACE_TAG_OHOS = (1ULL << 30); // OHOS generic tag.