From 173cc11e2dd69bf9b39ef0e56bfa87bda32bdf8e Mon Sep 17 00:00:00 2001 From: kamalyzl Date: Mon, 1 Jul 2019 16:50:30 -0500 Subject: [PATCH 1/2] change strcpy --- src/symbolize.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/symbolize.cc b/src/symbolize.cc index 1ffc6079a..c3fcf05f4 100644 --- a/src/symbolize.cc +++ b/src/symbolize.cc @@ -844,7 +844,7 @@ static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out, Dl_info info; if (dladdr(pc, &info)) { if ((int)strlen(info.dli_sname) < out_size) { - strcpy(out, info.dli_sname); + strlcpy(out, info.dli_sname); // Symbolization succeeded. Now we try to demangle the symbol. DemangleInplace(out, out_size); return true; From 78f9880f11ad5588f3fa5e189d23ec48d9c57cb8 Mon Sep 17 00:00:00 2001 From: kamalyzl Date: Mon, 1 Jul 2019 16:58:45 -0500 Subject: [PATCH 2/2] update variables --- src/demangle.cc | 2 +- src/googletest.h | 4 ++-- src/logging.cc | 2 +- src/stl_logging_unittest.cc | 2 +- src/symbolize.cc | 6 +++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/demangle.cc b/src/demangle.cc index 9369f9a65..5ac87f623 100644 --- a/src/demangle.cc +++ b/src/demangle.cc @@ -1340,7 +1340,7 @@ bool Demangle(const char *mangled, char *out, int out_size) { // Extract the string `(?...)` const char *rparen = strchr(lparen, ')'); size_t length = rparen - lparen - 1; - strncpy(buffer, lparen + 1, length); + strlcpy(buffer, lparen + 1, length); buffer[length] = '\0'; mangled = buffer; } // Else the symbol wasn't inside a set of parentheses diff --git a/src/googletest.h b/src/googletest.h index 49ddbc0a5..5edad46bc 100644 --- a/src/googletest.h +++ b/src/googletest.h @@ -450,8 +450,8 @@ static inline string Munge(const string& filename) { string line = MungeLine(buf); char null_str[256]; char ptr_str[256]; - sprintf(null_str, "%p", static_cast(NULL)); - sprintf(ptr_str, "%p", reinterpret_cast(PTR_TEST_VALUE)); + snprintf(null_str, "%p", static_cast(NULL)); + snprintf(ptr_str, "%p", reinterpret_cast(PTR_TEST_VALUE)); StringReplace(&line, "__NULLP__", null_str); StringReplace(&line, "__PTRTEST__", ptr_str); diff --git a/src/logging.cc b/src/logging.cc index 0c86cf622..8e6235032 100644 --- a/src/logging.cc +++ b/src/logging.cc @@ -2086,7 +2086,7 @@ int posix_strerror_r(int err, char *buf, size_t len) { return -1; } #endif - strncat(buf, rc, len-1); + strlcat(buf, rc, len-1); return 0; } } diff --git a/src/stl_logging_unittest.cc b/src/stl_logging_unittest.cc index 269094c07..dbde79a81 100644 --- a/src/stl_logging_unittest.cc +++ b/src/stl_logging_unittest.cc @@ -136,7 +136,7 @@ static void TestSTLLogging() { v.push_back(i); if (i > 0) expected += ' '; char buf[256]; - sprintf(buf, "%d", i); + snprintf(buf, "%d", i); expected += buf; } v.push_back(100); diff --git a/src/symbolize.cc b/src/symbolize.cc index c3fcf05f4..885443b0d 100644 --- a/src/symbolize.cc +++ b/src/symbolize.cc @@ -651,7 +651,7 @@ OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc, if (object_fd < 0) { // Failed to open object file. Copy the object file name to // |out_file_name|. - strncpy(out_file_name, cursor, out_file_name_size); + strlcpy(out_file_name, cursor, out_file_name_size); // Making sure |out_file_name| is always null-terminated. out_file_name[out_file_name_size - 1] = '\0'; return -1; @@ -735,7 +735,7 @@ static void SafeAppendString(const char* source, char* dest, int dest_size) { SAFE_ASSERT(dest_string_length < dest_size); dest += dest_string_length; dest_size -= dest_string_length; - strncpy(dest, source, dest_size); + strlcpy(dest, source, dest_size); // Making sure |dest| is always null-terminated. dest[dest_size - 1] = '\0'; } @@ -909,7 +909,7 @@ static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out, reinterpret_cast(pc), 0, symbol); if (ret == 1 && static_cast(symbol->NameLen) < out_size) { // `NameLen` does not include the null terminating character. - strncpy(out, symbol->Name, static_cast(symbol->NameLen) + 1); + strlcpy(out, symbol->Name, static_cast(symbol->NameLen) + 1); out[static_cast(symbol->NameLen)] = '\0'; // Symbolization succeeded. Now we try to demangle the symbol. DemangleInplace(out, out_size);