From c4ccb01eeb635945fc2283f6f4ef1e6bed66006d Mon Sep 17 00:00:00 2001 From: liuyifei Date: Tue, 3 Sep 2024 08:12:12 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E9=9D=9E=E5=AE=89=E5=85=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=95=B4=E6=94=B9-5.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuyifei --- frameworks/libhilog/BUILD.gn | 5 +++- .../libhilog/vsnprintf/include/vsprintf_p.h | 24 +++++++++++++++++++ frameworks/libhilog/vsnprintf/output_p.inl | 21 +++++++++++++--- frameworks/libhilog/vsnprintf/vsprintf_p.c | 23 ++++++++++++++++++ interfaces/native/innerkits/BUILD.gn | 6 ++++- 5 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 frameworks/libhilog/vsnprintf/include/vsprintf_p.h create mode 100644 frameworks/libhilog/vsnprintf/vsprintf_p.c diff --git a/frameworks/libhilog/BUILD.gn b/frameworks/libhilog/BUILD.gn index d1ca37c..c8bbd92 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 0000000..fecb5fc --- /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 eb6ba05..ba87839 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) { @@ -718,7 +721,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 { @@ -773,15 +777,19 @@ NORMAL_CHAR: /* 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 @@ -790,14 +798,21 @@ NORMAL_CHAR: 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 0000000..1df454f --- /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 6acc3af..bdb885e 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" -- Gitee From 8d986068e797160214b115bd8a54b39803cb1efa Mon Sep 17 00:00:00 2001 From: liuyifei Date: Tue, 3 Sep 2024 08:14:35 +0000 Subject: [PATCH 2/3] update frameworks/libhilog/vsnprintf/output_p.inl. Signed-off-by: liuyifei --- frameworks/libhilog/vsnprintf/output_p.inl | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/frameworks/libhilog/vsnprintf/output_p.inl b/frameworks/libhilog/vsnprintf/output_p.inl index ba87839..01dd69c 100644 --- a/frameworks/libhilog/vsnprintf/output_p.inl +++ b/frameworks/libhilog/vsnprintf/output_p.inl @@ -776,19 +776,15 @@ 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 @@ -796,22 +792,15 @@ NORMAL_CHAR: { 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); } } -- Gitee From f3ecec1a7a24823ee538dfaeeabca22a25565e33 Mon Sep 17 00:00:00 2001 From: liuyifei Date: Tue, 3 Sep 2024 08:16:14 +0000 Subject: [PATCH 3/3] update frameworks/libhilog/vsnprintf/output_p.inl. Signed-off-by: liuyifei --- frameworks/libhilog/vsnprintf/output_p.inl | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/libhilog/vsnprintf/output_p.inl b/frameworks/libhilog/vsnprintf/output_p.inl index 01dd69c..06a14fc 100644 --- a/frameworks/libhilog/vsnprintf/output_p.inl +++ b/frameworks/libhilog/vsnprintf/output_p.inl @@ -720,7 +720,6 @@ 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' */ formatBufLen = (size_t)(unsigned int)bufferSize + (size_t)2; // size 2: include '+' and '\0' floatBuf = (char *)SECUREC_MALLOC(formatBufLen); if (floatBuf != NULL) { -- Gitee