diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index e201517ba30e8e78157932a0c57d5bfc88bfaba9..a364ea8b032cbf154f31e3bc823ff66f30953da5 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -69,10 +69,7 @@ template("libhilog_source") { "$utils_root/log_utils.cpp", ] - vsnprintf_sources = [ - "$vsnprintf_root/vsnprintf_s_p.c", - "$vsnprintf_root/vsprintf_p.c", - ] + vsnprintf_sources = [ "$vsnprintf_root/vsnprintf_s_p.c" ] sources = [ "hilog.cpp", diff --git a/frameworks/libhilog/utils/log_print.cpp b/frameworks/libhilog/utils/log_print.cpp index 37b3cb6266f97da92d1f2456b1dfe11d09a3fdfa..4509da2a105e0aca6ccd3980a6a6f83a569b6752 100644 --- a/frameworks/libhilog/utils/log_print.cpp +++ b/frameworks/libhilog/utils/log_print.cpp @@ -171,7 +171,7 @@ void LogPrintWithFormat(const LogContent& content, const LogFormat& format, std: if (*pScan == '\n') { char tmp[MAX_LOG_LEN]; int len = static_cast(pScan - pHead); - errno_t ret = memcpy_s(tmp, MAX_LOG_LEN - 1, pHead, len); + errno_t ret = memcpy_s(tmp, MAX_LOG_LEN, pHead, len); if (ret != EOK) { break; } diff --git a/frameworks/libhilog/vsnprintf/include/vsprintf_p.h b/frameworks/libhilog/vsnprintf/include/vsprintf_p.h deleted file mode 100644 index fecb5fca01fdf49fd50e117d1b87321f07315612..0000000000000000000000000000000000000000 --- a/frameworks/libhilog/vsnprintf/include/vsprintf_p.h +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef HILOG_OVERRIDE_VSPRINTF_P_H -#define HILOG_OVERRIDE_VSPRINTF_P_H - -#include -#include - -int VsprintfP(char *strDest, size_t destMax, const char *format, va_list argList); - -#endif /* HILOG_OVERRIDE_VSPRINTF_P_H */ \ No newline at end of file diff --git a/frameworks/libhilog/vsnprintf/output_p.inl b/frameworks/libhilog/vsnprintf/output_p.inl index 06a14fc21332aa57ccf62dcbf3ed814229d8f0fb..7680ea40d6c8761f5ee96e3fe5feba92c38cad37 100644 --- a/frameworks/libhilog/vsnprintf/output_p.inl +++ b/frameworks/libhilog/vsnprintf/output_p.inl @@ -19,11 +19,13 @@ * And sufficient input validation is performed before calling */ -#include "vsprintf_p.h" +#include #ifndef OUTPUT_P_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 #define OUTPUT_P_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 +#undef SECUREC_COMPATIBLE_LINUX_FORMAT + #define SECUREC_FLOAT_BUFSIZE (309+40) /* max float point value */ #define SECUREC_FLOAT_BUFSIZE_LB (4932+40) /* max long double value */ @@ -67,7 +69,7 @@ static int SecIndirectSprintf(char *strDest, size_t destMax, const char *format, va_start(arglist, format); SECUREC_MASK_MSVC_CRT_WARNING - ret = VsprintfP(strDest, destMax, format, arglist); + ret = vsprintf_s(strDest, destMax, format, arglist); SECUREC_END_MASK_MSVC_CRT_WARNING va_end(arglist); (void)arglist; /* to clear e438 last value assigned not used , the compiler will optimize this code */ @@ -107,7 +109,7 @@ static int SecIsSameSize(size_t sizeA, size_t sizeB) ++(src); \ } \ } else { \ - (void)memcpy(_stream->cur, src, ((size_t)(unsigned int)txtLen * (sizeof(SecChar)))); \ + (void)memcpy_s(_stream->cur, _stream->count, src, ((size_t)(unsigned int)txtLen * (sizeof(SecChar)))); \ _stream->cur += (size_t)(unsigned int)txtLen * (sizeof(SecChar)); \ } \ _stream->count -= txtLen * (int)(sizeof(SecChar)); \ diff --git a/frameworks/libhilog/vsnprintf/vsprintf_p.c b/frameworks/libhilog/vsnprintf/vsprintf_p.c deleted file mode 100644 index 1df454f2109a11c1b1db7fab55545f94a109f84a..0000000000000000000000000000000000000000 --- a/frameworks/libhilog/vsnprintf/vsprintf_p.c +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "vsprintf_p.h" - -#include - -int VsprintfP(char *strDest, size_t destMax, const char *format, va_list argList) -{ - return vsprintf_s(strDest, destMax, format, argList); -} \ No newline at end of file diff --git a/interfaces/native/innerkits/BUILD.gn b/interfaces/native/innerkits/BUILD.gn index 9bb67ec28337d4187c8750ef6223a69278cd7467..ab9f1899d5191e0bb39c646c5ba679ae7bdf3bad 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -173,10 +173,7 @@ ohos_static_library("libhilog_base") { "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", ] - vsnprintf_sources = [ - "$vsnprintf_root/vsnprintf_s_p.c", - "$vsnprintf_root/vsprintf_p.c", - ] + vsnprintf_sources = [ "$vsnprintf_root/vsnprintf_s_p.c" ] sources = [ "$libhilog_base_root/hilog_base.c" ] sources += vsnprintf_sources diff --git a/services/hilogd/include/log_data.h b/services/hilogd/include/log_data.h index c793c964e6d6bd4f7b959fc99fb9360860bbf68c..ff01086ba4db530bf3500211f8d77779fb3ccb51 100644 --- a/services/hilogd/include/log_data.h +++ b/services/hilogd/include/log_data.h @@ -53,10 +53,10 @@ struct HilogData { } tag = tmp; content = tmp + mtagLen; - if (strncpy_s(tag, mtagLen + 1, mtag, mtagLen - 1)) { + if (strncpy_s(tag, MAX_TAG_LEN, mtag, mtagLen - 1)) { return; } - if (strncpy_s(content, mfmtLen + 1, mfmt, mfmtLen - 1)) { + if (strncpy_s(content, MAX_LOG_LEN, mfmt, mfmtLen - 1)) { return; } } diff --git a/services/hilogd/kmsg_parser.cpp b/services/hilogd/kmsg_parser.cpp index 8417b000ebc48c7199e35ab04579c2f50a7b2e22..8c6631bd9c93b7490ade94e22b34c03127cbfe34 100644 --- a/services/hilogd/kmsg_parser.cpp +++ b/services/hilogd/kmsg_parser.cpp @@ -97,10 +97,10 @@ std::optional KmsgParser::ParseKmsg(const std::vector& km (void)clock_gettime(CLOCK_REALTIME, &ts); msg.tv_sec = static_cast(ts.tv_sec); msg.tv_nsec = static_cast(ts.tv_nsec); - if (strncpy_s(msg.tag, tagLen + 1, tagStr.c_str(), tagLen) != 0) { + if (strncpy_s(msg.tag, msgLen - sizeof(HilogMsg), tagStr.c_str(), tagLen) != 0) { return {}; } - if (strncpy_s(CONTENT_PTR((&msg)), MAX_LOG_LEN, kmsgStr.c_str(), len) != 0) { + if (strncpy_s(CONTENT_PTR((&msg)), msgLen - sizeof(HilogMsg) - tagLen, kmsgStr.c_str(), len) != 0) { return {}; } return msgWrap; diff --git a/services/hilogd/log_compress.cpp b/services/hilogd/log_compress.cpp index c05d6a053e100116494f8b1b0bcc87f2cf19cd11..9881d8c2dc0bbf216ee54bf379d44b92a070b990 100644 --- a/services/hilogd/log_compress.cpp +++ b/services/hilogd/log_compress.cpp @@ -160,7 +160,7 @@ int ZstdCompress::Compress(const LogPersisterBuffer &inBuffer, LogPersisterBuffe do { ZSTD_outBuffer output = {buffOut, CHUNK, 0}; size_t const remaining = ZSTD_compressStream2(cctx, &output, &input, mode); - if (memmove_s(zdata + dst_pos, zdlen, reinterpret_cast(buffOut), output.pos) != 0) { + if (memmove_s(zdata + dst_pos, zdlen - dst_pos, reinterpret_cast(buffOut), output.pos) != 0) { return -1; } dst_pos += output.pos; diff --git a/services/hilogd/log_persister_rotator.cpp b/services/hilogd/log_persister_rotator.cpp index 68f1fff81f179a4e39dd0fecc0e0017d1a7ba19d..1c697b425f8eb7ac46a23bfa83f86158fa2fa9ff 100644 --- a/services/hilogd/log_persister_rotator.cpp +++ b/services/hilogd/log_persister_rotator.cpp @@ -32,7 +32,9 @@ namespace HiviewDFX { std::string GetFileNameIndex(const int index) { char res[MAX_LOG_INDEX_LEN]; - (void)snprintf_s(res, sizeof(res), sizeof(res) - 1, "%03d", index % MAX_LOG_FILE_NUM); + if (snprintf_s(res, sizeof(res), sizeof(res) - 1, "%03d", index % MAX_LOG_FILE_NUM) < 0) { + return ""; + } std::string fileNameIndex(res); return fileNameIndex; } diff --git a/services/hilogd/service_controller.cpp b/services/hilogd/service_controller.cpp index 7c9afd9c0f40aee609df2f83a394df7c93e29da7..a32581851506c4c0e16079f846e64de7eba2c422 100644 --- a/services/hilogd/service_controller.cpp +++ b/services/hilogd/service_controller.cpp @@ -470,14 +470,18 @@ void ServiceController::LogFilterFromOutputRqst(const OutputRqst& rqst, LogFilte filter.blackTag = rqst.blackTag; filter.tagCount = rqst.tagCount; for (i = 0; i < rqst.tagCount; i++) { - (void)strncpy_s(filter.tags[i], MAX_TAG_LEN, rqst.tags[i], MAX_TAG_LEN - 1); + if (strncpy_s(filter.tags[i], MAX_TAG_LEN, rqst.tags[i], MAX_TAG_LEN - 1) != EOK) { + return; + } } filter.blackPid = rqst.blackPid; filter.pidCount = rqst.pidCount; for (i = 0; i < rqst.pidCount; i++) { filter.pids[i] = rqst.pids[i]; } - (void)strncpy_s(filter.regex, MAX_REGEX_STR_LEN, rqst.regex, MAX_REGEX_STR_LEN - 1); + if (strncpy_s(filter.regex, MAX_REGEX_STR_LEN, rqst.regex, MAX_REGEX_STR_LEN - 1) != EOK) { + return; + } filter.Print(); // Permission check uid_t uid = m_communicationSocket->GetUid(); @@ -576,7 +580,9 @@ void ServiceController::PersistStartRqst2Msg(const PersistStartRqst &rqst, LogPe fileName = (isKmsgType ? "hilog_kmsg" : "hilog"); } string filePath = LOG_PERSISTER_DIR + fileName; - (void)strncpy_s(msg.filePath, FILE_PATH_MAX_LEN, filePath.c_str(), filePath.length()); + if (strncpy_s(msg.filePath, FILE_PATH_MAX_LEN, filePath.c_str(), filePath.length()) != EOK) { + return; + } } int StartPersistStoreJob(const PersistRecoveryInfo& info, HilogBuffer& hilogBuffer, bool restore) @@ -671,9 +677,13 @@ void ServiceController::HandlePersistQueryRqst(const PersistQueryRqst& rqst) task.fileNum = it->fileNum; task.fileSize = it->fileSize; task.outputFilter.types = it->logType; - (void)strncpy_s(task.fileName, MAX_FILE_NAME_LEN, it->filePath, MAX_FILE_NAME_LEN - 1); - (void)strncpy_s(task.stream, MAX_STREAM_NAME_LEN, - LogCompress::CompressType2Str(it->compressAlg).c_str(), MAX_STREAM_NAME_LEN - 1); + if (strncpy_s(task.fileName, MAX_FILE_NAME_LEN, it->filePath, MAX_FILE_NAME_LEN - 1) != EOK) { + return; + } + if (strncpy_s(task.stream, MAX_STREAM_NAME_LEN, + LogCompress::CompressType2Str(it->compressAlg).c_str(), MAX_STREAM_NAME_LEN - 1) != EOK) { + return; + } rsp.jobNum++; } WriteRspHeader(IoctlCmd::PERSIST_QUERY_RSP, sizeof(rsp)); diff --git a/services/hilogtool/main.cpp b/services/hilogtool/main.cpp index bde031d8d4fce10dbe626d39a81d451768941f79..760102871305f1744a22636833171fc61e7f225d 100644 --- a/services/hilogtool/main.cpp +++ b/services/hilogtool/main.cpp @@ -349,14 +349,18 @@ struct HilogArgs { rqst.blackTag = blackTag; rqst.tagCount = tagCount; for (i = 0; i < tagCount; i++) { - (void)strncpy_s(rqst.tags[i], MAX_TAG_LEN, tags[i].c_str(), tags[i].length()); + if (strncpy_s(rqst.tags[i], MAX_TAG_LEN, tags[i].c_str(), tags[i].length()) != EOK) { + return; + } } rqst.blackPid = blackPid; rqst.pidCount = pidCount; for (i = 0; i < pidCount; i++) { rqst.pids[i] = pids[i]; } - (void)strncpy_s(rqst.regex, MAX_REGEX_STR_LEN, regex.c_str(), regex.length()); + if (strncpy_s(rqst.regex, MAX_REGEX_STR_LEN, regex.c_str(), regex.length()) != EOK) { + return; + } rqst.noBlock = noBlock; rqst.tailLines = tailLines; } @@ -367,8 +371,12 @@ struct HilogArgs { rqst.jobId = jobId; rqst.fileNum = fileNum; rqst.fileSize = fileSize; - (void)strncpy_s(rqst.fileName, MAX_FILE_NAME_LEN, fileName.c_str(), fileName.length()); - (void)strncpy_s(rqst.stream, MAX_STREAM_NAME_LEN, stream.c_str(), stream.length()); + if (strncpy_s(rqst.fileName, MAX_FILE_NAME_LEN, fileName.c_str(), fileName.length()) != EOK) { + return; + } + if (strncpy_s(rqst.stream, MAX_STREAM_NAME_LEN, stream.c_str(), stream.length()) != EOK) { + return; + } } void ToPersistStopRqst(PersistStopRqst& rqst)