diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index d1ca37ccd1362e7664ece14c37b839de3ff52ab4..c8bbd92fe0d1102891074895640425dfe01bc0be 100644 --- a/frameworks/libhilog/BUILD.gn +++ b/frameworks/libhilog/BUILD.gn @@ -63,7 +63,10 @@ template("libhilog_source") { "$utils_root/log_utils.cpp", ] - vsnprintf_sources = [ "$vsnprintf_root/vsnprintf_s_p.c" ] + vsnprintf_sources = [ + "$vsnprintf_root/vsnprintf_s_p.c", + "$vsnprintf_root/vsprintf_p.c", + ] sources = [ "hilog.cpp", diff --git a/frameworks/libhilog/vsnprintf/include/vsprintf_p.h b/frameworks/libhilog/vsnprintf/include/vsprintf_p.h new file mode 100644 index 0000000000000000000000000000000000000000..fecb5fca01fdf49fd50e117d1b87321f07315612 --- /dev/null +++ b/frameworks/libhilog/vsnprintf/include/vsprintf_p.h @@ -0,0 +1,24 @@ +/* + * 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 eb6ba050ff49a53068b7cc58aa54b2f986aa133e..06a14fc21332aa57ccf62dcbf3ed814229d8f0fb 100644 --- a/frameworks/libhilog/vsnprintf/output_p.inl +++ b/frameworks/libhilog/vsnprintf/output_p.inl @@ -19,6 +19,8 @@ * And sufficient input validation is performed before calling */ +#include "vsprintf_p.h" + #ifndef OUTPUT_P_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 #define OUTPUT_P_INL_2B263E9C_43D8_44BB_B17A_6D2033DECEE5 @@ -58,14 +60,14 @@ typedef union { #endif } SecBuffer; -static int SecIndirectSprintf(char *strDest, const char *format, ...) +static int SecIndirectSprintf(char *strDest, size_t destMax, const char *format, ...) { int ret; /* If initialization causes e838 */ va_list arglist; va_start(arglist, format); SECUREC_MASK_MSVC_CRT_WARNING - ret = vsprintf(strDest, format, arglist); + ret = VsprintfP(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 */ @@ -689,6 +691,7 @@ NORMAL_CHAR: /* floating point conversion */ formatBuf.str = buffer.str; /* output buffer for float string with default size */ + size_t formatBufLen = sizeof(buffer); /* compute the precision value */ if (formatAttr.precision < 0) { @@ -717,8 +720,8 @@ NORMAL_CHAR: if (bufferSize >= SECUREC_BUFFER_SIZE) { /* the current value of SECUREC_BUFFER_SIZE could NOT store the formatted float string */ - /* size include '+' and '\0' */ - floatBuf = (char *)SECUREC_MALLOC(((size_t)(unsigned int)bufferSize + (size_t)2)); + formatBufLen = (size_t)(unsigned int)bufferSize + (size_t)2; // size 2: include '+' and '\0' + floatBuf = (char *)SECUREC_MALLOC(formatBufLen); if (floatBuf != NULL) { formatBuf.str = floatBuf; } else { @@ -772,32 +775,32 @@ NORMAL_CHAR: long double tmp = (long double)va_arg(arglist, long double); /* call system sprintf to format float value */ if (formatAttr.dynWidth && formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, formatAttr.fldWidth,formatAttr.precision, tmp); } else if (formatAttr.dynWidth) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, formatAttr.fldWidth, tmp); } else if (formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, formatAttr.precision, tmp); } else { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, tmp); + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, tmp); } } else #endif { double tmp = (double)va_arg(arglist, double); if (formatAttr.dynWidth && formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, formatAttr.fldWidth, - formatAttr.precision, tmp); + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, + formatAttr.fldWidth, formatAttr.precision, tmp); } else if (formatAttr.dynWidth) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, formatAttr.fldWidth, - tmp); + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, + formatAttr.fldWidth, tmp); } else if (formatAttr.dynPrecision) { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, formatAttr.precision, - tmp); + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, + formatAttr.precision, tmp); } else { - textLen = SecIndirectSprintf(formatBuf.str, (char *)fltFmtStr, tmp); + textLen = SecIndirectSprintf(formatBuf.str, formatBufLen, (char *)fltFmtStr, tmp); } } diff --git a/frameworks/libhilog/vsnprintf/vsprintf_p.c b/frameworks/libhilog/vsnprintf/vsprintf_p.c new file mode 100644 index 0000000000000000000000000000000000000000..1df454f2109a11c1b1db7fab55545f94a109f84a --- /dev/null +++ b/frameworks/libhilog/vsnprintf/vsprintf_p.c @@ -0,0 +1,23 @@ +/* + * 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 6acc3af25f77d05e4b64b43da01103993b92f330..bdb885ec6da9e0f26e1a8709bee54d0df6e147f6 100644 --- a/interfaces/native/innerkits/BUILD.gn +++ b/interfaces/native/innerkits/BUILD.gn @@ -136,7 +136,10 @@ ohos_static_library("libhilog_base") { "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", ] - vsnprintf_sources = [ "$vsnprintf_root/vsnprintf_s_p.c" ] + vsnprintf_sources = [ + "$vsnprintf_root/vsnprintf_s_p.c", + "$vsnprintf_root/vsprintf_p.c", + ] sources = [ "$libhilog_base_root/hilog_base.c" ] sources += vsnprintf_sources @@ -145,6 +148,7 @@ ohos_static_library("libhilog_base") { "HILOG_PROHIBIT_ALLOCATION", ] configs = [ ":libhilog_base_config" ] + external_deps = [ "bounds_checking_function:libsec_static" ] subsystem_name = "hiviewdfx" part_name = "hilog"